PocketFlow / utility_function
The-Pocket/PocketFlow/.cursor/rules/utility_function/llm.mdc
Guidelines for using PocketFlow, Utility Function, LLM Wrapper
Cursor rule11k starsChanged 15 months ago
- Reads credentials
What's in it
- LLM Wrappers
- Improvements
---
description: Guidelines for using PocketFlow, Utility Function, LLM Wrapper
globs:
alwaysApply: false
---
# LLM Wrappers
Check out libraries like [litellm](https://github.com/BerriAI/litellm).
Here, we provide some minimal example implementations:
1. OpenAI
```python
def call_llm(prompt):
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY_HERE")
r = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
return r.choices[0].message.content
# Example usage
call_llm("How are you?")
```
> Store the API key in an environment variable like OPENAI_API_KEY for security.
{: .best-practice }
2. Claude (Anthropic)
```python
def call_llm(prompt):
from anthropic import Anthropic
client = Anthropic(api_key="YOUR_API_KEY_HERE")
r = client.messages.create(
model="claude-3-7-sonnet-20250219",
max_tokens=3000,
messages=[
{"role": "user", "content": prompt}
]
)
return r.content[0].text
```
3. Google (Generative AI Studio / PaLM API)
```python
def call_llm(prompt):
from google import genai
client = genai.Client(api_key='GEMINI_API_KEY')
response = client.models.generate_content(
model='gemini-2.0-flash-001',
contents=prompt
)
return response.text
```
4. Azure (Azure OpenAI)
```python
def call_llm(prompt):
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint="https://.openai.azure.com/",
api_key="YOUR_API_KEY_HERE",
api_version="2023-05-15"
)
r = client.chat.completions.create(
model="",
messages=[{"role": "user", "content": prompt}]
)
return r.choices[0].message.content
```
5. Ollama (Local LLM)
```python
def call_llm(prompt):
from ollama import chat
response = chat(
model="llama2",
messages=[{"role": "user", "content": prompt}]
)
return response.message.content
```
6. DeepSeek
```python
def call_llm(prompt):
from openai import OpenAI
client = OpenAI(api_key="YOUR_DEEPSEEK_API_KEY", base_url="https://api.deepseek.com")
r = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": prompt}]
)
return r.choices[0].message.content
```
## Improvements
Feel free to enhance your `call_llm` function as needed. Here are examples:
- Handle chat history:
```python
def call_llm(messages):
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY_HERE")
r = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
return r.choices[0].message.content
```
- Add in-memory caching
```python
from functools import lru_cache
@lru_cache(maxsize=1000)
def call_llm(prompt):
# Your implementation here
pass
```
> ⚠️ Caching conflicts with Node retries, as retries yield the same result.
>
> To address this, you could use cached results only if not retried.
{: .warning }
```python
from functools import lru_cache
@lru_cache(maxsize=1000)
def cached_call(prompt):
pass
def call_llm(prompt, use_cache):
if use_cache:
return cached_call(prompt)
# Call the underlying function directly
return cached_call.__wrapped__(prompt)
class SummarizeNode(Node):
def exec(self, text):
return call_llm(f"Summarize: {text}", self.cur_retry==0)
```
- Enable logging:
```python
def call_llm(prompt):
import logging
logging.info(f"Prompt: {prompt}")
response = ... # Your implementation here
logging.info(f"Response: {response}")
return response
```
More agent context in The-Pocket/PocketFlow
19 other files this repository gives its agents.
Cursor rule
- .cursor/rules/core_abstraction/async.mdc
- .cursor/rules/core_abstraction/batch.mdc
- .cursor/rules/core_abstraction/communication.mdc
- .cursor/rules/core_abstraction/flow.mdc
- .cursor/rules/core_abstraction/node.mdc
- .cursor/rules/core_abstraction/parallel.mdc
- .cursor/rules/design_pattern/agent.mdc
- .cursor/rules/design_pattern/mapreduce.mdc
- .cursor/rules/design_pattern/multi_agent.mdc
- .cursor/rules/design_pattern/rag.mdc
- .cursor/rules/design_pattern/structure.mdc
- .cursor/rules/design_pattern/workflow.mdc
- .cursor/rules/guide_for_pocketflow.mdc
- .cursor/rules/utility_function/chunking.mdc
- .cursor/rules/utility_function/embedding.mdc
- .cursor/rules/utility_function/text_to_speech.mdc
- .cursor/rules/utility_function/vector.mdc
- .cursor/rules/utility_function/viz.mdc
- .cursor/rules/utility_function/websearch.mdc
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
Reports can't be read right now.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

