-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
groq[minor]: Implement streaming tool calls #6203
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e3fb90
implemented and added test
bracesproul 6015579
chore: lint files
bracesproul 40ef156
ayrn
bracesproul 53b06d7
Merge branch 'main' of https://github.com/langchain-ai/langchainjs in…
bracesproul 270b401
chore: lint files
bracesproul 8dda83f
Merge branch 'main' into brace/stream-groq-tools
bracesproul 203f366
ensure name/id fields are only yielded once for streaming tool calls
bracesproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import { | |
LangSmithParams, | ||
type BaseChatModelParams, | ||
} from "@langchain/core/language_models/chat_models"; | ||
import * as ChatCompletionsAPI from "groq-sdk/resources/chat/completions"; | ||
import * as CompletionsAPI from "groq-sdk/resources/completions"; | ||
import { | ||
AIMessage, | ||
AIMessageChunk, | ||
|
@@ -19,6 +21,7 @@ import { | |
ToolMessage, | ||
OpenAIToolCall, | ||
isAIMessage, | ||
BaseMessageChunk, | ||
} from "@langchain/core/messages"; | ||
import { | ||
ChatGeneration, | ||
|
@@ -32,7 +35,6 @@ import { | |
} from "@langchain/openai"; | ||
import { isZodSchema } from "@langchain/core/utils/types"; | ||
import Groq from "groq-sdk"; | ||
import { ChatCompletionChunk } from "groq-sdk/lib/chat_completions_ext"; | ||
import { | ||
ChatCompletion, | ||
ChatCompletionCreateParams, | ||
|
@@ -146,8 +148,8 @@ export function messageToGroqRole(message: BaseMessage): GroqRoleEnum { | |
|
||
function convertMessagesToGroqParams( | ||
messages: BaseMessage[] | ||
): Array<ChatCompletion.Choice.Message> { | ||
return messages.map((message): ChatCompletion.Choice.Message => { | ||
): Array<ChatCompletionsAPI.ChatCompletionMessage> { | ||
return messages.map((message): ChatCompletionsAPI.ChatCompletionMessage => { | ||
if (typeof message.content !== "string") { | ||
throw new Error("Non string message content not supported"); | ||
} | ||
|
@@ -172,12 +174,12 @@ function convertMessagesToGroqParams( | |
completionParam.tool_call_id = (message as ToolMessage).tool_call_id; | ||
} | ||
} | ||
return completionParam as ChatCompletion.Choice.Message; | ||
return completionParam as ChatCompletionsAPI.ChatCompletionMessage; | ||
}); | ||
} | ||
|
||
function groqResponseToChatMessage( | ||
message: ChatCompletion.Choice.Message | ||
message: ChatCompletionsAPI.ChatCompletionMessage | ||
): BaseMessage { | ||
const rawToolCalls: OpenAIToolCall[] | undefined = message.tool_calls as | ||
| OpenAIToolCall[] | ||
|
@@ -206,10 +208,34 @@ function groqResponseToChatMessage( | |
} | ||
} | ||
|
||
function _convertDeltaToolCallToToolCallChunk( | ||
toolCalls?: ChatCompletionsAPI.ChatCompletionChunk.Choice.Delta.ToolCall[], | ||
index?: number | ||
): ToolCallChunk[] | undefined { | ||
if (!toolCalls?.length) return undefined; | ||
|
||
return toolCalls.map((tc) => ({ | ||
id: tc.id, | ||
name: tc.function?.name, | ||
args: tc.function?.arguments, | ||
type: "tool_call_chunk", | ||
index, | ||
})); | ||
} | ||
|
||
function _convertDeltaToMessageChunk( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delta: Record<string, any> | ||
) { | ||
delta: Record<string, any>, | ||
index: number | ||
): { | ||
message: BaseMessageChunk; | ||
toolCallData?: { | ||
id: string; | ||
name: string; | ||
index: number; | ||
type: "tool_call_chunk"; | ||
}[]; | ||
} { | ||
const { role } = delta; | ||
const content = delta.content ?? ""; | ||
let additional_kwargs; | ||
|
@@ -225,13 +251,43 @@ function _convertDeltaToMessageChunk( | |
additional_kwargs = {}; | ||
} | ||
if (role === "user") { | ||
return new HumanMessageChunk({ content }); | ||
return { | ||
message: new HumanMessageChunk({ content }), | ||
}; | ||
} else if (role === "assistant") { | ||
return new AIMessageChunk({ content, additional_kwargs }); | ||
const toolCallChunks = _convertDeltaToolCallToToolCallChunk( | ||
delta.tool_calls, | ||
index | ||
); | ||
return { | ||
message: new AIMessageChunk({ | ||
content, | ||
additional_kwargs, | ||
tool_call_chunks: toolCallChunks | ||
? toolCallChunks.map((tc) => ({ | ||
type: tc.type, | ||
args: tc.args, | ||
index: tc.index, | ||
})) | ||
: undefined, | ||
}), | ||
toolCallData: toolCallChunks | ||
? toolCallChunks.map((tc) => ({ | ||
id: tc.id ?? "", | ||
name: tc.name ?? "", | ||
index: tc.index ?? index, | ||
type: "tool_call_chunk", | ||
})) | ||
: undefined, | ||
}; | ||
} else if (role === "system") { | ||
return new SystemMessageChunk({ content }); | ||
return { | ||
message: new SystemMessageChunk({ content }), | ||
}; | ||
} else { | ||
return new ChatMessageChunk({ content, role }); | ||
return { | ||
message: new ChatMessageChunk({ content, role }), | ||
}; | ||
} | ||
} | ||
|
||
|
@@ -322,16 +378,16 @@ export class ChatGroq extends BaseChatModel< | |
ls_provider: "groq", | ||
ls_model_name: this.model, | ||
ls_model_type: "chat", | ||
ls_temperature: params.temperature, | ||
ls_max_tokens: params.max_tokens, | ||
ls_temperature: params.temperature ?? this.temperature, | ||
ls_max_tokens: params.max_tokens ?? this.maxTokens, | ||
ls_stop: options.stop, | ||
}; | ||
} | ||
|
||
async completionWithRetry( | ||
request: ChatCompletionCreateParamsStreaming, | ||
options?: OpenAICoreRequestOptions | ||
): Promise<AsyncIterable<ChatCompletionChunk>>; | ||
): Promise<AsyncIterable<ChatCompletionsAPI.ChatCompletionChunk>>; | ||
|
||
async completionWithRetry( | ||
request: ChatCompletionCreateParamsNonStreaming, | ||
|
@@ -341,7 +397,9 @@ export class ChatGroq extends BaseChatModel< | |
async completionWithRetry( | ||
request: ChatCompletionCreateParams, | ||
options?: OpenAICoreRequestOptions | ||
): Promise<AsyncIterable<ChatCompletionChunk> | ChatCompletion> { | ||
): Promise< | ||
AsyncIterable<ChatCompletionsAPI.ChatCompletionChunk> | ChatCompletion | ||
> { | ||
return this.caller.call(async () => | ||
this.client.chat.completions.create(request, options) | ||
); | ||
|
@@ -391,76 +449,73 @@ export class ChatGroq extends BaseChatModel< | |
): AsyncGenerator<ChatGenerationChunk> { | ||
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. Hey team, I've flagged this PR for your review as it introduces a new external HTTP request using |
||
const params = this.invocationParams(options); | ||
const messagesMapped = convertMessagesToGroqParams(messages); | ||
if (options.tools !== undefined && options.tools.length > 0) { | ||
const result = await this._generateNonStreaming( | ||
messages, | ||
options, | ||
runManager | ||
); | ||
const generationMessage = result.generations[0].message as AIMessage; | ||
if ( | ||
generationMessage === undefined || | ||
typeof generationMessage.content !== "string" | ||
) { | ||
throw new Error("Could not parse Groq output."); | ||
const response = await this.completionWithRetry( | ||
{ | ||
...params, | ||
messages: messagesMapped, | ||
stream: true, | ||
}, | ||
{ | ||
signal: options?.signal, | ||
headers: options?.headers, | ||
} | ||
const toolCallChunks: ToolCallChunk[] | undefined = | ||
generationMessage.tool_calls?.map((toolCall, i) => ({ | ||
name: toolCall.name, | ||
args: JSON.stringify(toolCall.args), | ||
id: toolCall.id, | ||
index: i, | ||
type: "tool_call_chunk", | ||
})); | ||
yield new ChatGenerationChunk({ | ||
message: new AIMessageChunk({ | ||
content: generationMessage.content, | ||
additional_kwargs: generationMessage.additional_kwargs, | ||
tool_call_chunks: toolCallChunks, | ||
}), | ||
text: generationMessage.content, | ||
}); | ||
} else { | ||
const response = await this.completionWithRetry( | ||
{ | ||
...params, | ||
messages: messagesMapped, | ||
stream: true, | ||
}, | ||
); | ||
let role = ""; | ||
const toolCall: { | ||
id: string; | ||
name: string; | ||
index: number; | ||
type: "tool_call_chunk"; | ||
}[] = []; | ||
for await (const data of response) { | ||
const choice = data?.choices[0]; | ||
if (!choice) { | ||
continue; | ||
} | ||
// The `role` field is populated in the first delta of the response | ||
// but is not present in subsequent deltas. Extract it when available. | ||
if (choice.delta?.role) { | ||
role = choice.delta.role; | ||
} | ||
|
||
const { message, toolCallData } = _convertDeltaToMessageChunk( | ||
{ | ||
signal: options?.signal, | ||
headers: options?.headers, | ||
} | ||
...choice.delta, | ||
role, | ||
} ?? {}, | ||
choice.index | ||
); | ||
let role = ""; | ||
for await (const data of response) { | ||
const choice = data?.choices[0]; | ||
if (!choice) { | ||
continue; | ||
} | ||
// The `role` field is populated in the first delta of the response | ||
// but is not present in subsequent deltas. Extract it when available. | ||
if (choice.delta?.role) { | ||
role = choice.delta.role; | ||
} | ||
const chunk = new ChatGenerationChunk({ | ||
message: _convertDeltaToMessageChunk( | ||
{ | ||
...choice.delta, | ||
role, | ||
} ?? {} | ||
), | ||
text: choice.delta.content ?? "", | ||
generationInfo: { | ||
finishReason: choice.finish_reason, | ||
}, | ||
|
||
if (toolCallData) { | ||
// First, ensure the ID is not already present in toolCall | ||
const newToolCallData = toolCallData.filter((tc) => | ||
toolCall.every((t) => t.id !== tc.id) | ||
); | ||
toolCall.push(...newToolCallData); | ||
|
||
// Yield here, ensuring the ID and name fields are only yielded once. | ||
yield new ChatGenerationChunk({ | ||
message: new AIMessageChunk({ | ||
content: "", | ||
tool_call_chunks: newToolCallData, | ||
}), | ||
text: "", | ||
}); | ||
yield chunk; | ||
void runManager?.handleLLMNewToken(chunk.text ?? ""); | ||
} | ||
if (options.signal?.aborted) { | ||
throw new Error("AbortError"); | ||
} | ||
|
||
const chunk = new ChatGenerationChunk({ | ||
message, | ||
text: choice.delta.content ?? "", | ||
generationInfo: { | ||
finishReason: choice.finish_reason, | ||
}, | ||
}); | ||
yield chunk; | ||
void runManager?.handleLLMNewToken(chunk.text ?? ""); | ||
} | ||
|
||
if (options.signal?.aborted) { | ||
throw new Error("AbortError"); | ||
} | ||
} | ||
|
||
|
@@ -518,7 +573,7 @@ export class ChatGroq extends BaseChatModel< | |
completion_tokens: completionTokens, | ||
prompt_tokens: promptTokens, | ||
total_tokens: totalTokens, | ||
} = data.usage as ChatCompletion.Usage; | ||
} = data.usage as CompletionsAPI.CompletionUsage; | ||
|
||
if (completionTokens) { | ||
tokenUsage.completionTokens = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hey there! 👋 I noticed that the dependency changes in the package.json file might impact the project's peer/dev/hard dependencies. I've flagged this for your review as it's important to ensure compatibility and stability. Keep up the great work! 🚀