repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import OpenAI from "openai"; |
| #2 | import { LLM, LLMResponse } from "./base"; |
| #3 | import { LLMConfig, Message } from "../types"; |
| #4 | |
| #5 | export class OpenAILLM implements LLM { |
| #6 | private openai: OpenAI; |
| #7 | private model: string; |
| #8 | |
| #9 | constructor(config: LLMConfig) { |
| #10 | this.openai = new OpenAI({ |
| #11 | apiKey: config.apiKey, |
| #12 | baseURL: config.baseURL, |
| #13 | }); |
| #14 | this.model = config.model || "gpt-4.1-nano-2025-04-14"; |
| #15 | } |
| #16 | |
| #17 | async generateResponse( |
| #18 | messages: Message[], |
| #19 | responseFormat?: { type: string }, |
| #20 | tools?: any[], |
| #21 | ): Promise<string | LLMResponse> { |
| #22 | const completion = await this.openai.chat.completions.create({ |
| #23 | messages: messages.map((msg) => { |
| #24 | const role = msg.role as "system" | "user" | "assistant"; |
| #25 | return { |
| #26 | role, |
| #27 | content: |
| #28 | typeof msg.content === "string" |
| #29 | ? msg.content |
| #30 | : JSON.stringify(msg.content), |
| #31 | }; |
| #32 | }), |
| #33 | model: this.model, |
| #34 | response_format: responseFormat as { type: "text" | "json_object" }, |
| #35 | ...(tools && { tools, tool_choice: "auto" }), |
| #36 | }); |
| #37 | |
| #38 | const response = completion.choices[0].message; |
| #39 | |
| #40 | if (response.tool_calls) { |
| #41 | return { |
| #42 | content: response.content || "", |
| #43 | role: response.role, |
| #44 | toolCalls: response.tool_calls.map((call) => ({ |
| #45 | name: call.function.name, |
| #46 | arguments: call.function.arguments, |
| #47 | })), |
| #48 | }; |
| #49 | } |
| #50 | |
| #51 | return response.content || ""; |
| #52 | } |
| #53 | |
| #54 | async generateChat(messages: Message[]): Promise<LLMResponse> { |
| #55 | const completion = await this.openai.chat.completions.create({ |
| #56 | messages: messages.map((msg) => { |
| #57 | const role = msg.role as "system" | "user" | "assistant"; |
| #58 | return { |
| #59 | role, |
| #60 | content: |
| #61 | typeof msg.content === "string" |
| #62 | ? msg.content |
| #63 | : JSON.stringify(msg.content), |
| #64 | }; |
| #65 | }), |
| #66 | model: this.model, |
| #67 | }); |
| #68 | const response = completion.choices[0].message; |
| #69 | return { |
| #70 | content: response.content || "", |
| #71 | role: response.role, |
| #72 | }; |
| #73 | } |
| #74 | } |
| #75 |