From 4c6913644b362b28f15b125c2fbe48165f1e048c Mon Sep 17 00:00:00 2001 From: polwex Date: Thu, 13 Feb 2025 12:29:32 +0700 Subject: init --- src/openai_tools.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/openai_tools.ts (limited to 'src/openai_tools.ts') diff --git a/src/openai_tools.ts b/src/openai_tools.ts new file mode 100644 index 0000000..feb2e4a --- /dev/null +++ b/src/openai_tools.ts @@ -0,0 +1,66 @@ +import type OpenAI from "openai"; +import type { Result } from "./types"; +type ToolCall = OpenAI.Chat.Completions.ChatCompletionMessageToolCall; + +type Tool = OpenAI.Chat.Completions.ChatCompletionTool; +type ToolMsg = OpenAI.Chat.Completions.ChatCompletionToolMessageParam; + +type Message = OpenAI.Chat.Completions.ChatCompletionMessage; + +export default class OpenAIToolUse { + api; + model; + socket; + tools; + message; + calls; + res: ToolMsg | null = null; + constructor( + api: OpenAI, + model: string, + tools: Tool[], + message: Message, + calls: ToolCall[], + ) { + this.api = api; + this.model = model; + this.socket = new WebSocket("http://localhost:8900"); + this.tools = tools; + this.message = message; + this.calls = calls; + for (let c of calls) { + console.log({ c }); + } + this.wsHandlers(); + } + wsHandlers() { + this.socket.addEventListener("open", (_data) => { + this.handleToolCalls(); + }); + this.socket.addEventListener("message", (ev) => { + const j = JSON.parse(ev.data); + if ("functionRes" in j) this.handleRes(j.functionRes); + }); + } + handleToolCalls() { + for (let c of this.calls) this.socket.send(JSON.stringify({ call: c })); + } + async handleRes(res: Result) { + if ("error" in res) { + console.log("TODO"); + return; + } + this.res = res.ok; + const messages = [this.message, res.ok]; + console.log({ messages }, "almost there"); + const completion = await this.api.chat.completions.create({ + model: this.model, + messages, + tools: this.tools, + }); + console.log({ completion }); + for (let choice of completion.choices) { + console.log({ choice }); + } + } +} -- cgit v1.2.3