Skip to content

Commit

Permalink
add anyscale notebook (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
TengHu authored Dec 28, 2023
1 parent 325eda8 commit cabc243
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 1 deletion.
220 changes: 220 additions & 0 deletions docs/source/notebooks/cookbooks/anyscale.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fecd752b-871f-4add-a7b0-bef18f98b787",
"metadata": {},
"source": [
"# Tutorial: Orchestrate Function Calling with Mistralai/Mistral-7B-Instruct-v0.1 using ActionWeaver through Anyscale Endpoints\n",
"\n",
"Welcome to this tutorial where we will guide you through using ActionWeaver to orchestrate function calling with the open-source model mistralai/Mistral-7B-Instruct-v0.1 via Anyscale endpoints.\n",
"\n",
"## Prerequisites\n",
"\n",
"Before we start, ensure you have the following:\n",
"\n",
"- An Anyscale account\n",
"- API key for Anyscale\n",
"\n",
"Follow the instructions to set up your account and obtain your API key: [Anyscale Documentation](https://docs.endpoints.anyscale.com/?_gl=1*1t5jz2h*_ga*ODk0Nzg3NTM1LjE3MDE5ODg4NzM.*_ga_T6EXHYG44V*MTcwMjc2NDYyNi43LjEuMTcwMjc2NDY2Ny4xOS4wLjA)\n",
"\n",
"Happy coding!\n",
"\n",
"## Additional Resources\n",
"\n",
"For more details regarding ActionWeaver please visit the [ActionWeaver GitHub repository](https://github.com/TengHu/ActionWeaver).\n",
"\n",
"Explore the repository to understand more about its capabilities, access documentation, and view the cookbook to get started!\n"
]
},
{
"cell_type": "markdown",
"id": "90f19e6d-f7a8-4cea-b10e-2a5d460ed32b",
"metadata": {},
"source": [
"**Step 1**: Patch OpenAI client"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "b63c61a5-dbb4-4e80-9d0c-99369c207a31",
"metadata": {},
"outputs": [],
"source": [
"\n",
"from actionweaver.llms import patch\n",
"from openai import OpenAI\n",
"import os\n",
"\n",
"openai_client = patch(OpenAI(\n",
" base_url = \"https://api.endpoints.anyscale.com/v1\",\n",
" api_key = os.getenv(\"ANYSCALE_API_TOKEN\"),\n",
"))"
]
},
{
"cell_type": "markdown",
"id": "99c6610b-480e-429f-a337-0ef9cb9aeb08",
"metadata": {},
"source": [
"**Step 2**: Define function you want model to invoke"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "26227372-4003-4622-8b2e-e4b6fb41ebd3",
"metadata": {},
"outputs": [],
"source": [
"from actionweaver import action\n",
"\n",
"@action(name=\"GetCurrentTime\")\n",
"def get_current_time() -> str:\n",
" \"\"\"\n",
" Use this for getting the current time in the specified time zone.\n",
" \n",
" :return: A string representing the current time in the specified time zone.\n",
" \"\"\"\n",
" print (\"Getting current time...\")\n",
" import datetime\n",
" current_time = datetime.datetime.now()\n",
" \n",
" return f\"The current time is {current_time}\"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "be43ebed-b074-45db-b689-e8247a8fd6ba",
"metadata": {},
"source": [
"**Step 3**: invoke the chat completion api with mistralai/Mistral-7B-Instruct-v0.1"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "1739728e-8879-461b-b4a1-1d95b6a9eea3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting current time...\n"
]
},
{
"data": {
"text/plain": [
"ChatCompletion(id='mistralai/Mistral-7B-Instruct-v0.1-1r6q1D_zIMOZlpQBCZsHC_pezU96tAjlRM2iEvPiG7E', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=' The current time is 2023-12-28 16:23:40.227544.', role='assistant', function_call=None, tool_calls=None, tool_call_id=None))], created=1703798621, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=42, prompt_tokens=638, total_tokens=680))"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"what time is it now\"}\n",
" ]\n",
"\n",
"\n",
"response = openai_client.chat.completions.create(\n",
" model=\"mistralai/Mistral-7B-Instruct-v0.1\",\n",
" messages=messages,\n",
" stream=False,\n",
" temperature=0.7,\n",
" actions=[get_current_time],\n",
")\n",
"\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "ebd5f35f-0b16-4bc8-a969-b4c2ded04442",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting current time...\n"
]
},
{
"data": {
"text/plain": [
"ChatCompletion(id='mistralai/Mistral-7B-Instruct-v0.1-o2VxZXY00gFNlgN-Jjdy04C1b2h20c11_X_kMwjE0sA', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=' The answer to the question \"What is the current time?\" is \"2023-12-28 16:14:14.601829\".', role='assistant', function_call=None, tool_calls=None, tool_call_id=None))], created=1703798055, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=51, prompt_tokens=660, total_tokens=711))"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_current_time.invoke(openai_client, messages=[{\"role\": \"user\", \"content\": \"what time\"}], model=\"mistralai/Mistral-7B-Instruct-v0.1\", stream=False, force=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9eb92adb-c7de-4e61-b895-6a56fff8c2d9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "159afcf9-6ca6-40b5-90a8-802cd1235589",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cff61db3-7f6d-45a0-8dfd-88437acaa78d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1bbc49c-caa1-4026-80ba-3ba428f51a1a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 2 additions & 1 deletion docs/source/notebooks/cookbooks/cookbook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"\n",
"# Examples\n",
"\n",
"- [ReAct/Planning Agent](planning_agent.ipynb)\n"
"- [ReAct/Planning Agent](planning_agent.ipynb)\n",
"- [Function Calling with Mistralai/Mistral-7B-Instruct-v0.1 through Anyscale Endpoints](anyscale.ipynb)\n"
]
},
{
Expand Down

0 comments on commit cabc243

Please sign in to comment.