-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from parea-ai/new-cookbook-example
Fix OpenAI Error on API when API key is not needed. Update cookbooks for OpenAI v >1 Add new cookbook example
- Loading branch information
Showing
9 changed files
with
1,637 additions
and
1,306 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
279 changes: 279 additions & 0 deletions
279
parea/cookbook/tracing_with_function_calling_and_chains.ipynb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"formattedRanges": [], | ||
"cell_id": "d9114afabb54438ea6c7f68a68bf6fb5", | ||
"deepnote_cell_type": "text-cell-h1" | ||
}, | ||
"source": [ | ||
"# Tracing with Function Calls and Chains" | ||
], | ||
"block_group": "53e88a688fb843c381543ca9b45c9d76" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"import os\n", | ||
"from typing import List, Dict, Any, Union\n", | ||
"\n", | ||
"from parea import Parea\n", | ||
"from parea.schemas.log import LLMInputs, Message, ModelParams\n", | ||
"from parea.schemas.models import Completion\n", | ||
"from parea.utils.trace_utils import trace, trace_insert\n", | ||
"\n", | ||
"p = Parea(api_key=os.environ[\"PAREA_API_KEY\"])" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"outputs": [], | ||
"source": [ | ||
"LIMIT = 2 # limit any loops to 2 iterations for demo purposes\n", | ||
"\n", | ||
"COURSE_FUNCTIONS = [\n", | ||
" {\n", | ||
" \"name\": \"generate_course_outline\",\n", | ||
" \"description\": \"Generates a course outline\",\n", | ||
" \"parameters\": {\n", | ||
" \"type\": \"object\",\n", | ||
" \"properties\": {\n", | ||
" \"Description\": {\"type\": \"string\", \"description\": \"The description of the course\"},\n", | ||
" \"chapters\": {\n", | ||
" \"type\": \"array\",\n", | ||
" \"items\": {\n", | ||
" \"type\": \"object\",\n", | ||
" \"properties\": {\n", | ||
" \"name\": {\"type\": \"string\", \"description\": \"The title of the chapter\"},\n", | ||
" \"description\": {\"type\": \"string\", \"description\": \"The summary of the chapter\"},\n", | ||
" },\n", | ||
" \"required\": [\"name\", \"description\"],\n", | ||
" },\n", | ||
" \"description\": \"The chapters included in the course\",\n", | ||
" \"minItems\": 1,\n", | ||
" \"maxItems\": LIMIT,\n", | ||
" },\n", | ||
" },\n", | ||
" \"required\": [\"Description\", \"chapters\"],\n", | ||
" },\n", | ||
" }\n", | ||
"]\n", | ||
"\n", | ||
"CHAPTER_FUNCTIONS = [\n", | ||
" {\n", | ||
" \"name\": \"generate_chapter_outline\",\n", | ||
" \"description\": \"Generates a chapter outline\",\n", | ||
" \"parameters\": {\n", | ||
" \"type\": \"object\",\n", | ||
" \"properties\": {\n", | ||
" \"name\": {\"type\": \"string\", \"description\": \"The title of the chapter\"},\n", | ||
" \"description\": {\"type\": \"string\", \"description\": \"The summary of the chapter\"},\n", | ||
" \"sections\": {\n", | ||
" \"type\": \"array\",\n", | ||
" \"items\": {\n", | ||
" \"type\": \"object\",\n", | ||
" \"properties\": {\n", | ||
" \"name\": {\"type\": \"string\", \"description\": \"The title of the section\"},\n", | ||
" \"description\": {\"type\": \"string\", \"description\": \"The summary of the section\"},\n", | ||
" },\n", | ||
" \"required\": [\"name\", \"description\"],\n", | ||
" },\n", | ||
" \"description\": \"The sections included in the chapter\",\n", | ||
" \"minItems\": 1,\n", | ||
" \"maxItems\": LIMIT,\n", | ||
" },\n", | ||
" },\n", | ||
" \"required\": [\"name\", \"description\", \"sections\"],\n", | ||
" },\n", | ||
" }\n", | ||
"]" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"ExecuteTime": { | ||
"end_time": "2023-12-09T19:18:33.559895Z", | ||
"start_time": "2023-12-09T19:18:33.291059Z" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"USER = \"[email protected]\"\n", | ||
"\n", | ||
"\n", | ||
"# Create a reusable call lmm helper function\n", | ||
"# Parea SDK is automatically traced\n", | ||
"def call_llm(messages: List[Dict[str, str]], name: str = \"LLM Call\") -> str:\n", | ||
" return p.completion(\n", | ||
" Completion(\n", | ||
" llm_configuration=LLMInputs(\n", | ||
" model=\"gpt-3.5-turbo-1106\",\n", | ||
" model_params=ModelParams(temp=0.0, max_length=512),\n", | ||
" messages=[Message(**m) for m in messages],\n", | ||
" functions=COURSE_FUNCTIONS + CHAPTER_FUNCTIONS,\n", | ||
" ),\n", | ||
" end_user_identifier=USER,\n", | ||
" trace_name=name,\n", | ||
" )\n", | ||
" ).content\n", | ||
"\n", | ||
"\n", | ||
"# Helper function to get function call arguments if they exists\n", | ||
"def get_function_call_or_content(response: str) -> Union[str, Dict[str, Any]]:\n", | ||
" # Function calls are returned in triple backticks\n", | ||
" parsed_response = response.replace(\"```\", \"\")\n", | ||
" # return function call or content string\n", | ||
" try:\n", | ||
" return json.loads(parsed_response).get(\"arguments\")\n", | ||
" except Exception as e:\n", | ||
" return parsed_response" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"# PROMPTS\n", | ||
"\n", | ||
"\n", | ||
"def get_course(topic: str):\n", | ||
" _course_outline = call_llm(\n", | ||
" messages=[{\"role\": \"user\", \"content\": f\"Generate a course outline on {topic}\"}],\n", | ||
" name=f\"Create {topic} Course Outline\",\n", | ||
" )\n", | ||
" course_outline = get_function_call_or_content(_course_outline)\n", | ||
" print(json.dumps(course_outline, indent=4))\n", | ||
" return course_outline\n", | ||
"\n", | ||
"\n", | ||
"# I want to group all llm calls into one trace,\n", | ||
"# so I add the trace decorator to create a parent trace,\n", | ||
"# each call_llm will be a child span\n", | ||
"@trace(name=\"Get Chapters\")\n", | ||
"def get_chapters(course_outline: Dict[str, str]):\n", | ||
" chapter_outlines = course_outline.get(\"chapters\", [])\n", | ||
" chapters = [\n", | ||
" get_function_call_or_content(\n", | ||
" call_llm(\n", | ||
" messages=[{\"role\": \"user\", \"content\": f\"Generate a chapter outline on {chapter.get('name')}, with description {chapter.get('description')}\"}],\n", | ||
" name=f\"Create Chapter {idx}\",\n", | ||
" )\n", | ||
" )\n", | ||
" for idx, chapter in enumerate(chapter_outlines, start=1)\n", | ||
" ]\n", | ||
" return chapters[:LIMIT]\n", | ||
"\n", | ||
"\n", | ||
"@trace\n", | ||
"def get_sections(chapter: Dict[str, str]):\n", | ||
" chapter_name = chapter.get(\"name\", \"section\")\n", | ||
" # I want the trace name to be dynamic based on the chapter param,\n", | ||
" # so I can use Parea trace_insert helper method to add dynamic data\n", | ||
" trace_insert({\"trace_name\": f\"Get {chapter_name} Sections\"})\n", | ||
"\n", | ||
" section_outlines = chapter.get(\"sections\", [])\n", | ||
" sections = [\n", | ||
" get_function_call_or_content(\n", | ||
" call_llm(\n", | ||
" messages=[{\"role\": \"user\", \"content\": f\"\"\"Generate a section outline on {section['name']}, with description {section['description']}\"\"\"}],\n", | ||
" name=f\"Create Section {idx}\",\n", | ||
" )\n", | ||
" )\n", | ||
" for idx, section in enumerate(section_outlines, start=1)\n", | ||
" ]\n", | ||
" return sections[:LIMIT]" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"@trace(name=\"Get Sections\")\n", | ||
"def get_all_sections(chapters: List[Dict[str, str]]):\n", | ||
" return [get_sections(chapter) for chapter in chapters]\n", | ||
"\n", | ||
"\n", | ||
"@trace\n", | ||
"def run_creation(topic: str):\n", | ||
" trace_insert({\"trace_name\": f\"Course on {topic}\"})\n", | ||
" course_outline = get_course(topic)\n", | ||
" chapters = get_chapters(course_outline)\n", | ||
" sections = get_all_sections(chapters)\n", | ||
" return sections\n", | ||
"\n", | ||
"\n", | ||
"@trace(name=\"Generate Courses for Topics\")\n", | ||
"def main(topics):\n", | ||
" for topic in topics:\n", | ||
" print(f\"\\n New Topic: {topic} \\n\")\n", | ||
" run_creation(topic)" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"main([\"Artificial Intelligence\", \"Machine Learning\", \"Deep Learning\"])" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Trace Log View in UI\n", | ||
"\n", | ||
"![TraceView](img/trace_log_view.png)" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
} | ||
], | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"deepnote": {}, | ||
"orig_nbformat": 2, | ||
"deepnote_notebook_id": "27ae7c4c406747b5b5936b15081cdaec", | ||
"deepnote_execution_queue": [], | ||
"kernelspec": { | ||
"name": "python3", | ||
"language": "python", | ||
"display_name": "Python 3 (ipykernel)" | ||
} | ||
} | ||
} |
Oops, something went wrong.