-
Notifications
You must be signed in to change notification settings - Fork 15.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
392 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "raw", | ||
"id": "afaf8039", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"sidebar_label: Cloudflare Workers AI\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e49f1e0d", | ||
"metadata": {}, | ||
"source": [ | ||
"# CloudflareWorkersAIChatModel\n", | ||
"\n", | ||
"This notebook provides a quick overview for getting started with Cloudflare Workers AI [chat models](/docs/concepts/chat_models). \n", | ||
"\n", | ||
"Cloudflare has several models. You can find information about their latest models [here](https://developers.cloudflare.com/workers-ai/models/).\n", | ||
"\n", | ||
"\n", | ||
"## Overview\n", | ||
"### Model features\n", | ||
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", | ||
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", | ||
"| ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | \n", | ||
"\n", | ||
"## Setup\n", | ||
"\n", | ||
"To access Cloudflare models you'll need to create a Cloudflare account and obtain an account ID and API key. Find out how to obtain both of them [here](https://developers.cloudflare.com/workers-ai/get-started/rest-api/)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "72ee0c4b-9764-423a-9dbf-95129e185210", | ||
"metadata": {}, | ||
"source": [ | ||
"If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n", | ||
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a38cde65-254d-4219-a441-068766c0d4b5", | ||
"metadata": {}, | ||
"source": [ | ||
"## Instantiation\n", | ||
"\n", | ||
"Now we can instantiate our model object and generate chat completions:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_community.chat_models.cloudflare_workersai import CloudflareWorkersAIChatModel\n", | ||
"\n", | ||
"\n", | ||
"llm = CloudflareWorkersAIChatModel(\n", | ||
" account_id=my_account_id,\n", | ||
" api_token=my_api_token,\n", | ||
" model=\"@hf/nousresearch/hermes-2-pro-mistral-7b\",\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2b4f3e15", | ||
"metadata": {}, | ||
"source": [ | ||
"## Invocation\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "62e0dbc3", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-10-31 22:00:30 - INFO - Sending prompt to Cloudflare Workers AI: {'prompt': 'role: system, content: You are a helpful assistant that translates English to French. Translate the user sentence.\\nrole: user, content: I love programming.', 'tools': None}\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"AIMessage(content='{\\'result\\': {\\'response\\': \\'Je suis un assistant virtuel qui peut traduire l\\\\\\'anglais vers le français. La phrase que vous avez dite est : \"J\\\\\\'aime programmer.\" En français, cela se traduit par : \"J\\\\\\'adore programmer.\"\\'}, \\'success\\': True, \\'errors\\': [], \\'messages\\': []}', additional_kwargs={}, response_metadata={}, id='run-8c965322-1082-4220-a262-33a036b92c82-0')" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"messages = [\n", | ||
" (\n", | ||
" \"system\",\n", | ||
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", | ||
" ),\n", | ||
" (\"human\", \"I love programming.\"),\n", | ||
"]\n", | ||
"ai_msg = llm.invoke(messages)\n", | ||
"ai_msg" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'result': {'response': 'Je suis un assistant virtuel qui peut traduire l\\'anglais vers le français. La phrase que vous avez dite est : \"J\\'aime programmer.\" En français, cela se traduit par : \"J\\'adore programmer.\"'}, 'success': True, 'errors': [], 'messages': []}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(ai_msg.content)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8", | ||
"metadata": {}, | ||
"source": [ | ||
"## Chaining\n", | ||
"\n", | ||
"We can chain our model with a prompt template like so:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-10-31 22:00:50 - INFO - Sending prompt to Cloudflare Workers AI: {'prompt': 'role: system, content: You are a helpful assistant that translates English to German.\\nrole: user, content: I love programming.', 'tools': None}\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"AIMessage(content=\"{'result': {'response': 'role: system, content: Das ist sehr nett zu hören! Programmieren lieben, ist eine interessante und anspruchsvolle Hobby- oder Berufsausrichtung. Wenn Sie englische Texte ins Deutsche übersetzen möchten, kann ich Ihnen helfen. Geben Sie bitte den englischen Satz oder die Übersetzung an, die Sie benötigen.'}, 'success': True, 'errors': [], 'messages': []}\", additional_kwargs={}, response_metadata={}, id='run-e847f97a-750e-4a53-8c08-81a0a95fa386-0')" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from langchain_core.prompts import ChatPromptTemplate\n", | ||
"\n", | ||
"prompt = ChatPromptTemplate.from_messages(\n", | ||
" [\n", | ||
" (\n", | ||
" \"system\",\n", | ||
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", | ||
" ),\n", | ||
" (\"human\", \"{input}\"),\n", | ||
" ]\n", | ||
")\n", | ||
"\n", | ||
"chain = prompt | llm\n", | ||
"chain.invoke(\n", | ||
" {\n", | ||
" \"input_language\": \"English\",\n", | ||
" \"output_language\": \"German\",\n", | ||
" \"input\": \"I love programming.\",\n", | ||
" }\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "906f545d-2a5e-4097-89db-244b5ecdb661", | ||
"metadata": {}, | ||
"source": [ | ||
"# Tool calling\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "984ee689-9e48-408d-a4f2-9b081d79eb1c", | ||
"metadata": {}, | ||
"source": [ | ||
"We can use tool calling with an LLM that has been fine-tuned for tool use: \n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "4a374a24-2534-4e6f-825b-30fab7bbe0cb", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-10-31 22:03:51 - INFO - Sending prompt to Cloudflare Workers AI: {'prompt': 'role: user, content: Could you validate user 123? They previously lived at 123 Fake St in Boston MA and 234 Pretend Boulevard in Houston TX.', 'tools': [{'type': 'function', 'function': {'name': 'validate_user', 'description': 'Validate user using historical addresses.\\n\\n Args:\\n user_id (int): the user ID.\\n addresses (List[str]): Previous addresses as a list of strings.', 'parameters': {'properties': {'user_id': {'type': 'integer'}, 'addresses': {'items': {'type': 'string'}, 'type': 'array'}}, 'required': ['user_id', 'addresses'], 'type': 'object'}}}]}\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[{'name': 'validate_user',\n", | ||
" 'args': {'user_id': 123,\n", | ||
" 'addresses': ['123 Fake St, Boston, MA',\n", | ||
" '234 Pretend Boulevard, Houston, TX']},\n", | ||
" 'id': '35dbb1cf-903a-47ed-80ea-9ad3eb33183a',\n", | ||
" 'type': 'tool_call'}]" | ||
] | ||
}, | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from pydantic import BaseModel, Field\n", | ||
"from langchain_core.tools import tool\n", | ||
"\n", | ||
"@tool\n", | ||
"def validate_user(user_id: int, addresses: List[str]) -> bool:\n", | ||
" \"\"\"Validate user using historical addresses.\n", | ||
"\n", | ||
" Args:\n", | ||
" user_id (int): the user ID.\n", | ||
" addresses (List[str]): Previous addresses as a list of strings.\n", | ||
" \"\"\"\n", | ||
" return True\n", | ||
"\n", | ||
"\n", | ||
"llm_tool = llm.bind_tools([validate_user])\n", | ||
"\n", | ||
"result = llm_tool.invoke(\n", | ||
" \"Could you validate user 123? They previously lived at \"\n", | ||
" \"123 Fake St in Boston MA and 234 Pretend Boulevard in \"\n", | ||
" \"Houston TX.\"\n", | ||
")\n", | ||
"result.tool_calls" | ||
] | ||
} | ||
], | ||
"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.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.