-
Notifications
You must be signed in to change notification settings - Fork 295
Add OpenAI provider tools #1582
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
baec6f2
Add OpenAI provider tools
rosetta-livekit-bot[bot] 0913c49
Test OpenAI provider tool serialization
rosetta-livekit-bot[bot] 70fbb20
Document OpenAI provider tools
rosetta-livekit-bot[bot] 7648d6b
Align OpenAI tool docs tone
rosetta-livekit-bot[bot] 812f4cd
Omit unset code interpreter container
rosetta-livekit-bot[bot] 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/agents-plugin-openai': minor | ||
| --- | ||
|
|
||
| Add OpenAI Responses provider tools for web search, file search, and code interpreter. |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { llm } from '@livekit/agents'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { z } from 'zod'; | ||
| import { toResponsesTools } from './tool_utils.js'; | ||
| import { CodeInterpreter, FileSearch, WebSearch } from './tools.js'; | ||
|
|
||
| describe('toResponsesTools', () => { | ||
| it('serializes function tools', () => { | ||
| const fn = llm.tool({ | ||
| name: 'lookup_weather', | ||
| description: 'Look up weather', | ||
| parameters: z.object({ city: z.string() }), | ||
| execute: async () => 'sunny', | ||
| }); | ||
|
|
||
| expect(toResponsesTools(new llm.ToolContext([fn]), true)).toEqual([ | ||
| { | ||
| type: 'function', | ||
| name: 'lookup_weather', | ||
| description: 'Look up weather', | ||
| parameters: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| type: 'object', | ||
| properties: { city: { type: 'string' } }, | ||
| required: ['city'], | ||
| additionalProperties: false, | ||
| }, | ||
| strict: true, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('serializes OpenAI provider tools', () => { | ||
| const tools = toResponsesTools( | ||
| new llm.ToolContext([ | ||
| new WebSearch({ | ||
| filters: { allowed_domains: ['docs.livekit.io'] }, | ||
| searchContextSize: 'low', | ||
| userLocation: { type: 'approximate', country: 'US' }, | ||
| }), | ||
| new FileSearch({ | ||
| vectorStoreIds: ['vs_123'], | ||
| maxNumResults: 3, | ||
| rankingOptions: { ranker: 'auto' }, | ||
| }), | ||
| new CodeInterpreter({ container: { type: 'auto', file_ids: ['file_123'] } }), | ||
| ]), | ||
| false, | ||
| ); | ||
|
|
||
| expect(tools).toEqual([ | ||
| { | ||
| type: 'web_search', | ||
| search_context_size: 'low', | ||
| filters: { allowed_domains: ['docs.livekit.io'] }, | ||
| user_location: { type: 'approximate', country: 'US' }, | ||
| }, | ||
| { | ||
| type: 'file_search', | ||
| vector_store_ids: ['vs_123'], | ||
| max_num_results: 3, | ||
| ranking_options: { ranker: 'auto' }, | ||
| }, | ||
| { type: 'code_interpreter', container: { type: 'auto', file_ids: ['file_123'] } }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('omits the code interpreter container when unset', () => { | ||
| expect(toResponsesTools(new llm.ToolContext([new CodeInterpreter()]), false)).toEqual([ | ||
| { type: 'code_interpreter' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('ignores non-OpenAI provider tools', () => { | ||
| class OtherProviderTool extends llm.ProviderTool {} | ||
|
|
||
| expect( | ||
| toResponsesTools(new llm.ToolContext([new OtherProviderTool({ id: 'other' })]), false), | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { llm } from '@livekit/agents'; | ||
| import type OpenAI from 'openai'; | ||
| import { OpenAITool } from './tools.js'; | ||
|
|
||
| export function toResponsesTools( | ||
| toolCtx: llm.ToolContext, | ||
| strictToolSchema: boolean, | ||
| ): OpenAI.Responses.Tool[] | undefined { | ||
| const tools = toolCtx | ||
| .flatten() | ||
| .map((tool) => { | ||
| if (llm.isFunctionTool(tool)) { | ||
| const oaiParams = { | ||
| type: 'function' as const, | ||
| name: tool.name, | ||
| description: tool.description, | ||
| parameters: llm.toJsonSchema( | ||
| tool.parameters, | ||
| true, | ||
| strictToolSchema, | ||
| ) as unknown as OpenAI.Responses.FunctionTool['parameters'], | ||
| } as OpenAI.Responses.FunctionTool; | ||
|
|
||
| if (strictToolSchema) { | ||
| oaiParams.strict = true; | ||
| } | ||
|
|
||
| return oaiParams; | ||
| } | ||
|
|
||
| if (tool instanceof OpenAITool) { | ||
| return tool.toToolConfig() as unknown as OpenAI.Responses.Tool; | ||
| } | ||
|
|
||
| return undefined; | ||
| }) | ||
| .filter((tool): tool is OpenAI.Responses.Tool => tool !== undefined); | ||
|
|
||
| return tools.length > 0 ? tools : undefined; | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { llm } from '@livekit/agents'; | ||
| import type OpenAI from 'openai'; | ||
|
|
||
| /** A provider tool for the OpenAI Responses API. */ | ||
| export abstract class OpenAITool extends llm.ProviderTool { | ||
| /** Convert the tool to an OpenAI Responses API tool configuration. */ | ||
| abstract toToolConfig(): Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
| * High level guidance for the amount of context window space to use for the search. | ||
| * One of `low`, `medium`, or `high`. `medium` is the default. | ||
| */ | ||
| export type WebSearchContextSize = 'low' | 'medium' | 'high'; | ||
|
|
||
| /** Options for the web search tool. */ | ||
| export interface WebSearchOptions { | ||
| /** | ||
| * Filters for the search. If `allowed_domains` is not provided, all domains are allowed. | ||
| */ | ||
| filters?: OpenAI.Responses.WebSearchTool['filters']; | ||
|
|
||
| /** | ||
| * High level guidance for the amount of context window space to use for the search. | ||
| * One of `low`, `medium`, or `high`. `medium` is the default. | ||
| */ | ||
| searchContextSize?: WebSearchContextSize | null; | ||
|
|
||
| /** The approximate location of the user. */ | ||
| userLocation?: OpenAI.Responses.WebSearchTool['user_location']; | ||
| } | ||
|
|
||
| /** | ||
| * Search the Internet for sources related to the prompt. | ||
| * | ||
| * @see https://platform.openai.com/docs/guides/tools-web-search | ||
| */ | ||
| export class WebSearch extends OpenAITool { | ||
| /** Filters for the search. */ | ||
| readonly filters: OpenAI.Responses.WebSearchTool['filters'] | undefined; | ||
|
|
||
| /** High level guidance for the amount of context window space to use for the search. */ | ||
| readonly searchContextSize: WebSearchContextSize | null; | ||
|
|
||
| /** The approximate location of the user. */ | ||
| readonly userLocation: OpenAI.Responses.WebSearchTool['user_location'] | undefined; | ||
|
|
||
| constructor({ filters, searchContextSize = 'medium', userLocation }: WebSearchOptions = {}) { | ||
| super({ id: 'openai_web_search' }); | ||
| this.filters = filters; | ||
| this.searchContextSize = searchContextSize; | ||
| this.userLocation = userLocation; | ||
| } | ||
|
|
||
| toToolConfig(): Record<string, unknown> { | ||
| const result: Record<string, unknown> = { | ||
| type: 'web_search', | ||
| search_context_size: this.searchContextSize, | ||
| }; | ||
| if (this.userLocation !== undefined) { | ||
| result.user_location = this.userLocation; | ||
| } | ||
| if (this.filters !== undefined) { | ||
| result.filters = this.filters; | ||
| } | ||
| return result; | ||
| } | ||
|
toubatbrian marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Options for the file search tool. */ | ||
| export interface FileSearchOptions { | ||
| /** The IDs of the vector stores to search. */ | ||
| vectorStoreIds?: string[]; | ||
|
|
||
| /** A filter to apply. */ | ||
| filters?: OpenAI.Responses.FileSearchTool['filters']; | ||
|
|
||
| /** The maximum number of results to return. This number should be between 1 and 50 inclusive. */ | ||
| maxNumResults?: number; | ||
|
|
||
| /** Ranking options for search. */ | ||
| rankingOptions?: OpenAI.Responses.FileSearchTool.RankingOptions; | ||
| } | ||
|
|
||
| /** | ||
| * A tool that searches for relevant content from uploaded files. | ||
| * | ||
| * @see https://platform.openai.com/docs/guides/tools-file-search | ||
| */ | ||
| export class FileSearch extends OpenAITool { | ||
| /** The IDs of the vector stores to search. */ | ||
| readonly vectorStoreIds: string[]; | ||
|
|
||
| /** A filter to apply. */ | ||
| readonly filters: OpenAI.Responses.FileSearchTool['filters'] | undefined; | ||
|
|
||
| /** The maximum number of results to return. */ | ||
| readonly maxNumResults: number | undefined; | ||
|
|
||
| /** Ranking options for search. */ | ||
| readonly rankingOptions: OpenAI.Responses.FileSearchTool.RankingOptions | undefined; | ||
|
|
||
| constructor({ | ||
| vectorStoreIds = [], | ||
| filters, | ||
| maxNumResults, | ||
| rankingOptions, | ||
| }: FileSearchOptions = {}) { | ||
| super({ id: 'openai_file_search' }); | ||
| this.vectorStoreIds = [...vectorStoreIds]; | ||
| this.filters = filters; | ||
| this.maxNumResults = maxNumResults; | ||
| this.rankingOptions = rankingOptions; | ||
| } | ||
|
|
||
| toToolConfig(): Record<string, unknown> { | ||
| const result: Record<string, unknown> = { | ||
| type: 'file_search', | ||
| vector_store_ids: this.vectorStoreIds, | ||
| }; | ||
| if (this.filters !== undefined) { | ||
| result.filters = this.filters; | ||
| } | ||
| if (this.maxNumResults !== undefined) { | ||
| result.max_num_results = this.maxNumResults; | ||
| } | ||
| if (this.rankingOptions !== undefined) { | ||
| result.ranking_options = this.rankingOptions; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| /** Options for the code interpreter tool. */ | ||
| export interface CodeInterpreterOptions { | ||
| /** | ||
| * The code interpreter container. Can be a container ID or an object that specifies uploaded file IDs | ||
| * to make available to the code. | ||
| */ | ||
| container?: OpenAI.Responses.Tool.CodeInterpreter['container'] | null; | ||
| } | ||
|
|
||
| /** | ||
| * A tool that runs Python code to help generate a response to a prompt. | ||
| * | ||
| * @see https://platform.openai.com/docs/guides/tools-code-interpreter | ||
| */ | ||
| export class CodeInterpreter extends OpenAITool { | ||
| /** The code interpreter container. */ | ||
| readonly container: OpenAI.Responses.Tool.CodeInterpreter['container'] | null; | ||
|
|
||
| constructor({ container = null }: CodeInterpreterOptions = {}) { | ||
| super({ id: 'openai_code_interpreter' }); | ||
| this.container = container; | ||
| } | ||
|
|
||
| toToolConfig(): Record<string, unknown> { | ||
| const result: Record<string, unknown> = { type: 'code_interpreter' }; | ||
| if (this.container !== null) { | ||
| result.container = this.container; | ||
| } | ||
| return result; | ||
| } | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.