-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
59 lines (51 loc) · 2.49 KB
/
ai.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Anthropic from "@anthropic-ai/sdk"
import { HfInference } from '@huggingface/inference'
const SYSTEM_PROMPT = `
You are an assistant that receives a list of ingredients that a user has and suggests a recipe they could make with some or all of those ingredients. You don't need to use every ingredient they mention in your recipe. The recipe can include additional ingredients they didn't mention, but try not to include too many extra ingredients. Format your response in markdown to make it easier to render to a web page
`
// 🚨👉 ALERT: Read message below! You've been warned! 👈🚨
// If you're following along on your local machine instead of
// here on Scrimba, make sure you don't commit your API keys
// to any repositories and don't deploy your project anywhere
// live online. Otherwise, anyone could inspect your source
// and find your API keys/tokens. If you want to deploy
// this project, you'll need to create a backend of some kind,
// either your own or using some serverless architecture where
// your API calls can be made. Doing so will keep your
// API keys private.
const anthropic = new Anthropic({
// 使用 import.meta.env 获取 API key(注意变量名称必须以 VITE_ 开头)
apiKey: import.meta.env.VITE_ANTHROPIC_API_KEY,
dangerouslyAllowBrowser: true,
})
export async function getRecipeFromChefClaude(ingredientsArr) {
const ingredientsString = ingredientsArr.join(", ")
const msg = await anthropic.messages.create({
model: "claude-3-haiku-20240307",
max_tokens: 1024,
system: SYSTEM_PROMPT,
messages: [
{ role: "user", content: `I have ${ingredientsString}. Please give me a recipe you'd recommend I make!` },
],
});
return msg.content[0].text
}
// Make sure you set an environment variable in Scrimba
// for HF_ACCESS_TOKEN
const hf = new HfInference(import.meta.env.VITE_HF_ACCESS_TOKEN)
export async function getRecipeFromMistral(ingredientsArr) {
const ingredientsString = ingredientsArr.join(", ")
try {
const response = await hf.chatCompletion({
model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: `I have ${ingredientsString}. Please give me a recipe you'd recommend I make!` },
],
max_tokens: 1024,
})
return response.choices[0].message.content
} catch (err) {
console.error(err.message)
}
}