CoolFace
Apppublic

WaledRashed24/comics

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
predictWithAnthropic.ts48 linesDownload Raw Back to queries
1"use server"2 3import { LLMPredictionFunctionParams } from '@/types';4import Anthropic from '@anthropic-ai/sdk';5import { MessageParam } from '@anthropic-ai/sdk/resources';6 7export async function predict({8  systemPrompt,9  userPrompt,10  nbMaxNewTokens,11  llmVendorConfig12}: LLMPredictionFunctionParams): Promise<string> {13  const anthropicApiKey = `${14    llmVendorConfig.apiKey ||15    process.env.AUTH_ANTHROPIC_API_KEY ||16    ""17  }`18  const anthropicApiModel = `${19    llmVendorConfig.modelId ||20    process.env.LLM_ANTHROPIC_API_MODEL ||21    "claude-3-opus-20240229"22  }`23  if (!anthropicApiKey) { throw new Error(`cannot call Anthropic without an API key`) }24  25  const anthropic = new Anthropic({26    apiKey: anthropicApiKey,27  })28 29  const messages: MessageParam[] = [30    { role: "user", content: userPrompt },31  ]32 33  try {34    const res = await anthropic.messages.create({35      messages: messages,36      // stream: false,37      system: systemPrompt,38      model: anthropicApiModel,39      // temperature: 0.8,40      max_tokens: nbMaxNewTokens,41    })42 43    return res.content[0]?.text || ""44  } catch (err) {45    console.error(`error during generation: ${err}`)46    return ""47  }48}