Skip to content

Commit

Permalink
Update anthropic.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
snekkenull authored Aug 1, 2023
1 parent 6153616 commit 97a7151
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/utils/anthropic.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 = "";
Expand All @@ -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;
}
};
};

0 comments on commit 97a7151

Please sign in to comment.