update tool docs

This commit is contained in:
zachary62 2025-01-02 16:27:53 +00:00
parent 443168de91
commit 97099a69f9
3 changed files with 77 additions and 18 deletions

View File

@ -23,9 +23,9 @@ We model the LLM workflow as a **Nested Directed Graph**:
</div> </div>
{: .note }
> Have questions? Chat with [AI Assistant](https://chatgpt.com/g/g-677464af36588191b9eba4901946557b-mini-llm-flow-assistant)
> Have questions? Chat with [AI Assistant](https://chatgpt.com/g/g-677464af36588191b9eba4901946557b-mini-llm-flow-assistant)
{: .note }
## Core Abstraction ## Core Abstraction
@ -43,8 +43,9 @@ We model the LLM workflow as a **Nested Directed Graph**:
- [Tool](./tool.md) - [Tool](./tool.md)
{: .warning }
> We do not provide built-in implementation for low-level details. Example implementations are provided as reference. > We do not provide built-in implementation for low-level details. Example implementations are provided as reference.
{: .warning }
## High-Level Paradigm ## High-Level Paradigm

View File

@ -12,10 +12,9 @@ We **don't** provide built-in LLM wrappers. Instead, please implement your own,
```python ```python
def call_llm(prompt): def call_llm(prompt):
from openai import OpenAI from openai import OpenAI
# Set the OpenAI API key (use environment variables, etc.)
client = OpenAI(api_key="YOUR_API_KEY_HERE") client = OpenAI(api_key="YOUR_API_KEY_HERE")
r = client.chat.completions.create( r = client.chat.completions.create(
model="gpt-4", model="gpt-4o",
messages=[{"role": "user", "content": prompt}] messages=[{"role": "user", "content": prompt}]
) )
return r.choices[0].message.content return r.choices[0].message.content
@ -24,6 +23,9 @@ def call_llm(prompt):
call_llm("How are you?") call_llm("How are you?")
``` ```
> Store the API key in an environment variable like OPENAI_API_KEY for security.
{: .note }
## Improvements ## Improvements
Feel free to enhance your `call_llm` function as needed. Here are examples: Feel free to enhance your `call_llm` function as needed. Here are examples:
@ -34,13 +36,13 @@ def call_llm(messages):
from openai import OpenAI from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY_HERE") client = OpenAI(api_key="YOUR_API_KEY_HERE")
r = client.chat.completions.create( r = client.chat.completions.create(
model="gpt-4", model="gpt-4o",
messages=messages messages=messages
) )
return r.choices[0].message.content return r.choices[0].message.content
``` ```
- Add in-memory caching: - Add in-memory caching
```python ```python
from functools import lru_cache from functools import lru_cache
@ -51,6 +53,10 @@ def call_llm(prompt):
pass pass
``` ```
> ⚠️ May overlap with Node retries by caching LLM responses
{: .warning }
- Enable logging: - Enable logging:
```python ```python

View File

@ -16,13 +16,15 @@ Similar to LLM wrappers, we **don't** provide built-in tools. Here, we recommend
```python ```python
def get_embedding(text): def get_embedding(text):
import openai from openai import OpenAI
# Set your API key elsewhere, e.g., environment variables client = OpenAI(api_key="YOUR_API_KEY_HERE")
r = openai.Embedding.create( r = client.embeddings.create(
model="text-embedding-ada-002", model="text-embedding-ada-002",
input=text input=text
) )
return r["data"][0]["embedding"] return r.data[0].embedding
get_embedding("What's the meaning of life?")
``` ```
--- ---
@ -45,6 +47,9 @@ def search_index(index, query_embedding, top_k=5):
top_k top_k
) )
return I, D return I, D
index = create_index(embeddings)
search_index(index, query_embedding)
``` ```
--- ---
@ -64,6 +69,10 @@ def execute_sql(query):
return result return result
``` ```
> ⚠️ Beware of SQL injection risk
{: .warning }
--- ---
## 4. Python Function Execution ## 4. Python Function Execution
@ -73,22 +82,65 @@ def run_code(code_str):
env = {} env = {}
exec(code_str, env) exec(code_str, env)
return env return env
run_code("print('Hello, world!')")
``` ```
> ⚠️ exec() is dangerous with untrusted input
{: .warning }
--- ---
## 5. PDF Extraction ## 5. PDF Extraction
If your PDFs are text-based, use PyMuPDF:
```python ```python
def extract_text_from_pdf(file_path): import fitz # PyMuPDF
import PyPDF2
pdfFileObj = open(file_path, "rb") def extract_text(pdf_path):
reader = PyPDF2.PdfReader(pdfFileObj) doc = fitz.open(pdf_path)
text = "" text = ""
for page in reader.pages: for page in doc:
text += page.extract_text() text += page.get_text()
pdfFileObj.close() doc.close()
return text return text
extract_text("document.pdf")
```
For image-based PDFs (e.g., scanned), OCR is needed. A easy and fast option is using an LLM with vision capabilities:
```python
from openai import OpenAI
import base64
def call_llm_vision(prompt, image_data):
client = OpenAI(api_key="YOUR_API_KEY_HERE")
img_base64 = base64.b64encode(image_data).decode('utf-8')
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{img_base64}"}}
]
}]
)
return response.choices[0].message.content
pdf_document = fitz.open("document.pdf")
page_num = 0
page = pdf_document[page_num]
pix = page.get_pixmap()
img_data = pix.tobytes("png")
call_llm_vision("Extract text from this image", img_data)
``` ```
--- ---