Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,041 Bytes
879455c d64b893 6463491 879455c e22103d 879455c 5dd2af5 d64b893 5dd2af5 879455c 6463491 879455c 6463491 879455c |
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 |
"use server"
import type { ChatCompletionMessage } from "openai/resources/chat"
import OpenAI from "openai"
export async function predict(inputs: string, nbMaxNewTokens: number): Promise<string> {
const openaiApiKey = `${process.env.AUTH_OPENAI_API_KEY || ""}`
const openaiApiBaseUrl = `${process.env.LLM_OPENAI_API_BASE_URL || "https://api.openai.com/v1"}`
const openaiApiModel = `${process.env.LLM_OPENAI_API_MODEL || "gpt-3.5-turbo"}`
const openai = new OpenAI({
apiKey: openaiApiKey,
baseURL: openaiApiBaseUrl,
})
const messages: ChatCompletionMessage[] = [
{ role: "assistant", content: inputs },
]
try {
const res = await openai.chat.completions.create({
messages: messages,
stream: false,
model: openaiApiModel,
temperature: 0.8,
max_tokens: nbMaxNewTokens,
// TODO: use the nbPanels to define a max token limit
})
return res.choices[0].message.content || ""
} catch (err) {
console.error(`error during generation: ${err}`)
return ""
}
} |