summaryrefslogtreecommitdiff
path: root/src/openai_tools.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/openai_tools.ts')
-rw-r--r--src/openai_tools.ts66
1 files changed, 66 insertions, 0 deletions
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<ToolMsg>) {
+ 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 });
+ }
+ }
+}