Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up to the previous PR "✔️ Anthropic model added" #82

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ TAVILY_API_KEY=

# Only writers can set a specific model. It must be compatible with the OpenAI API.
# USE_SPECIFIC_API_FOR_WRITER=true
# Mention specific provider below("anthorpic" or "openai")
# SPECIFIC_PROVIDER=
# SPECIFIC_API_BASE=
# SPECIFIC_API_KEY=
# SPECIFIC_API_MODEL=
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TAVILY_API_KEY=[YOUR_TAVILY_API_KEY]

# Only writers can set a specific model. It must be compatible with the OpenAI API.
# USE_SPECIFIC_API_FOR_WRITER=true
# Mention specific provider below("anthorpic" or "openai")
# SPECIFIC_PROVIDER=
# SPECIFIC_API_BASE=
# SPECIFIC_API_KEY=
# SPECIFIC_API_MODEL=
Expand Down
Binary file modified bun.lockb
Binary file not shown.
25 changes: 17 additions & 8 deletions lib/agents/writer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import { createStreamableUI, createStreamableValue } from 'ai/rsc'
import { ExperimentalMessage, experimental_streamText } from 'ai'
import { Section } from '@/components/section'
import { BotMessage } from '@/components/message'

import { createAnthropic } from '@ai-sdk/anthropic';
export async function writer(
uiStream: ReturnType<typeof createStreamableUI>,
streamText: ReturnType<typeof createStreamableValue<string>>,
messages: ExperimentalMessage[]
) {
const openai = new OpenAI({
baseUrl: process.env.SPECIFIC_API_BASE,
apiKey: process.env.SPECIFIC_API_KEY,
organization: '' // optional organization
})

var openai, anthropic;
if (process.env.SPECIFIC_PROVIDER === 'anthropic') {
anthropic = createAnthropic({
baseUrl: process.env.SPECIFIC_API_BASE,
apiKey: process.env.SPECIFIC_API_KEY,
})
} else {
openai = new OpenAI({
baseUrl: process.env.SPECIFIC_API_BASE,
apiKey: process.env.SPECIFIC_API_KEY,
organization: '' // optional organization
})
}
let fullResponse = ''
const answerSection = (
<Section title="Answer">
Expand All @@ -24,7 +31,9 @@ export async function writer(
uiStream.append(answerSection)

await experimental_streamText({
model: openai.chat(process.env.SPECIFIC_API_MODEL || 'llama3-70b-8192'),
model: process.env.SPECIFIC_PROVIDER === 'anthropic' ?
anthropic!(process.env.SPECIFIC_API_MODEL || 'claude-3-haiku-20240307') :
openai!.chat(process.env.SPECIFIC_API_MODEL || 'llama3-70b-8192'),
maxTokens: 2500,
system: `As a professional writer, your job is to generate a comprehensive and informative, yet concise answer of 400 words or less for the given question based solely on the provided search results (URL and content). You must only use information from the provided search results. Use an unbiased and journalistic tone. Combine search results together into a coherent answer. Do not repeat text. If there are any images relevant to your answer, be sure to include them as well. Aim to directly address the user's question, augmenting your response with insights gleaned from the search results.
Whenever quoting or referencing information from a specific URL, always cite the source URL explicitly. Please match the language of the response to the user's language.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"lint": "next lint"
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.7",
"@ai-sdk/openai": "^0.0.2",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
Expand Down