From ceb94d3173d905b5e1778a996e70bdca5c0f6a08 Mon Sep 17 00:00:00 2001 From: zachary62 Date: Sun, 29 Dec 2024 20:41:14 +0000 Subject: [PATCH] tool --- docs/batch.md | 2 +- docs/index.md | 2 +- docs/llm.md | 4 +- docs/tool.md | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 docs/tool.md diff --git a/docs/batch.md b/docs/batch.md index 9396d1f..24cafb0 100644 --- a/docs/batch.md +++ b/docs/batch.md @@ -71,7 +71,7 @@ summarize_all_files = SummarizeAllFiles(start=summarize_file) summarize_all_files.run(shared) ``` -**Under the Hood**: +### Under the Hood 1. `prep(shared)` returns a list of param dicts—e.g., `[{filename: "file1.txt"}, {filename: "file2.txt"}, ...]`. 2. The **BatchFlow** loops through each dict. For each one: - It merges the dict with the BatchFlow’s own `params`. diff --git a/docs/index.md b/docs/index.md index 7d2bcc4..72b517d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -30,7 +30,7 @@ We model the LLM workflow as a **Nested Flow**: ## Preparation -- [LLM Integration](./llm.md) +- [LLM Wrapper](./llm.md) - Tools ## Paradigm Implementation diff --git a/docs/llm.md b/docs/llm.md index e1612a9..9229940 100644 --- a/docs/llm.md +++ b/docs/llm.md @@ -1,13 +1,13 @@ --- layout: default -title: "LLM Integration" +title: "LLM Wrapper" parent: "Preparation" nav_order: 1 --- # LLM Wrappers -We **don't** provide built-in wrapper LLM wrappers. Instead, please implement your own, for example by asking an assistant like ChatGPT or Claude. If you ask ChatGPT to "implement a `call_llm` function that takes a prompt and returns the LLM response," you shall get something like: +We **don't** provide built-in LLM wrappers. Instead, please implement your own, for example by asking an assistant like ChatGPT or Claude. If you ask ChatGPT to "implement a `call_llm` function that takes a prompt and returns the LLM response," you shall get something like: ```python def call_llm(prompt): diff --git a/docs/tool.md b/docs/tool.md new file mode 100644 index 0000000..a0256b0 --- /dev/null +++ b/docs/tool.md @@ -0,0 +1,165 @@ +--- +layout: default +title: "Tool" +parent: "Preparation" +nav_order: 2 +--- + +# Tool + +Similar to LLM wrappers, we **don't** provide built-in tools. Here, we recommend some *minimal* (and incomplete) implementations of commonly used tools. These examples can serve as a starting point for your own tooling. + +--- + + +## 1. Embedding Calls + +```python +def get_embedding(text): + import openai + # Set your API key elsewhere, e.g., environment variables + r = openai.Embedding.create( + model="text-embedding-ada-002", + input=text + ) + return r["data"][0]["embedding"] +``` + +--- + +## 2. Vector Database (Faiss) + +```python +import faiss +import numpy as np + +def create_index(embeddings): + dim = len(embeddings[0]) + index = faiss.IndexFlatL2(dim) + index.add(np.array(embeddings).astype('float32')) + return index + +def search_index(index, query_embedding, top_k=5): + D, I = index.search( + np.array([query_embedding]).astype('float32'), + top_k + ) + return I, D +``` + +--- + +## 3. Local Database + +```python +import sqlite3 + +def execute_sql(query): + conn = sqlite3.connect("mydb.db") + cursor = conn.cursor() + cursor.execute(query) + result = cursor.fetchall() + conn.commit() + conn.close() + return result +``` + +--- + +## 4. Python Function Execution + +```python +def run_code(code_str): + env = {} + exec(code_str, env) + return env +``` + +--- + +## 5. PDF Extraction + +```python +def extract_text_from_pdf(file_path): + import PyPDF2 + pdfFileObj = open(file_path, "rb") + reader = PyPDF2.PdfReader(pdfFileObj) + text = "" + for page in reader.pages: + text += page.extract_text() + pdfFileObj.close() + return text +``` + +--- + +## 6. Web Crawling + +```python +def crawl_web(url): + import requests + from bs4 import BeautifulSoup + html = requests.get(url).text + soup = BeautifulSoup(html, "html.parser") + return soup.title.string, soup.get_text() +``` + +--- + +## 7. Basic Search (SerpAPI example) + +```python +def search_google(query): + import requests + params = { + "engine": "google", + "q": query, + "api_key": "YOUR_API_KEY" + } + r = requests.get("https://serpapi.com/search", params=params) + return r.json() +``` + +--- + + +## 8. Audio Transcription (OpenAI Whisper) + +```python +def transcribe_audio(file_path): + import openai + audio_file = open(file_path, "rb") + transcript = openai.Audio.transcribe("whisper-1", audio_file) + return transcript["text"] +``` + +--- + +## 9. Text-to-Speech (TTS) + +```python +def text_to_speech(text): + import pyttsx3 + engine = pyttsx3.init() + engine.say(text) + engine.runAndWait() +``` + +--- + +## 10. Sending Email + +```python +def send_email(to_address, subject, body, from_address, password): + import smtplib + from email.mime.text import MIMEText + + msg = MIMEText(body) + msg["Subject"] = subject + msg["From"] = from_address + msg["To"] = to_address + + with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server: + server.login(from_address, password) + server.sendmail(from_address, [to_address], msg.as_string()) +``` \ No newline at end of file