Created
July 30, 2025 03:14
-
-
Save Heilum/2cd0c84590e149ee4bb0c6d8feb96d65 to your computer and use it in GitHub Desktop.
Integrate genkit with kimi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load environment variables from .env.local first | |
import dotenv from 'dotenv'; | |
dotenv.config({ path: '.env.local' }); | |
import { openAI } from '@genkit-ai/compat-oai/openai'; | |
import { genkit, z } from 'genkit'; | |
import express from 'express'; | |
// Set environment variables for Moonshot API | |
// console.log('MOONSHOT_API_KEY loaded:', process.env.MOONSHOT_API_KEY ? 'YES ✅' : 'NO ❌'); | |
process.env.OPENAI_API_KEY = process.env.MOONSHOT_API_KEY; | |
process.env.OPENAI_BASE_URL = 'https://api.moonshot.cn/v1'; | |
// Initialize Genkit with OpenAI plugin configured for Moonshot's Kimi API | |
const ai = genkit({ | |
plugins: [ | |
openAI(), | |
], | |
// Available Kimi models: | |
// moonshot-v1-8k (8K context) | |
// moonshot-v1-32k (32K context) | |
// moonshot-v1-128k (128K context) | |
model: openAI.model('moonshot-v1-32k', { | |
temperature: 0.8, | |
}), | |
}); | |
// Define input schema | |
const RecipeInputSchema = z.object({ | |
ingredient: z.string().describe('Main ingredient or cuisine type'), | |
dietaryRestrictions: z.string().optional().describe('Any dietary restrictions'), | |
}); | |
// Define output schema | |
const RecipeSchema = z.object({ | |
title: z.string(), | |
description: z.string(), | |
prepTime: z.string(), | |
cookTime: z.string(), | |
servings: z.number(), | |
ingredients: z.array(z.string()), | |
instructions: z.array(z.string()), | |
tips: z.array(z.string()).optional(), | |
}); | |
// Define a recipe generator flow | |
export const recipeGeneratorFlow = ai.defineFlow( | |
{ | |
name: 'recipeGeneratorFlow', | |
inputSchema: RecipeInputSchema, | |
outputSchema: RecipeSchema, | |
}, | |
async (input) => { | |
// Create a detailed prompt that specifies the exact JSON format needed | |
const prompt = `创建一个食谱,要求如下: | |
主要成分: ${input.ingredient} | |
饮食限制: ${input.dietaryRestrictions || 'none'} | |
返回一个JSON对象,要求如下: | |
- title: 食谱名称 | |
- description: 简要描述 | |
- prepTime: 准备时间 (例如: "15 分钟") | |
- cookTime: 烹饪时间 (例如: "30 分钟") | |
- servings: 份数 (例如: 2) | |
- ingredients: 食材 (例如: ["1 个牛油果", "2 杯面粉"]) | |
- instructions: 步骤 (例如: ["1. 将牛油果切成小块", "2. 将面粉倒入碗中"]) | |
- tips: 提示 (例如: ["1. 将牛油果切成小块", "2. 将面粉倒入碗中"]) | |
确保包含"description"字段,并使用"prepTime"/"cookTime" (不是"preparation_time"/"cooking_time")。`; | |
// Generate structured recipe data using the same schema | |
const { output } = await ai.generate({ | |
prompt, | |
output: { schema: RecipeSchema }, | |
}); | |
if (!output) throw new Error('Failed to generate recipe'); | |
return output; | |
} | |
); | |
// Run the flow | |
async function main() { | |
const recipe = await recipeGeneratorFlow({ | |
ingredient: '牛油果', | |
dietaryRestrictions: '素食者' | |
}); | |
console.log(recipe); | |
} | |
// Commented out for now to avoid API calls during development | |
// main().catch(console.error); | |
//console.log('Genkit app started successfully! Recipe generator flow is ready.'); | |
const app = express(); | |
app.use(express.json()); | |
app.post('/api/recipe', async (req, res) => { | |
try { | |
const { ingredient, dietaryRestrictions } = req.body; | |
const result = await recipeGeneratorFlow({ ingredient, dietaryRestrictions }); | |
res.json(result); | |
} catch (err: any) { | |
res.status(500).json({ error: err.message }); | |
} | |
}); | |
const PORT = 3000; | |
app.listen(PORT, () => { | |
console.log(`API server running on http://localhost:${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment