diff --git a/src/utils/anthropic.ts b/src/utils/anthropic.ts index ea28592..01050fc 100644 --- a/src/utils/anthropic.ts +++ b/src/utils/anthropic.ts @@ -1,4 +1,4 @@ -import { AI_PROMPT, Client, HUMAN_PROMPT } from "@anthropic-ai/sdk"; +import Anthropic, { HUMAN_PROMPT } from "@anthropic-ai/sdk"; import type { ChatMessage } from '@/types'; const apiKey = import.meta.env.ANTHROPIC_API_KEY; @@ -7,7 +7,8 @@ const model = import.meta.env.ANTHROPIC_API_MODEL || 'claude-2'; const max_tokens = import.meta.env.ANTHROPIC_API_TOKEN || '1000000'; -export const client = new Client(apiKey); +// Instantiate with "apiKey" as an object property +const client = new Anthropic({ apiKey }); export const generatePrompt = (messages: ChatMessage[]): string => { let prompt = ""; @@ -20,17 +21,25 @@ export const generatePrompt = (messages: ChatMessage[]): string => { export const completeWithAnthropic = async (prompt: string) => { try { - const response = await client.complete({ + const params = { prompt, model, stop_sequences: [HUMAN_PROMPT], max_tokens_to_sample: max_tokens, stream: true, - }); + }; - return response.completion; + let text = ''; + // Use async iterator to get the completion + const stream = await client.completions.create(params); + for await (const completion of stream) { + const diff = completion.completion; + text += diff; + } + + return text; } catch (error) { console.error("Anthropic API Error: ", error); throw error; } -}; \ No newline at end of file +};