import { ai } from "../src/ai/ai"; import { z } from "genkit"; const inputSchema = z.object({ userResponse: z.string().default(""), userCustomSystemPrompt: z.string().default(""), argTools: z.array(z.any()), history: z.array( z .object({ content: z.array( z.union([ z.string(), z.object({ text: z.string().optional(), // Text is optional toolRequest: z.object({}).optional(), // ToolRequest is optional }), ]) ), role: z.string(), }) .passthrough() // Allow additional properties ), systemPrompt: z.string().default(""), }); // Define the prompt template as a function const promptFunction = async (input) => { let { userResponse, history, userCustomSystemPrompt, argTools, } = input; if (userCustomSystemPrompt) { } // just to remove the warning not used // Function to generate role-based messages const generateRoleMessage = (role, content: string | object) => { // If content is a string, create a standard message object if (typeof content === "string") { return { role, content: [{ text: content }] }; } // If content is an object, return it as is (assuming it's a valid toolRequest object) return { role, content: [{ text: JSON.stringify(content) }] }; //content: [content] }; }; // Create the history part of the messages const historyMessages = (history || []) .map((entry) => entry.content.map((contentItem) => { if (typeof contentItem === "string") { return generateRoleMessage(entry.role, contentItem); } else if ( contentItem && typeof contentItem === "object" && contentItem.text ) { return generateRoleMessage(entry.role, contentItem.text); } else if ( contentItem && typeof contentItem === "object" && contentItem.toolRequest ) { return generateRoleMessage(entry.role, contentItem.toolRequest); } return null; }) ) .flat() .filter(Boolean); const nextMessages = historyMessages.slice(-10); const formattedHistory = nextMessages .map( (msg, index) => `Message ${index + 1} - ${msg.role}: ${msg.content .map((c) => c.text || "") .join(", ")}` ) .join("\n"); const systemMessages = []; if (formattedHistory != '') { systemMessages.push( generateRoleMessage( "system", `You an expert in everything, an AI assistant created to be helpful, honest, and provide insightful and extremely valuable context aware responses. Your recent chat history:\n${formattedHistory}` ) ); } else { systemMessages.push( generateRoleMessage( "system", `You an expert in everything, an AI assistant created to be helpful, honest, and provide insightful and extremely valuable context aware responses. This is the first interaction with this user.` ) ); } if (userResponse) { systemMessages.push(generateRoleMessage("user", userResponse)); } return { //system: systemMessages, messages: systemMessages, history: {}, tools: argTools, returnToolRequests: true, model: "openai/gpt-4o", }; }; // Define the prompt using definePrompt const nonLearningPrompt = ai.definePrompt( { name: "nonLearningPrompt", input: { schema: inputSchema, }, }, promptFunction ); export default nonLearningPrompt; ==================== function toAnthropicRole(role, toolMessageType) { switch (role) { case "user": return "user"; case "model": return "assistant"; case "tool": return toolMessageType === "tool_use" ? "assistant" : "user"; default: throw new Error(`role ${role} doesn't map to an Anthropic role.`); } } function toGroqRole(role) { switch (role) { case "user": return "user"; case "model": case "tool": return "assistant"; case "system": return "system"; default: throw new Error(`role ${role} doesn't map to a Groq role.`); } } function toMistralRole(role) { switch (role) { case "user": return "user"; case "model": return "assistant"; case "system": return "system"; case "tool": return "assistant"; default: throw new Error(`Role ${role} doesn't map to a Mistral role.`); } } function toOpenAIRole(role) { switch (role) { case "user": return "user"; case "model": return "assistant"; case "system": return "system"; case "tool": return "tool"; default: throw new Error(`role ${role} doesn't map to an OpenAI role.`); } } function toCohereRole(role) { switch (role) { case "user": return "USER"; case "model": return "CHATBOT"; case "system": return "SYSTEM"; case "tool": return "CHATBOT"; default: throw new Error(`role ${role} doesn't map to a Cohere role.`); } } function toGeminiRole( role: MessageData['role'], model?: ModelReference ): string { switch (role) { case 'user': return 'user'; case 'model': return 'model'; case 'system': if (model && SUPPORTED_V15_MODELS[model.name]) { // We should have already pulled out the supported system messages, // anything remaining is unsupported; throw an error. throw new Error( 'system role is only supported for a single message in the first position' ); } else { throw new Error('system role is not supported'); } case 'tool': return 'function'; default: return 'user'; } } Where export const SUPPORTED_V15_MODELS = { 'gemini-1.5-pro': gemini15Pro, 'gemini-1.5-flash': gemini15Flash, 'gemini-1.5-flash-8b': gemini15Flash8b, 'gemini-2.0-flash': gemini20Flash, 'gemini-2.0-pro-exp-02-05': gemini20ProExp0205, };