-
Notifications
You must be signed in to change notification settings - Fork 147
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
fix: assistant function call when multple tool are needed in the same… #4483
Merged
nicolasburtey
merged 2 commits into
main
from
fix--assistant-function-call-when-multple-tool-are-needed-in-the-same-query
May 20, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ import { sleep } from "@/utils" | |
import { UnknownDomainError } from "@/domain/shared" | ||
import { | ||
ChatAssistantNotFoundError, | ||
TimeoutAssistantError, | ||
UnknownChatAssistantError, | ||
} from "@/domain/support/errors" | ||
|
||
|
@@ -134,30 +135,36 @@ export const Assistant = (): ChatAssistant => { | |
} | ||
} | ||
|
||
const processAction = async (run: OpenAI.Beta.Threads.Runs.Run) => { | ||
const processAction = async (run: OpenAI.Beta.Threads.Runs.Run): Promise<string[]> => { | ||
const action = run.required_action | ||
assert(action?.type === "submit_tool_outputs") | ||
|
||
const name = action.submit_tool_outputs.tool_calls[0].function.name | ||
assert(name === "queryBlinkKnowledgeBase") | ||
const outputs: string[] = [] | ||
|
||
const args = action.submit_tool_outputs.tool_calls[0].function.arguments | ||
const query = JSON.parse(args).query_str | ||
for (const toolCall of action.submit_tool_outputs.tool_calls) { | ||
const name = toolCall.function.name | ||
assert(name === "queryBlinkKnowledgeBase") | ||
|
||
const vector = await textToVector(query) | ||
if (vector instanceof Error) throw vector | ||
const args = toolCall.function.arguments | ||
const query = JSON.parse(args).query_str | ||
|
||
const relatedQueries = await retrieveRelatedQueries(vector) | ||
if (relatedQueries instanceof Error) throw relatedQueries | ||
const vector = await textToVector(query) | ||
if (vector instanceof Error) throw vector | ||
|
||
let output = "" | ||
let i = 0 | ||
for (const query of relatedQueries) { | ||
output += `Context chunk ${i}:\n${query}\n-----\n` | ||
i += 1 | ||
const relatedQueries = await retrieveRelatedQueries(vector) | ||
if (relatedQueries instanceof Error) throw relatedQueries | ||
|
||
let output = "" | ||
let i = 0 | ||
for (const query of relatedQueries) { | ||
output += `Context chunk ${i}:\n${query}\n-----\n` | ||
i += 1 | ||
} | ||
|
||
outputs.push(output) | ||
} | ||
|
||
return output | ||
return outputs | ||
} | ||
|
||
const waitForCompletion = async ({ | ||
|
@@ -166,8 +173,11 @@ export const Assistant = (): ChatAssistant => { | |
}: { | ||
runId: string | ||
threadId: string | ||
}) => { | ||
}): Promise<true | ChatAssistantError> => { | ||
let run: OpenAI.Beta.Threads.Runs.Run | ||
const maxRetries = 60 // Assuming a 30-second timeout with 500ms sleep | ||
let retries = 0 | ||
|
||
try { | ||
run = await openai.beta.threads.runs.retrieve(threadId, runId) | ||
} catch (err) { | ||
|
@@ -177,32 +187,34 @@ export const Assistant = (): ChatAssistant => { | |
while ( | ||
["queued", "in_progress", "cancelling", "requires_action"].includes(run.status) | ||
) { | ||
// TODO: max timer for this loop | ||
// add open telemetry here? or is it already present with the http requests? | ||
if (retries >= maxRetries) { | ||
return new TimeoutAssistantError() | ||
} | ||
|
||
// Add telemetry here if needed | ||
await sleep(500) | ||
retries += 1 | ||
|
||
try { | ||
run = await openai.beta.threads.runs.retrieve(threadId, runId) | ||
} catch (err) { | ||
return new UnknownChatAssistantError(err) | ||
} | ||
|
||
if (run.status === "requires_action") { | ||
let output: string | ||
let outputs: string[] | ||
try { | ||
output = await processAction(run) | ||
outputs = await processAction(run) | ||
} catch (err) { | ||
return new UnknownChatAssistantError(err) | ||
} | ||
|
||
try { | ||
await openai.beta.threads.runs.submitToolOutputs(threadId, runId, { | ||
tool_outputs: [ | ||
{ | ||
tool_call_id: run.required_action?.submit_tool_outputs.tool_calls[0].id, | ||
output, | ||
}, | ||
], | ||
tool_outputs: outputs.map((output, index) => ({ | ||
tool_call_id: run.required_action?.submit_tool_outputs.tool_calls[index].id, | ||
output, | ||
})), | ||
}) | ||
} catch (err) { | ||
return new UnknownChatAssistantError(err) | ||
|
@@ -222,12 +234,12 @@ export const Assistant = (): ChatAssistant => { | |
const responseThread = messages.data[0] | ||
|
||
if (responseThread.content[0]?.type !== "text") { | ||
return new UnknownChatAssistantError("last message is not text") | ||
return new UnknownChatAssistantError("Last message is not text") | ||
} | ||
|
||
return true | ||
} else { | ||
return new UnknownChatAssistantError("issue running the assistant") | ||
return new UnknownChatAssistantError("Issue running the assistant") | ||
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. else block is not necessary |
||
} | ||
} | ||
|
||
|
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.
not a blocker but this is not a standard way to throw an exception. if we want to use it please catch properly
AssertionError
in the catch block