Files

1 line
9.6 KiB
JSON

{"repo": "burakdobur/pocketflow-ts", "files": {"docs/utility_function/llm.mdc": {"9b89bf81cc34414d72e1f6764e60bb289b17615e": {"t": 1785746717.2083542, "n": 4511, "h": "a4d01952bad4f9c1", "c": "---\ndescription: Guidelines for using PocketFlow, Utility Function, LLM Wrapper\nglobs: \nalwaysApply: false\n---\n# LLM Wrappers\n\nCheck out libraries like [litellm](https://github.com/BerriAI/litellm). \nHere, we provide some minimal example implementations:\n\n1. OpenAI\n ```typescript\n import { Configuration, OpenAIApi } from \"openai\";\n\n const configuration = new Configuration({ apiKey: \"YOUR_API_KEY_HERE\" });\n const openai = new OpenAIApi(configuration);\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n return response.data.choices[0].message?.content || \"\";\n }\n\n // Example usage\n callLLM(\"How are you?\").then(console.log);\n ```\n > Store the API key in an environment variable like `OPENAI_API_KEY` for security.\n {: .best-practice }\n\n2. Claude (Anthropic)\n ```typescript\n import { Client } from \"@anthropic-ai/sdk\";\n\n const client = new Client(\"YOUR_API_KEY_HERE\");\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await client.completions.create({\n model: \"claude-3\",\n max_tokens_to_sample: 3000,\n prompt,\n });\n return response.completion;\n }\n ```\n\n3. Google (Generative AI Studio / PaLM API)\n ```typescript\n import { GenerativeLanguageServiceClient } from \"@google-cloud/generative-language\";\n\n const client = new GenerativeLanguageServiceClient();\n\n async function callLLM(prompt: string): Promise<string> {\n const [response] = await client.generateText({\n model: \"models/text-bison-001\",\n prompt,\n });\n return response.candidates[0].output || \"\";\n }\n ```\n\n4. Azure (Azure OpenAI)\n ```typescript\n import axios from \"axios\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await axios.post(\n \"https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-05-15\",\n { messages: [{ role: \"user\", content: prompt }] },\n { headers: { \"api-key\": \"YOUR_API_KEY_HERE\" } }\n );\n return response.data.choices[0].message.content;\n }\n ```\n\n5. Ollama (Local LLM)\n ```typescript\n import { chat } from \"ollama\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await chat({\n model: \"llama2\",\n messages: [{ role: \"user\", content: prompt }],\n });\n return response.message.content;\n }\n ```\n\n6. DeepSeek\n ```typescript\n import axios from \"axios\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await axios.post(\n \"https://api.deepseek.com/chat/completions\",\n { model: \"deepseek-chat\", messages: [{ role: \"user\", content: prompt }] },\n { headers: { Authorization: \"Bearer YOUR_API_KEY_HERE\" } }\n );\n return response.data.choices[0].message.content;\n }\n ```\n\n\n## Improvements\nFeel free to enhance your `callLLM` function as needed. Here are examples:\n\n- Handle chat history:\n\n```typescript\nasync function callLLM(messages: { role: string; content: string }[]): Promise<string> {\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages,\n });\n return response.data.choices[0].message?.content || \"\";\n}\n```\n\n- Add in-memory caching:\n\n```typescript\nconst cache = new Map<string, string>();\n\nasync function callLLM(prompt: string, useCache: boolean = true): Promise<string> {\n if (useCache && cache.has(prompt)) {\n return cache.get(prompt) || \"\";\n }\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n const result = response.data.choices[0].message?.content || \"\";\n if (useCache) {\n cache.set(prompt, result);\n }\n return result;\n}\n```\n\n> ⚠️ Caching conflicts with Node retries, as retries yield the same result.\n>\n> To address this, you could use cached results only if not retried.\n{: .warning }\n\n- Enable logging:\n\n```typescript\nasync function callLLM(prompt: string): Promise<string> {\n console.log(`Prompt: ${prompt}`);\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n const result = response.data.choices[0].message?.content || \"\";\n console.log(`Response: ${result}`);\n return result;\n}\n```\n\n"}, "3f320841866b16a5fa22703cdb631e530f2570f8": {"t": 1785767231.6914496, "n": 4511, "h": "a4d01952bad4f9c1", "c": "---\ndescription: Guidelines for using PocketFlow, Utility Function, LLM Wrapper\nglobs: \nalwaysApply: false\n---\n# LLM Wrappers\n\nCheck out libraries like [litellm](https://github.com/BerriAI/litellm). \nHere, we provide some minimal example implementations:\n\n1. OpenAI\n ```typescript\n import { Configuration, OpenAIApi } from \"openai\";\n\n const configuration = new Configuration({ apiKey: \"YOUR_API_KEY_HERE\" });\n const openai = new OpenAIApi(configuration);\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n return response.data.choices[0].message?.content || \"\";\n }\n\n // Example usage\n callLLM(\"How are you?\").then(console.log);\n ```\n > Store the API key in an environment variable like `OPENAI_API_KEY` for security.\n {: .best-practice }\n\n2. Claude (Anthropic)\n ```typescript\n import { Client } from \"@anthropic-ai/sdk\";\n\n const client = new Client(\"YOUR_API_KEY_HERE\");\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await client.completions.create({\n model: \"claude-3\",\n max_tokens_to_sample: 3000,\n prompt,\n });\n return response.completion;\n }\n ```\n\n3. Google (Generative AI Studio / PaLM API)\n ```typescript\n import { GenerativeLanguageServiceClient } from \"@google-cloud/generative-language\";\n\n const client = new GenerativeLanguageServiceClient();\n\n async function callLLM(prompt: string): Promise<string> {\n const [response] = await client.generateText({\n model: \"models/text-bison-001\",\n prompt,\n });\n return response.candidates[0].output || \"\";\n }\n ```\n\n4. Azure (Azure OpenAI)\n ```typescript\n import axios from \"axios\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await axios.post(\n \"https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-05-15\",\n { messages: [{ role: \"user\", content: prompt }] },\n { headers: { \"api-key\": \"YOUR_API_KEY_HERE\" } }\n );\n return response.data.choices[0].message.content;\n }\n ```\n\n5. Ollama (Local LLM)\n ```typescript\n import { chat } from \"ollama\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await chat({\n model: \"llama2\",\n messages: [{ role: \"user\", content: prompt }],\n });\n return response.message.content;\n }\n ```\n\n6. DeepSeek\n ```typescript\n import axios from \"axios\";\n\n async function callLLM(prompt: string): Promise<string> {\n const response = await axios.post(\n \"https://api.deepseek.com/chat/completions\",\n { model: \"deepseek-chat\", messages: [{ role: \"user\", content: prompt }] },\n { headers: { Authorization: \"Bearer YOUR_API_KEY_HERE\" } }\n );\n return response.data.choices[0].message.content;\n }\n ```\n\n\n## Improvements\nFeel free to enhance your `callLLM` function as needed. Here are examples:\n\n- Handle chat history:\n\n```typescript\nasync function callLLM(messages: { role: string; content: string }[]): Promise<string> {\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages,\n });\n return response.data.choices[0].message?.content || \"\";\n}\n```\n\n- Add in-memory caching:\n\n```typescript\nconst cache = new Map<string, string>();\n\nasync function callLLM(prompt: string, useCache: boolean = true): Promise<string> {\n if (useCache && cache.has(prompt)) {\n return cache.get(prompt) || \"\";\n }\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n const result = response.data.choices[0].message?.content || \"\";\n if (useCache) {\n cache.set(prompt, result);\n }\n return result;\n}\n```\n\n> ⚠️ Caching conflicts with Node retries, as retries yield the same result.\n>\n> To address this, you could use cached results only if not retried.\n{: .warning }\n\n- Enable logging:\n\n```typescript\nasync function callLLM(prompt: string): Promise<string> {\n console.log(`Prompt: ${prompt}`);\n const response = await openai.createChatCompletion({\n model: \"gpt-4\",\n messages: [{ role: \"user\", content: prompt }],\n });\n const result = response.data.choices[0].message?.content || \"\";\n console.log(`Response: ${result}`);\n return result;\n}\n```\n\n"}}}}