-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: Add DeepSeek AI provider support #1026
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import BaseEmbedding from '../../base/embedding'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| type DeepSeekEmbeddingConfig = { | ||
| apiKey: string; | ||
| model: string; | ||
| baseURL?: string; | ||
| }; | ||
|
|
||
| class DeepSeekEmbedding extends BaseEmbedding<DeepSeekEmbeddingConfig> { | ||
| deepseekClient: OpenAI; | ||
|
|
||
| constructor(protected config: DeepSeekEmbeddingConfig) { | ||
| super(config); | ||
|
|
||
| this.deepseekClient = new OpenAI({ | ||
| apiKey: this.config.apiKey, | ||
| baseURL: this.config.baseURL || 'https://api.deepseek.com/v1', | ||
| }); | ||
| } | ||
|
|
||
| async embedDocuments(documents: string[]): Promise<number[][]> { | ||
| const response = await this.deepseekClient.embeddings.create({ | ||
| model: this.config.model, | ||
| input: documents, | ||
| }); | ||
|
|
||
| return response.data.map((item) => item.embedding); | ||
| } | ||
|
|
||
| async embedQuery(document: string): Promise<number[]> { | ||
| const response = await this.deepseekClient.embeddings.create({ | ||
| model: this.config.model, | ||
| input: [document], | ||
| }); | ||
|
|
||
| return response.data[0].embedding; | ||
| } | ||
| } | ||
|
|
||
| export default DeepSeekEmbedding; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| import OpenAI from 'openai'; | ||
| import BaseLLM from '../../base/llm'; | ||
| import { zodTextFormat, zodResponseFormat } from 'openai/helpers/zod'; | ||
| import { | ||
| GenerateObjectInput, | ||
| GenerateTextInput, | ||
| GenerateTextOutput, | ||
| StreamTextOutput, | ||
| ToolCall, | ||
| } from '../../types'; | ||
| import { parse } from 'partial-json'; | ||
| import z from 'zod'; | ||
| import { | ||
| ChatCompletionAssistantMessageParam, | ||
| ChatCompletionMessageParam, | ||
| ChatCompletionTool, | ||
| ChatCompletionToolMessageParam, | ||
| } from 'openai/resources/index.mjs'; | ||
| import { Message } from '@/lib/types'; | ||
| import { repairJson } from '@toolsycc/json-repair'; | ||
|
|
||
| type DeepSeekConfig = { | ||
| apiKey: string; | ||
| model: string; | ||
| baseURL?: string; | ||
| }; | ||
|
|
||
| class DeepSeekLLM extends BaseLLM<DeepSeekConfig> { | ||
| deepseekClient: OpenAI; | ||
|
|
||
| constructor(protected config: DeepSeekConfig) { | ||
| super(config); | ||
|
|
||
| this.deepseekClient = new OpenAI({ | ||
| apiKey: this.config.apiKey, | ||
| baseURL: this.config.baseURL || 'https://api.deepseek.com/v1', | ||
| }); | ||
| } | ||
|
|
||
| convertToDeepSeekMessages(messages: Message[]): ChatCompletionMessageParam[] { | ||
| return messages.map((msg) => { | ||
| if (msg.role === 'tool') { | ||
| return { | ||
| role: 'tool', | ||
| tool_call_id: msg.id, | ||
| content: msg.content, | ||
| } as ChatCompletionToolMessageParam; | ||
| } else if (msg.role === 'assistant') { | ||
| return { | ||
| role: 'assistant', | ||
| content: msg.content, | ||
| ...(msg.tool_calls && | ||
| msg.tool_calls.length > 0 && { | ||
| tool_calls: msg.tool_calls?.map((tc) => ({ | ||
| id: tc.id, | ||
| type: 'function', | ||
| function: { | ||
| name: tc.name, | ||
| arguments: JSON.stringify(tc.arguments), | ||
| }, | ||
| })), | ||
| }), | ||
| } as ChatCompletionAssistantMessageParam; | ||
| } | ||
|
|
||
| return msg; | ||
| }); | ||
| } | ||
|
|
||
| async generateText(input: GenerateTextInput): Promise<GenerateTextOutput> { | ||
| const deepseekTools: ChatCompletionTool[] = []; | ||
|
|
||
| input.tools?.forEach((tool) => { | ||
| deepseekTools.push({ | ||
| type: 'function', | ||
| function: { | ||
| name: tool.name, | ||
| description: tool.description, | ||
| parameters: z.toJSONSchema(tool.schema), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const response = await this.deepseekClient.chat.completions.create({ | ||
| model: this.config.model, | ||
| tools: deepseekTools.length > 0 ? deepseekTools : undefined, | ||
| messages: this.convertToDeepSeekMessages(input.messages), | ||
| temperature: input.options?.temperature ?? 1.0, | ||
| top_p: input.options?.topP, | ||
| max_completion_tokens: input.options?.maxTokens, | ||
| stop: input.options?.stopSequences, | ||
| frequency_penalty: input.options?.frequencyPenalty, | ||
| presence_penalty: input.options?.presencePenalty, | ||
| }); | ||
|
|
||
| if (response.choices && response.choices.length > 0) { | ||
| return { | ||
| content: response.choices[0].message.content!, | ||
| toolCalls: | ||
| response.choices[0].message.tool_calls | ||
| ?.map((tc) => { | ||
| if (tc.type === 'function') { | ||
| return { | ||
| name: tc.function.name, | ||
| id: tc.id, | ||
| arguments: JSON.parse(tc.function.arguments), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Unguarded Prompt for AI agents |
||
| }; | ||
| } | ||
| }) | ||
| .filter((tc) => tc !== undefined) || [], | ||
| additionalInfo: { | ||
| finishReason: response.choices[0].finish_reason, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| throw new Error('No response from DeepSeek'); | ||
| } | ||
|
|
||
| async *streamText( | ||
| input: GenerateTextInput, | ||
| ): AsyncGenerator<StreamTextOutput> { | ||
| const deepseekTools: ChatCompletionTool[] = []; | ||
|
|
||
| input.tools?.forEach((tool) => { | ||
| deepseekTools.push({ | ||
| type: 'function', | ||
| function: { | ||
| name: tool.name, | ||
| description: tool.description, | ||
| parameters: z.toJSONSchema(tool.schema), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const stream = await this.deepseekClient.chat.completions.create({ | ||
| model: this.config.model, | ||
| messages: this.convertToDeepSeekMessages(input.messages), | ||
| tools: deepseekTools.length > 0 ? deepseekTools : undefined, | ||
| temperature: input.options?.temperature ?? 1.0, | ||
| top_p: input.options?.topP, | ||
| max_completion_tokens: input.options?.maxTokens, | ||
| stop: input.options?.stopSequences, | ||
| frequency_penalty: input.options?.frequencyPenalty, | ||
| presence_penalty: input.options?.presencePenalty, | ||
| stream: true, | ||
| }); | ||
|
|
||
| let recievedToolCalls: { name: string; id: string; arguments: string }[] = | ||
| []; | ||
|
|
||
| for await (const chunk of stream) { | ||
| if (chunk.choices && chunk.choices.length > 0) { | ||
| const toolCalls = chunk.choices[0].delta.tool_calls; | ||
| yield { | ||
| contentChunk: chunk.choices[0].delta.content || '', | ||
| toolCallChunk: | ||
| toolCalls?.map((tc) => { | ||
| if (!recievedToolCalls[tc.index]) { | ||
| const call = { | ||
| name: tc.function?.name!, | ||
| id: tc.id!, | ||
| arguments: tc.function?.arguments || '', | ||
| }; | ||
| recievedToolCalls.push(call); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Streaming tool-call reconstruction is mis-indexed: new calls are appended with Prompt for AI agents |
||
| return { ...call, arguments: parse(call.arguments || '{}') }; | ||
| } else { | ||
| const existingCall = recievedToolCalls[tc.index]; | ||
| existingCall.arguments += tc.function?.arguments || ''; | ||
| return { | ||
| ...existingCall, | ||
| arguments: parse(existingCall.arguments), | ||
| }; | ||
| } | ||
| }) || [], | ||
| done: chunk.choices[0].finish_reason !== null, | ||
| additionalInfo: { | ||
| finishReason: chunk.choices[0].finish_reason, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async generateObject<T>(input: GenerateObjectInput): Promise<T> { | ||
| // DeepSeek doesn't support native JSON schema response format like OpenAI | ||
| // So we use the standard completion with a system prompt | ||
| const response = await this.deepseekClient.chat.completions.create({ | ||
| messages: [ | ||
| ...this.convertToDeepSeekMessages(input.messages), | ||
| { | ||
| role: 'system', | ||
| content: `You must respond with valid JSON that matches the following schema: ${JSON.stringify(z.toJSONSchema(input.schema))}. Do not include any markdown formatting, explanations, or extra text - only the raw JSON object.`, | ||
| }, | ||
| ], | ||
| model: this.config.model, | ||
| temperature: input.options?.temperature ?? 1.0, | ||
| top_p: input.options?.topP, | ||
| max_completion_tokens: input.options?.maxTokens, | ||
| stop: input.options?.stopSequences, | ||
| frequency_penalty: input.options?.frequencyPenalty, | ||
| presence_penalty: input.options?.presencePenalty, | ||
| }); | ||
|
|
||
| if (response.choices && response.choices.length > 0) { | ||
| try { | ||
| const content = response.choices[0].message.content!; | ||
| const repaired = repairJson(content, { | ||
| extractJson: true, | ||
| }) as string; | ||
| return input.schema.parse(JSON.parse(repaired)) as T; | ||
| } catch (err) { | ||
| throw new Error(`Error parsing response from DeepSeek: ${err}`); | ||
| } | ||
| } | ||
|
|
||
| throw new Error('No response from DeepSeek'); | ||
| } | ||
|
|
||
| async *streamObject<T>(input: GenerateObjectInput): AsyncGenerator<T> { | ||
| // DeepSeek doesn't support native streaming JSON response format | ||
| // Fall back to non-streaming for object generation | ||
| const result = await this.generateObject<T>(input); | ||
| yield result; | ||
| } | ||
| } | ||
|
|
||
| export default DeepSeekLLM; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: DeepSeek chat-completions payload uses
max_completion_tokensinstead of the documentedmax_tokens, risking ignored limits or request errors.Prompt for AI agents