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 { Ollama } from "ollama"; |
| #2 | import { LLM, LLMResponse } from "./base"; |
| #3 | import { LLMConfig, Message } from "../types"; |
| #4 | import { logger } from "../utils/logger"; |
| #5 | |
| #6 | export class OllamaLLM implements LLM { |
| #7 | private ollama: Ollama; |
| #8 | private model: string; |
| #9 | // Using this variable to avoid calling the Ollama server multiple times |
| #10 | private initialized: boolean = false; |
| #11 | |
| #12 | constructor(config: LLMConfig) { |
| #13 | this.ollama = new Ollama({ |
| #14 | host: config.config?.url || "http://localhost:11434", |
| #15 | }); |
| #16 | this.model = config.model || "llama3.1:8b"; |
| #17 | this.ensureModelExists().catch((err) => { |
| #18 | logger.error(`Error ensuring model exists: ${err}`); |
| #19 | }); |
| #20 | } |
| #21 | |
| #22 | async generateResponse( |
| #23 | messages: Message[], |
| #24 | responseFormat?: { type: string }, |
| #25 | tools?: any[], |
| #26 | ): Promise<string | LLMResponse> { |
| #27 | try { |
| #28 | await this.ensureModelExists(); |
| #29 | } catch (err) { |
| #30 | logger.error(`Error ensuring model exists: ${err}`); |
| #31 | } |
| #32 | |
| #33 | const completion = await this.ollama.chat({ |
| #34 | model: this.model, |
| #35 | messages: messages.map((msg) => { |
| #36 | const role = msg.role as "system" | "user" | "assistant"; |
| #37 | return { |
| #38 | role, |
| #39 | content: |
| #40 | typeof msg.content === "string" |
| #41 | ? msg.content |
| #42 | : JSON.stringify(msg.content), |
| #43 | }; |
| #44 | }), |
| #45 | ...(responseFormat?.type === "json_object" && { format: "json" }), |
| #46 | ...(tools && { tools, tool_choice: "auto" }), |
| #47 | }); |
| #48 | |
| #49 | const response = completion.message; |
| #50 | |
| #51 | if (response.tool_calls) { |
| #52 | return { |
| #53 | content: response.content || "", |
| #54 | role: response.role, |
| #55 | toolCalls: response.tool_calls.map((call) => ({ |
| #56 | name: call.function.name, |
| #57 | arguments: JSON.stringify(call.function.arguments), |
| #58 | })), |
| #59 | }; |
| #60 | } |
| #61 | |
| #62 | return response.content || ""; |
| #63 | } |
| #64 | |
| #65 | async generateChat(messages: Message[]): Promise<LLMResponse> { |
| #66 | try { |
| #67 | await this.ensureModelExists(); |
| #68 | } catch (err) { |
| #69 | logger.error(`Error ensuring model exists: ${err}`); |
| #70 | } |
| #71 | |
| #72 | const completion = await this.ollama.chat({ |
| #73 | messages: messages.map((msg) => { |
| #74 | const role = msg.role as "system" | "user" | "assistant"; |
| #75 | return { |
| #76 | role, |
| #77 | content: |
| #78 | typeof msg.content === "string" |
| #79 | ? msg.content |
| #80 | : JSON.stringify(msg.content), |
| #81 | }; |
| #82 | }), |
| #83 | model: this.model, |
| #84 | }); |
| #85 | const response = completion.message; |
| #86 | return { |
| #87 | content: response.content || "", |
| #88 | role: response.role, |
| #89 | }; |
| #90 | } |
| #91 | |
| #92 | private async ensureModelExists(): Promise<boolean> { |
| #93 | if (this.initialized) { |
| #94 | return true; |
| #95 | } |
| #96 | const local_models = await this.ollama.list(); |
| #97 | if (!local_models.models.find((m: any) => m.name === this.model)) { |
| #98 | logger.info(`Pulling model ${this.model}...`); |
| #99 | await this.ollama.pull({ model: this.model }); |
| #100 | } |
| #101 | this.initialized = true; |
| #102 | return true; |
| #103 | } |
| #104 | } |
| #105 |