File size: 1,293 Bytes
879455c
 
ba986c0
879455c
b022cb9
879455c
ba986c0
 
 
 
b022cb9
 
 
 
 
 
 
 
 
 
ca2922b
b022cb9
 
879455c
b022cb9
879455c
 
 
 
 
ba986c0
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use server"

import type { ChatCompletionMessageParam } from "openai/resources/chat"
import OpenAI from "openai"
import { LLMPredictionFunctionParams } from "@/types"

export async function predict({
  systemPrompt,
  userPrompt,
  nbMaxNewTokens,
  llmVendorConfig
}: LLMPredictionFunctionParams): Promise<string> {
  const openaiApiKey = `${
    llmVendorConfig.apiKey ||
    process.env.AUTH_OPENAI_API_KEY ||
    ""
  }`
  const openaiApiModel = `${
    llmVendorConfig.modelId ||
    process.env.LLM_OPENAI_API_MODEL ||
    "gpt-4-turbo"
  }`

  const openaiApiBaseUrl = `${process.env.LLM_OPENAI_API_BASE_URL || "https://api.openai.com/v1"}`

  const openai = new OpenAI({
    apiKey: openaiApiKey,
    baseURL: openaiApiBaseUrl,
  })

  const messages: ChatCompletionMessageParam[] = [
    { role: "system", content: systemPrompt },
    { role: "user", content: userPrompt },
  ]

  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 ""
  }
}