|
| 1 | +/** |
| 2 | + * Groq LLM Node |
| 3 | + * |
| 4 | + * Generates text responses using Groq's high-speed inference API. |
| 5 | + * Uses OpenAI-compatible API format with custom baseURL. |
| 6 | + */ |
| 7 | + |
| 8 | +import { BaseNode, NodeContext, createEvent, handleLLMError } from "@aituber-flow/sdk"; |
| 9 | +import type { Event } from "@aituber-flow/sdk"; |
| 10 | +import OpenAI from "openai"; |
| 11 | + |
| 12 | +interface PromptSection { |
| 13 | + type: "text" | "input"; |
| 14 | + content: string; |
| 15 | +} |
| 16 | + |
| 17 | +export default class GroqLLMNode extends BaseNode { |
| 18 | + private static readonly DEMO_RESPONSE = |
| 19 | + "これはデモモードの応答です。実際のLLMを使用するにはAPIキーを設定してください。"; |
| 20 | + |
| 21 | + private client: OpenAI | null = null; |
| 22 | + private model: string = "llama-3.3-70b-versatile"; |
| 23 | + private systemPrompt: string = "You are a helpful assistant."; |
| 24 | + private temperature: number = 0.7; |
| 25 | + private maxTokens: number = 1024; |
| 26 | + private promptSections: PromptSection[] | null = null; |
| 27 | + |
| 28 | + async setup(config: Record<string, any>, context: NodeContext): Promise<void> { |
| 29 | + const apiKey = config.apiKey ?? ""; |
| 30 | + this.model = config.model ?? "llama-3.3-70b-versatile"; |
| 31 | + this.systemPrompt = config.systemPrompt ?? "You are a helpful assistant."; |
| 32 | + this.temperature = config.temperature ?? 0.7; |
| 33 | + this.maxTokens = config.maxTokens ?? 1024; |
| 34 | + this.promptSections = config.promptSections ?? null; |
| 35 | + |
| 36 | + if (!apiKey) { |
| 37 | + await context.log( |
| 38 | + "[デモモード] Groq APIキー未設定 - 定型文応答を返します", |
| 39 | + "warning", |
| 40 | + ); |
| 41 | + } else { |
| 42 | + this.client = new OpenAI({ |
| 43 | + apiKey, |
| 44 | + baseURL: "https://api.groq.com/openai/v1", |
| 45 | + }); |
| 46 | + await context.log(`Groq client initialized (model: ${this.model})`); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private buildPromptFromSections(inputs: Record<string, any>): string { |
| 51 | + if (!this.promptSections) { |
| 52 | + return (inputs.prompt as string) ?? ""; |
| 53 | + } |
| 54 | + |
| 55 | + const parts: string[] = []; |
| 56 | + for (const section of this.promptSections) { |
| 57 | + if (section.type === "text") { |
| 58 | + parts.push(section.content); |
| 59 | + } else if (section.type === "input") { |
| 60 | + let inputValue: any = inputs[section.content] ?? ""; |
| 61 | + if (typeof inputValue === "object" && inputValue !== null && !Array.isArray(inputValue)) { |
| 62 | + if ("message" in inputValue) { |
| 63 | + inputValue = inputValue.message; |
| 64 | + } else if ("text" in inputValue) { |
| 65 | + inputValue = inputValue.text; |
| 66 | + } else { |
| 67 | + inputValue = String(inputValue); |
| 68 | + } |
| 69 | + } |
| 70 | + parts.push(inputValue ? String(inputValue) : ""); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return parts.join("\n"); |
| 75 | + } |
| 76 | + |
| 77 | + async execute( |
| 78 | + inputs: Record<string, any>, |
| 79 | + context: NodeContext, |
| 80 | + ): Promise<Record<string, any>> { |
| 81 | + if (!this.client) { |
| 82 | + await context.log("[デモモード] 定型文応答を返します", "info"); |
| 83 | + return { response: GroqLLMNode.DEMO_RESPONSE }; |
| 84 | + } |
| 85 | + |
| 86 | + const prompt = this.promptSections |
| 87 | + ? this.buildPromptFromSections(inputs) |
| 88 | + : ((inputs.prompt as string) ?? ""); |
| 89 | + |
| 90 | + if (!prompt) { |
| 91 | + await context.log("No prompt provided", "warning"); |
| 92 | + return { response: "" }; |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + await context.log(`Calling Groq API (${this.model})...`); |
| 97 | + |
| 98 | + const characterName = context.getCharacterName(); |
| 99 | + const characterPersonality = context.getCharacterPersonality(); |
| 100 | + |
| 101 | + let fullSystemPrompt = this.systemPrompt; |
| 102 | + if (characterPersonality) { |
| 103 | + fullSystemPrompt = `${this.systemPrompt}\n\nYou are ${characterName}. ${characterPersonality}`; |
| 104 | + } |
| 105 | + |
| 106 | + const response = await this.client.chat.completions.create({ |
| 107 | + model: this.model, |
| 108 | + messages: [ |
| 109 | + { role: "system" as const, content: fullSystemPrompt }, |
| 110 | + { role: "user" as const, content: prompt }, |
| 111 | + ], |
| 112 | + temperature: this.temperature, |
| 113 | + max_tokens: this.maxTokens, |
| 114 | + }); |
| 115 | + |
| 116 | + const result = response.choices[0].message.content ?? ""; |
| 117 | + await context.log(`Response received (${result.length} chars)`); |
| 118 | + |
| 119 | + await context.emitEvent( |
| 120 | + createEvent("response.generated", { |
| 121 | + text: result, |
| 122 | + model: this.model, |
| 123 | + }), |
| 124 | + ); |
| 125 | + |
| 126 | + return { response: result }; |
| 127 | + } catch (error: unknown) { |
| 128 | + const result = await handleLLMError(error, "Groq", context); |
| 129 | + return { response: result.response }; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + async teardown(): Promise<void> { |
| 134 | + this.client = null; |
| 135 | + } |
| 136 | +} |
0 commit comments