mirror of
https://github.com/sortedcord/omnia.git
synced 2026-07-22 12:02:49 +05:30
feat: Port ILLMProvider interface and langchain gemini adapter
This commit is contained in:
@@ -1 +1 @@
|
||||
export {};
|
||||
export * from "./llm.js";
|
||||
|
||||
22
packages/llm/src/llm.ts
Normal file
22
packages/llm/src/llm.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export interface LLMRequest<T extends z.ZodTypeAny> {
|
||||
systemPrompt: string;
|
||||
userContext: string;
|
||||
schema: T; // The Zod schema
|
||||
temperature?: number;
|
||||
}
|
||||
|
||||
export interface LLMResponse<T> {
|
||||
success: boolean;
|
||||
data?: T;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface ILLMProvider {
|
||||
providerName: string;
|
||||
// We use Zod to ensure the generic T matches the schema
|
||||
generateStructuredResponse<T extends z.ZodTypeAny>(
|
||||
request: LLMRequest<T>,
|
||||
): Promise<LLMResponse<z.infer<T>>>;
|
||||
}
|
||||
26
packages/llm/src/providers/google-genai.ts
Normal file
26
packages/llm/src/providers/google-genai.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { z } from "zod";
|
||||
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
|
||||
import { ILLMProvider, LLMRequest, LLMResponse } from "../llm.js";
|
||||
|
||||
export class GeminiProvider implements ILLMProvider {
|
||||
providerName = "Gemini";
|
||||
private model: ChatGoogleGenerativeAI;
|
||||
|
||||
constructor(apiKey: string) {
|
||||
this.model = new ChatGoogleGenerativeAI({
|
||||
apiKey,
|
||||
model: "gemini-3-flash",
|
||||
});
|
||||
}
|
||||
|
||||
async generateStructuredResponse<T extends z.ZodTypeAny>(
|
||||
request: LLMRequest<T>,
|
||||
): Promise<LLMResponse<z.infer<T>>> {
|
||||
const structuredModel = this.model.withStructuredOutput(request.schema);
|
||||
const result = await structuredModel.invoke([
|
||||
{ role: "system", content: request.systemPrompt },
|
||||
{ role: "user", content: request.userContext },
|
||||
]);
|
||||
return { success: true, data: result as z.infer<T> };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user