File size: 1,188 Bytes
e015622
 
b022cb9
e015622
 
 
 
 
 
 
b022cb9
 
 
 
 
 
 
 
 
 
 
 
d0daba2
e015622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server"

import { LLMPredictionFunctionParams } from '@/types';
import Anthropic from '@anthropic-ai/sdk';
import { MessageParam } from '@anthropic-ai/sdk/resources';

export async function predict({
  systemPrompt,
  userPrompt,
  nbMaxNewTokens,
  llmVendorConfig
}: LLMPredictionFunctionParams): Promise<string> {
  const anthropicApiKey = `${
    llmVendorConfig.apiKey ||
    process.env.AUTH_ANTHROPIC_API_KEY ||
    ""
  }`
  const anthropicApiModel = `${
    llmVendorConfig.modelId ||
    process.env.LLM_ANTHROPIC_API_MODEL ||
    "claude-3-opus-20240229"
  }`
  if (!anthropicApiKey) { throw new Error(`cannot call Anthropic without an API key`) }
  
  const anthropic = new Anthropic({
    apiKey: anthropicApiKey,
  })

  const messages: MessageParam[] = [
    { role: "user", content: userPrompt },
  ]

  try {
    const res = await anthropic.messages.create({
      messages: messages,
      // stream: false,
      system: systemPrompt,
      model: anthropicApiModel,
      // temperature: 0.8,
      max_tokens: nbMaxNewTokens,
    })

    return res.content[0]?.text || ""
  } catch (err) {
    console.error(`error during generation: ${err}`)
    return ""
  }
}