fix(llm): end reasoning before responses
This commit is contained in:
parent
1ac6b4bec4
commit
11d2f3e5f8
4 changed files with 34 additions and 12 deletions
|
|
@ -407,21 +407,35 @@ const step = (state: ParserState, event: GeminiEvent) => {
|
||||||
if ("thoughtSignature" in part && part.thoughtSignature && "thought" in part && part.thought)
|
if ("thoughtSignature" in part && part.thoughtSignature && "thought" in part && part.thought)
|
||||||
reasoningSignature = part.thoughtSignature
|
reasoningSignature = part.thoughtSignature
|
||||||
if ("text" in part && part.text.length > 0) {
|
if ("text" in part && part.text.length > 0) {
|
||||||
lifecycle = part.thought
|
if (part.thought) {
|
||||||
? Lifecycle.reasoningDelta(
|
lifecycle = Lifecycle.reasoningDelta(
|
||||||
lifecycle,
|
lifecycle,
|
||||||
events,
|
events,
|
||||||
"reasoning-0",
|
"reasoning-0",
|
||||||
part.text,
|
part.text,
|
||||||
part.thoughtSignature ? googleMetadata({ thoughtSignature: part.thoughtSignature }) : undefined,
|
part.thoughtSignature ? googleMetadata({ thoughtSignature: part.thoughtSignature }) : undefined,
|
||||||
)
|
)
|
||||||
: Lifecycle.textDelta(lifecycle, events, "text-0", part.text)
|
continue
|
||||||
|
}
|
||||||
|
lifecycle = Lifecycle.reasoningEnd(
|
||||||
|
lifecycle,
|
||||||
|
events,
|
||||||
|
"reasoning-0",
|
||||||
|
reasoningSignature ? googleMetadata({ thoughtSignature: reasoningSignature }) : undefined,
|
||||||
|
)
|
||||||
|
lifecycle = Lifecycle.textDelta(lifecycle, events, "text-0", part.text)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("functionCall" in part) {
|
if ("functionCall" in part) {
|
||||||
const input = part.functionCall.args
|
const input = part.functionCall.args
|
||||||
const id = `tool_${nextToolCallId++}`
|
const id = `tool_${nextToolCallId++}`
|
||||||
|
lifecycle = Lifecycle.reasoningEnd(
|
||||||
|
lifecycle,
|
||||||
|
events,
|
||||||
|
"reasoning-0",
|
||||||
|
reasoningSignature ? googleMetadata({ thoughtSignature: reasoningSignature }) : undefined,
|
||||||
|
)
|
||||||
lifecycle = Lifecycle.stepStart(lifecycle, events)
|
lifecycle = Lifecycle.stepStart(lifecycle, events)
|
||||||
events.push(
|
events.push(
|
||||||
LLMEvent.toolCall({
|
LLMEvent.toolCall({
|
||||||
|
|
|
||||||
|
|
@ -411,7 +411,12 @@ const step = (state: ParserState, event: OpenAIChatEvent) =>
|
||||||
if (delta?.reasoning_content)
|
if (delta?.reasoning_content)
|
||||||
lifecycle = Lifecycle.reasoningDelta(lifecycle, events, "reasoning-0", delta.reasoning_content)
|
lifecycle = Lifecycle.reasoningDelta(lifecycle, events, "reasoning-0", delta.reasoning_content)
|
||||||
|
|
||||||
if (delta?.content) lifecycle = Lifecycle.textDelta(lifecycle, events, "text-0", delta.content)
|
if (delta?.content) {
|
||||||
|
lifecycle = Lifecycle.reasoningEnd(lifecycle, events, "reasoning-0")
|
||||||
|
lifecycle = Lifecycle.textDelta(lifecycle, events, "text-0", delta.content)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolDeltas.length) lifecycle = Lifecycle.reasoningEnd(lifecycle, events, "reasoning-0")
|
||||||
|
|
||||||
for (const tool of toolDeltas) {
|
for (const tool of toolDeltas) {
|
||||||
const result = ToolStream.appendOrStart(
|
const result = ToolStream.appendOrStart(
|
||||||
|
|
|
||||||
|
|
@ -347,10 +347,10 @@ describe("Gemini route", () => {
|
||||||
{ type: "step-start", index: 0 },
|
{ type: "step-start", index: 0 },
|
||||||
{ type: "reasoning-start", id: "reasoning-0" },
|
{ type: "reasoning-start", id: "reasoning-0" },
|
||||||
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
|
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
|
||||||
|
{ type: "reasoning-end", id: "reasoning-0" },
|
||||||
{ type: "text-start", id: "text-0" },
|
{ type: "text-start", id: "text-0" },
|
||||||
{ type: "text-delta", id: "text-0", text: "Hello" },
|
{ type: "text-delta", id: "text-0", text: "Hello" },
|
||||||
{ type: "text-delta", id: "text-0", text: "!" },
|
{ type: "text-delta", id: "text-0", text: "!" },
|
||||||
{ type: "reasoning-end", id: "reasoning-0" },
|
|
||||||
{ type: "text-end", id: "text-0" },
|
{ type: "text-end", id: "text-0" },
|
||||||
{ type: "step-finish", index: 0, reason: "stop", usage, providerMetadata: undefined },
|
{ type: "step-finish", index: 0, reason: "stop", usage, providerMetadata: undefined },
|
||||||
{
|
{
|
||||||
|
|
@ -399,6 +399,9 @@ describe("Gemini route", () => {
|
||||||
providerMetadata: { google: { thoughtSignature: "thought_sig" } },
|
providerMetadata: { google: { thoughtSignature: "thought_sig" } },
|
||||||
})
|
})
|
||||||
expect(toolCall).toMatchObject({ providerMetadata: { google: { thoughtSignature: "tool_sig" } } })
|
expect(toolCall).toMatchObject({ providerMetadata: { google: { thoughtSignature: "tool_sig" } } })
|
||||||
|
expect(response.events.findIndex((event) => event.type === "reasoning-end")).toBeLessThan(
|
||||||
|
response.events.findIndex((event) => event.type === "tool-call"),
|
||||||
|
)
|
||||||
|
|
||||||
const prepared = yield* LLMClient.prepare<Gemini.GeminiBody>(
|
const prepared = yield* LLMClient.prepare<Gemini.GeminiBody>(
|
||||||
LLM.request({
|
LLM.request({
|
||||||
|
|
|
||||||
|
|
@ -542,9 +542,9 @@ describe("OpenAI Chat route", () => {
|
||||||
{ type: "step-start", index: 0 },
|
{ type: "step-start", index: 0 },
|
||||||
{ type: "reasoning-start", id: "reasoning-0" },
|
{ type: "reasoning-start", id: "reasoning-0" },
|
||||||
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
|
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
|
||||||
|
{ type: "reasoning-end", id: "reasoning-0" },
|
||||||
{ type: "text-start", id: "text-0" },
|
{ type: "text-start", id: "text-0" },
|
||||||
{ type: "text-delta", id: "text-0", text: "Hello" },
|
{ type: "text-delta", id: "text-0", text: "Hello" },
|
||||||
{ type: "reasoning-end", id: "reasoning-0" },
|
|
||||||
{ type: "text-end", id: "text-0" },
|
{ type: "text-end", id: "text-0" },
|
||||||
{ type: "step-finish", index: 0, reason: "stop" },
|
{ type: "step-finish", index: 0, reason: "stop" },
|
||||||
{ type: "finish", reason: "stop" },
|
{ type: "finish", reason: "stop" },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue