docs: response metadata (#20527)

pull/20534/head
Bagatur 3 months ago committed by GitHub
parent 45d045b2c5
commit 07f23bd4ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -7,7 +7,7 @@
"source": [
"---\n",
"sidebar_position: 0\n",
"title: Quick Start\n",
"title: Quickstart\n",
"---"
]
},
@ -16,7 +16,7 @@
"id": "a1a454a9-f963-417b-8be0-e60317cd328c",
"metadata": {},
"source": [
"# Quick Start\n",
"# Quickstart\n",
"\n",
"Chat models are a variation on language models.\n",
"While chat models use language models under the hood, the interface they use is a bit different.\n",

@ -0,0 +1,354 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6bd1219b-f31c-41b0-95e6-3204ad894ac7",
"metadata": {},
"source": [
"# Response metadata\n",
"\n",
"Many model providers include some metadata in their chat generation responses. This metadata can be accessed via the `AIMessage.response_metadata: Dict` attribute. Depending on the model provider and model configuration, this can contain information like [token counts](/docs/modules/model_io/chat/token_usage_tracking/), [logprobs](/docs/modules/model_io/chat/logprobs/), and more.\n",
"\n",
"Here's what the response metadata looks like for a few different providers:\n",
"\n",
"## OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "161f5898-9976-4a75-943d-03eda1a40a60",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'completion_tokens': 164,\n",
" 'prompt_tokens': 17,\n",
" 'total_tokens': 181},\n",
" 'model_name': 'gpt-4-turbo',\n",
" 'system_fingerprint': 'fp_76f018034d',\n",
" 'finish_reason': 'stop',\n",
" 'logprobs': None}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4-turbo\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "98eab683-df03-44a1-a034-ebbe7c6851b6",
"metadata": {},
"source": [
"## Anthropic"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "61c43496-83b5-4d71-bd60-3e6d46c62a5e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 'msg_01CzQyD7BX8nkhDNfT1QqvEp',\n",
" 'model': 'claude-3-sonnet-20240229',\n",
" 'stop_reason': 'end_turn',\n",
" 'stop_sequence': None,\n",
" 'usage': {'input_tokens': 17, 'output_tokens': 296}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "c1f24f69-18f6-43c1-8b26-3f88ec515259",
"metadata": {},
"source": [
"## Google VertexAI"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "39549336-25f5-4839-9846-f687cd77e59b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'is_blocked': False,\n",
" 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False},\n",
" {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False},\n",
" {'category': 'HARM_CATEGORY_HARASSMENT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False},\n",
" {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False}],\n",
" 'citation_metadata': None,\n",
" 'usage_metadata': {'prompt_token_count': 10,\n",
" 'candidates_token_count': 30,\n",
" 'total_token_count': 40}}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_google_vertexai import ChatVertexAI\n",
"\n",
"llm = ChatVertexAI(model=\"gemini-pro\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "bc4ef8bb-eee3-4266-b530-0af9b3b79fe9",
"metadata": {},
"source": [
"## Bedrock (Anthropic)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1e4ac668-4c6a-48ad-9a6f-7b291477b45d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'model_id': 'anthropic.claude-v2',\n",
" 'usage': {'prompt_tokens': 19, 'completion_tokens': 371, 'total_tokens': 390}}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_community.chat_models import BedrockChat\n",
"\n",
"llm = BedrockChat(model_id=\"anthropic.claude-v2\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "ee040d15-5575-4309-a9e9-aed5a09c78e3",
"metadata": {},
"source": [
"## MistralAI"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "deb41321-52d0-4795-a40c-4a811a13d7b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'prompt_tokens': 19,\n",
" 'total_tokens': 141,\n",
" 'completion_tokens': 122},\n",
" 'model': 'mistral-small',\n",
" 'finish_reason': 'stop'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_mistralai import ChatMistralAI\n",
"\n",
"llm = ChatMistralAI()\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "297c7be4-9505-48ac-96c0-4dc2047cfe7f",
"metadata": {},
"source": [
"## Groq"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "744e14ec-ff50-4642-9893-ff7bdf8927ff",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'completion_time': 0.243,\n",
" 'completion_tokens': 132,\n",
" 'prompt_time': 0.022,\n",
" 'prompt_tokens': 22,\n",
" 'queue_time': None,\n",
" 'total_time': 0.265,\n",
" 'total_tokens': 154},\n",
" 'model_name': 'mixtral-8x7b-32768',\n",
" 'system_fingerprint': 'fp_7b44c65f25',\n",
" 'finish_reason': 'stop',\n",
" 'logprobs': None}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_groq import ChatGroq\n",
"\n",
"llm = ChatGroq()\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "7cdeec00-8a8f-422a-8819-47c646578b65",
"metadata": {},
"source": [
"## TogetherAI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a984118e-a731-4864-bcea-7dc6c6b3d139",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'completion_tokens': 208,\n",
" 'prompt_tokens': 20,\n",
" 'total_tokens': 228},\n",
" 'model_name': 'mistralai/Mixtral-8x7B-Instruct-v0.1',\n",
" 'system_fingerprint': None,\n",
" 'finish_reason': 'eos',\n",
" 'logprobs': None}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(\n",
" base_url=\"https://api.together.xyz/v1\",\n",
" api_key=os.environ[\"TOGETHER_API_KEY\"],\n",
" model=\"mistralai/Mixtral-8x7B-Instruct-v0.1\",\n",
")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "3d5e0614-8dc2-4948-a0b5-dc76c7837a5a",
"metadata": {},
"source": [
"## FireworksAI"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "6ae32a93-26db-41bb-95c2-38ddd5085fbe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'prompt_tokens': 19,\n",
" 'total_tokens': 219,\n",
" 'completion_tokens': 200},\n",
" 'model_name': 'accounts/fireworks/models/mixtral-8x7b-instruct',\n",
" 'system_fingerprint': '',\n",
" 'finish_reason': 'length',\n",
" 'logprobs': None}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_fireworks import ChatFireworks\n",
"\n",
"llm = ChatFireworks(model=\"accounts/fireworks/models/mixtral-8x7b-instruct\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "poetry-venv-2",
"language": "python",
"name": "poetry-venv-2"
},
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -1,5 +1,15 @@
{
"cells": [
{
"cell_type": "raw",
"id": "e9437c8a-d8b7-4bf6-8ff4-54068a5a266c",
"metadata": {},
"source": [
"---\n",
"sidebar_position: 1.5\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "d0df7646-b1e1-4014-a841-6dae9b3c50d9",
@ -66,9 +76,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "poetry-venv",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "poetry-venv"
"name": "python3"
},
"language_info": {
"codemirror_mode": {

@ -7,35 +7,130 @@
"source": [
"# Tracking token usage\n",
"\n",
"This notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API.\n",
"This notebook goes over how to track your token usage for specific calls."
]
},
{
"cell_type": "markdown",
"id": "1a55e87a-3291-4e7f-8e8e-4c69b0854384",
"metadata": {},
"source": [
"## Using AIMessage.response_metadata\n",
"\n",
"Let's first look at an extremely simple example of tracking token usage for a single Chat model call."
"A number of model providers return token usage information as part of the chat generation response. When available, this is included in the [AIMessage.response_metadata](/docs/modules/model_io/chat/response_metadata/). Here's an example with OpenAI:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "467ccdeb-6b62-45e5-816e-167cd24d2586",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'token_usage': {'completion_tokens': 225,\n",
" 'prompt_tokens': 17,\n",
" 'total_tokens': 242},\n",
" 'model_name': 'gpt-4-turbo',\n",
" 'system_fingerprint': 'fp_76f018034d',\n",
" 'finish_reason': 'stop',\n",
" 'logprobs': None}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# !pip install -qU langchain-openai\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4-turbo\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "9d5026e9-3ad4-41e6-9946-9f1a26f4a21f",
"metadata": {},
"source": [
"And here's an example with Anthropic:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "145404f1-e088-4824-b468-236c486a9903",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 'msg_01P61rdHbapEo6h3fjpfpCQT',\n",
" 'model': 'claude-3-sonnet-20240229',\n",
" 'stop_reason': 'end_turn',\n",
" 'stop_sequence': None,\n",
" 'usage': {'input_tokens': 17, 'output_tokens': 306}}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# !pip install -qU langchain-anthropic\n",
"\n",
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\")\n",
"msg = llm.invoke([(\"human\", \"What's the oldest known example of cuneiform\")])\n",
"msg.response_metadata"
]
},
{
"cell_type": "markdown",
"id": "d6845407-af25-4eed-bc3e-50925c6661e0",
"metadata": {},
"source": [
"## Using callbacks\n",
"\n",
"There are also some API-specific callback context managers that allow you to track token usage across multiple calls. It is currently only implemented for the OpenAI API and Bedrock Anthropic API.\n",
"\n",
"### OpenAI\n",
"\n",
"Let's first look at an extremely simple example of tracking token usage for a single Chat model call."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9455db35",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.callbacks import get_openai_callback\n",
"from langchain_openai import ChatOpenAI"
"# !pip install -qU langchain-community wikipedia\n",
"\n",
"from langchain_community.callbacks.manager import get_openai_callback"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"id": "d1c55cc9",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(model=\"gpt-4\")"
"llm = ChatOpenAI(model=\"gpt-4-turbo\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 5,
"id": "31667d54",
"metadata": {},
"outputs": [
@ -43,11 +138,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Tokens Used: 24\n",
"Tokens Used: 26\n",
"\tPrompt Tokens: 11\n",
"\tCompletion Tokens: 13\n",
"\tCompletion Tokens: 15\n",
"Successful Requests: 1\n",
"Total Cost (USD): $0.0011099999999999999\n"
"Total Cost (USD): $0.00056\n"
]
}
],
@ -67,7 +162,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 6,
"id": "e09420f4",
"metadata": {},
"outputs": [
@ -75,7 +170,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"48\n"
"52\n"
]
}
],
@ -96,21 +191,43 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 17,
"id": "5d1125c6",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
"from langchain_openai import OpenAI\n",
"from langchain.agents import AgentExecutor, create_tool_calling_agent, load_tools\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
"agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)"
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\"system\", \"You're a helpful assistant\"),\n",
" (\"human\", \"{input}\"),\n",
" (\"placeholder\", \"{agent_scratchpad}\"),\n",
" ]\n",
")\n",
"tools = load_tools([\"wikipedia\"])\n",
"agent = create_tool_calling_agent(llm, tools, prompt)\n",
"agent_executor = AgentExecutor(\n",
" agent=agent, tools=tools, verbose=True, stream_runnable=False\n",
")"
]
},
{
"cell_type": "markdown",
"id": "9c1ae74d-8300-4041-9ff4-66093ee592b1",
"metadata": {},
"source": [
"```{=mdx}\n",
":::note\n",
"We have to set `stream_runnable=False` for token counting to work. By default the AgentExecutor will stream the underlying agent so that you can get the most granular results when streaming events via AgentExecutor.stream_events. However, OpenAI does not return token counts when streaming model responses, so we need to turn off the underlying streaming.\n",
":::\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 18,
"id": "2f98c536",
"metadata": {},
"outputs": [
@ -122,44 +239,109 @@
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `Search` with `Olivia Wilde's current boyfriend`\n",
"Invoking: `wikipedia` with `Hummingbird`\n",
"\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mPage: Hummingbird\n",
"Summary: Hummingbirds are birds native to the Americas and comprise the biological family Trochilidae. With approximately 366 species and 113 genera, they occur from Alaska to Tierra del Fuego, but most species are found in Central and South America. As of 2024, 21 hummingbird species are listed as endangered or critically endangered, with numerous species declining in population.Hummingbirds have varied specialized characteristics to enable rapid, maneuverable flight: exceptional metabolic capacity, adaptations to high altitude, sensitive visual and communication abilities, and long-distance migration in some species. Among all birds, male hummingbirds have the widest diversity of plumage color, particularly in blues, greens, and purples. Hummingbirds are the smallest mature birds, measuring 7.513 cm (35 in) in length. The smallest is the 5 cm (2.0 in) bee hummingbird, which weighs less than 2.0 g (0.07 oz), and the largest is the 23 cm (9 in) giant hummingbird, weighing 1824 grams (0.630.85 oz). Noted for long beaks, hummingbirds are specialized for feeding on flower nectar, but all species also consume small insects.\n",
"They are known as hummingbirds because of the humming sound created by their beating wings, which flap at high frequencies audible to other birds and humans. They hover at rapid wing-flapping rates, which vary from around 12 beats per second in the largest species to 80 per second in small hummingbirds.\n",
"Hummingbirds have the highest mass-specific metabolic rate of any homeothermic animal. To conserve energy when food is scarce and at night when not foraging, they can enter torpor, a state similar to hibernation, and slow their metabolic rate to 115 of its normal rate. While most hummingbirds do not migrate, the rufous hummingbird has one of the longest migrations among birds, traveling twice per year between Alaska and Mexico, a distance of about 3,900 miles (6,300 km).\n",
"Hummingbirds split from their sister group, the swifts and treeswifts, around 42 million years ago. The oldest known fossil hummingbird is Eurotrochilus, from the Rupelian Stage of Early Oligocene Europe.\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m['Things are looking golden for Olivia Wilde, as the actress has jumped back into the dating pool following her split from Harry Styles — read ...', \"“I did not want service to take place at the home of Olivia's current partner because Otis and Daisy might be present,” Sudeikis wrote in his ...\", \"February 2021: Olivia Wilde praises Harry Styles' modesty. One month after the duo made headlines with their budding romance, Wilde gave her new beau major ...\", 'An insider revealed to People that the new couple had been dating for some time. \"They were in Montecito, California this weekend for a wedding, ...', 'A source told People last year that Wilde and Styles were still friends despite deciding to take a break. \"He\\'s still touring and is now going ...', \"... love life. “He's your typical average Joe.” The source adds, “She's not giving too much away right now and wants to keep the relationship ...\", \"Multiple sources said the two were “taking a break” from dating because of distance and different priorities. “He's still touring and is now ...\", 'Comments. Filed under. celebrity couples · celebrity dating · harry styles · jason sudeikis · olivia wilde ... Now Holds A Darker MeaningNYPost.', '... dating during filming. The 39-year-old did however look very cosy with the comedian, although his relationship status is unknown. Olivia ...']\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `Search` with `Harry Styles current age`\n",
"responded: Olivia Wilde's current boyfriend is Harry Styles. Let me find out his age for you.\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m29 years\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `Calculator` with `29 ^ 0.23`\n",
"\n",
"Page: Bee hummingbird\n",
"Summary: The bee hummingbird, zunzuncito or Helena hummingbird (Mellisuga helenae) is a species of hummingbird, native to the island of Cuba in the Caribbean. It is the smallest known bird. The bee hummingbird feeds on nectar of flowers and bugs found in Cuba.\n",
"\n",
"\u001b[0m\u001b[33;1m\u001b[1;3mAnswer: 2.169459462491557\u001b[0m\u001b[32;1m\u001b[1;3mHarry Styles' current age (29 years) raised to the 0.23 power is approximately 2.17.\u001b[0m\n",
"Page: Hummingbird cake\n",
"Summary: Hummingbird cake is a banana-pineapple spice cake originating in Jamaica and a popular dessert in the southern United States since the 1970s. Ingredients include flour, sugar, salt, vegetable oil, ripe banana, pineapple, cinnamon, pecans, vanilla extract, eggs, and leavening agent. It is often served with cream cheese frosting.\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `wikipedia` with `Fastest bird`\n",
"\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mPage: Fastest animals\n",
"Summary: This is a list of the fastest animals in the world, by types of animal.\n",
"\n",
"\n",
"\n",
"Page: List of birds by flight speed\n",
"Summary: This is a list of the fastest flying birds in the world. A bird's velocity is necessarily variable; a hunting bird will reach much greater speeds while diving to catch prey than when flying horizontally. The bird that can achieve the greatest airspeed is the peregrine falcon, able to exceed 320 km/h (200 mph) in its dives. A close relative of the common swift, the white-throated needletail (Hirundapus caudacutus), is commonly reported as the fastest bird in level flight with a reported top speed of 169 km/h (105 mph). This record remains unconfirmed as the measurement methods have never been published or verified. The record for the fastest confirmed level flight by a bird is 111.5 km/h (69.3 mph) held by the common swift.\n",
"\n",
"Page: Ostrich\n",
"Summary: Ostriches are large flightless birds. They are the heaviest and largest living birds, with adult common ostriches weighing anywhere between 63.5 and 145 kilograms and laying the largest eggs of any living land animal. With the ability to run at 70 km/h (43.5 mph), they are the fastest birds on land. They are farmed worldwide, with significant industries in the Philippines and in Namibia. Ostrich leather is a lucrative commodity, and the large feathers are used as plumes for the decoration of ceremonial headgear. Ostrich eggs have been used by humans for millennia.\n",
"Ostriches are of the genus Struthio in the order Struthioniformes, part of the infra-class Palaeognathae, a diverse group of flightless birds also known as ratites that includes the emus, rheas, cassowaries, kiwis and the extinct elephant birds and moas. There are two living species of ostrich: the common ostrich, native to large areas of sub-Saharan Africa, and the Somali ostrich, native to the Horn of Africa. The common ostrich was historically native to the Arabian Peninsula, and ostriches were present across Asia as far east as China and Mongolia during the Late Pleistocene and possibly into the Holocene.\u001b[0m\u001b[32;1m\u001b[1;3m### Hummingbird's Scientific Name\n",
"The scientific name for the bee hummingbird, which is the smallest known bird and a species of hummingbird, is **Mellisuga helenae**. It is native to Cuba.\n",
"\n",
"### Fastest Bird Species\n",
"The fastest bird in terms of airspeed is the **peregrine falcon**, which can exceed speeds of 320 km/h (200 mph) during its diving flight. In level flight, the fastest confirmed speed is held by the **common swift**, which can fly at 111.5 km/h (69.3 mph).\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"Total Tokens: 1929\n",
"Prompt Tokens: 1799\n",
"Completion Tokens: 130\n",
"Total Cost (USD): $0.06176999999999999\n"
"Total Tokens: 1583\n",
"Prompt Tokens: 1412\n",
"Completion Tokens: 171\n",
"Total Cost (USD): $0.019250000000000003\n"
]
}
],
"source": [
"with get_openai_callback() as cb:\n",
" response = agent.run(\n",
" \"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\"\n",
" response = agent_executor.invoke(\n",
" {\n",
" \"input\": \"What's a hummingbird's scientific name and what's the fastest bird species?\"\n",
" }\n",
" )\n",
" print(f\"Total Tokens: {cb.total_tokens}\")\n",
" print(f\"Prompt Tokens: {cb.prompt_tokens}\")\n",
" print(f\"Completion Tokens: {cb.completion_tokens}\")\n",
" print(f\"Total Cost (USD): ${cb.total_cost}\")"
]
},
{
"cell_type": "markdown",
"id": "ebc9122b-050b-4006-b763-264b0b26d9df",
"metadata": {},
"source": [
"### Bedrock Anthropic\n",
"\n",
"The `get_bedrock_anthropic_callback` works very similarly:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "4a3eced5-2ff7-49a7-a48b-768af8658323",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tokens Used: 79\n",
"\tPrompt Tokens: 26\n",
"\tCompletion Tokens: 53\n",
"Successful Requests: 2\n",
"Total Cost (USD): $0.00148\n"
]
}
],
"source": [
"# !pip install boto3\n",
"from langchain_community.callbacks.manager import get_bedrock_anthropic_callback\n",
"from langchain_community.chat_models import BedrockChat\n",
"\n",
"llm = BedrockChat(model_id=\"anthropic.claude-v2\")\n",
"\n",
"with get_bedrock_anthropic_callback() as cb:\n",
" result = llm.invoke(\"Tell me a joke\")\n",
" result2 = llm.invoke(\"Tell me a joke\")\n",
" print(cb)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "poetry-venv-2",
"language": "python",
"name": "python3"
"name": "poetry-venv-2"
},
"language_info": {
"codemirror_mode": {

Loading…
Cancel
Save