feat: Port ILLMProvider interface and langchain gemini adapter

This commit is contained in:
2026-07-06 08:03:31 +05:30
parent 535ed1142f
commit 3d02e5fc25
3 changed files with 49 additions and 1 deletions

View File

@@ -1 +1 @@
export {};
export * from "./llm.js";

22
packages/llm/src/llm.ts Normal file
View 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>>>;
}

View 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> };
}
}