Skip to content

Commit

Permalink
fix: 修复 anthropic 默认 max_tokens 过大的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Aug 27, 2024
1 parent a77c0e6 commit e16adf4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/llm/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ async function convertMessages(
)
}

function getModelMaxTokens(model: string): number {
if (model.startsWith('claude-3-')) {
return 4096
}
return 8192
}

export function anthropicBase(
createClient: () => AnthropicTypes,
): Omit<IAnthropicVertex, 'requiredEnv' | 'supportModels'> {
Expand All @@ -135,7 +142,7 @@ export function anthropicBase(
).includes(it.role),
),
),
max_tokens: req.max_tokens ?? 8192,
max_tokens: req.max_tokens ?? getModelMaxTokens(req.model),
temperature: req.temperature!,
metadata: {
user_id: req.user,
Expand Down
2 changes: 1 addition & 1 deletion src/llm/deepseek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IChat } from './base'
import { openaiBase } from './openai'

export function deepseek(env: Record<string, string>): IChat {
const r = openaiBase(env, {
const r = openaiBase({
apiKey: env.DEEPSEEK_API_KEY,
baseURL: 'https://api.deepseek.com',
})
Expand Down
2 changes: 1 addition & 1 deletion src/llm/groq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IChat } from './base'
import { openaiBase } from './openai'

export function groq(env: Record<string, string>): IChat {
const r = openaiBase(env, {
const r = openaiBase({
apiKey: env.GROQ_API_KEY,
baseURL: 'https://api.groq.com/openai/v1',
})
Expand Down
2 changes: 1 addition & 1 deletion src/llm/lingyiwanwu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { openaiBase } from './openai'

export function lingyiwanwu(env: Record<string, string>) {
const r = openaiBase(env, {
const r = openaiBase({
baseURL: 'https://api.lingyiwanwu.com/v1',
apiKey: env.LINGYIWANWU_API_KEY,
})
Expand Down
2 changes: 1 addition & 1 deletion src/llm/moonshot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { openaiBase } from './openai'

export function moonshot(env: Record<string, string>) {
const r = openaiBase(env, {
const r = openaiBase({
baseURL: 'https://api.moonshot.cn/v1',
apiKey: env.MOONSHOT_API_KEY,
})
Expand Down
10 changes: 4 additions & 6 deletions src/llm/openai.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import OpenAI, { ClientOptions } from 'openai'
import { IChat } from './base'

export function openaiBase(
env: Record<string, string>,
options?: ClientOptions,
): IChat {
const client = new OpenAI(options)
export function openaiBase(options?: ClientOptions): IChat {
return {
name: 'openai',
supportModels: [
Expand Down Expand Up @@ -43,9 +39,11 @@ export function openaiBase(
],
requiredEnv: ['OPENAI_API_KEY'],
invoke(req) {
const client = new OpenAI(options)
return client.chat.completions.create({ ...req, stream: false })
},
async *stream(req, signal) {
const client = new OpenAI(options)
const stream = await client.chat.completions.create(
{ ...req, stream: true },
{ signal },
Expand All @@ -58,7 +56,7 @@ export function openaiBase(
}

export function openai(env: Record<string, string>): IChat {
return openaiBase(env, {
return openaiBase({
apiKey: env.OPENAI_API_KEY,
})
}

0 comments on commit e16adf4

Please sign in to comment.