-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAIService.js
59 lines (55 loc) · 1.54 KB
/
OpenAIService.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 { OPENAI_API_KEY } from "@env";
export const fetchAISuggestions = async (promptText) => {
try {
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-3.5-turbo-instruct",
prompt: promptText,
temperature: 0.5,
max_tokens: 1000,
}),
});
const data = await response.json();
if (response.ok) {
return data.choices[0].text;
} else {
throw new Error(data.error.message);
}
} catch (error) {
console.error("Error fetching AI suggestions:", error);
throw error;
}
};
export const fetchVetAdvice = async (conversationHistory) => {
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-3.5-turbo-1106",
messages: conversationHistory,
temperature: 0.9,
max_tokens: 200,
}),
});
const data = await response.json();
if (response.ok) {
const lastMessage =
data.choices[data.choices.length - 1].message.content.trim();
return lastMessage;
} else {
throw new Error(data.error.message);
}
} catch (error) {
console.error("Error fetching vet advice:", error);
throw error;
}
};