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 { AzureOpenAI } from "openai"; |
| #2 | import { LLM, LLMResponse } from "./base"; |
| #3 | import { LLMConfig, Message } from "../types"; |
| #4 | |
| #5 | export class AzureOpenAILLM implements LLM { |
| #6 | private client: AzureOpenAI; |
| #7 | private model: string; |
| #8 | |
| #9 | constructor(config: LLMConfig) { |
| #10 | if (!config.apiKey || !config.modelProperties?.endpoint) { |
| #11 | throw new Error("Azure OpenAI requires both API key and endpoint"); |
| #12 | } |
| #13 | |
| #14 | const { endpoint, ...rest } = config.modelProperties; |
| #15 | |
| #16 | this.client = new AzureOpenAI({ |
| #17 | apiKey: config.apiKey, |
| #18 | endpoint: endpoint as string, |
| #19 | ...rest, |
| #20 | }); |
| #21 | this.model = config.model || "gpt-4"; |
| #22 | } |
| #23 | |
| #24 | async generateResponse( |
| #25 | messages: Message[], |
| #26 | responseFormat?: { type: string }, |
| #27 | tools?: any[], |
| #28 | ): Promise<string | LLMResponse> { |
| #29 | const completion = await this.client.chat.completions.create({ |
| #30 | messages: messages.map((msg) => { |
| #31 | const role = msg.role as "system" | "user" | "assistant"; |
| #32 | return { |
| #33 | role, |
| #34 | content: |
| #35 | typeof msg.content === "string" |
| #36 | ? msg.content |
| #37 | : JSON.stringify(msg.content), |
| #38 | }; |
| #39 | }), |
| #40 | model: this.model, |
| #41 | response_format: responseFormat as { type: "text" | "json_object" }, |
| #42 | ...(tools && { tools, tool_choice: "auto" }), |
| #43 | }); |
| #44 | |
| #45 | const response = completion.choices[0].message; |
| #46 | |
| #47 | if (response.tool_calls) { |
| #48 | return { |
| #49 | content: response.content || "", |
| #50 | role: response.role, |
| #51 | toolCalls: response.tool_calls.map((call) => ({ |
| #52 | name: call.function.name, |
| #53 | arguments: call.function.arguments, |
| #54 | })), |
| #55 | }; |
| #56 | } |
| #57 | |
| #58 | return response.content || ""; |
| #59 | } |
| #60 | |
| #61 | async generateChat(messages: Message[]): Promise<LLMResponse> { |
| #62 | const completion = await this.client.chat.completions.create({ |
| #63 | messages: messages.map((msg) => { |
| #64 | const role = msg.role as "system" | "user" | "assistant"; |
| #65 | return { |
| #66 | role, |
| #67 | content: |
| #68 | typeof msg.content === "string" |
| #69 | ? msg.content |
| #70 | : JSON.stringify(msg.content), |
| #71 | }; |
| #72 | }), |
| #73 | model: this.model, |
| #74 | }); |
| #75 | |
| #76 | const response = completion.choices[0].message; |
| #77 | return { |
| #78 | content: response.content || "", |
| #79 | role: response.role, |
| #80 | }; |
| #81 | } |
| #82 | } |
| #83 |