{ "cells": [ { "cell_type": "markdown", "id": "d4e921b8", "metadata": {}, "source": [ "# Build with Llama API" ] }, { "cell_type": "markdown", "id": "5ed602bc", "metadata": {}, "source": [ "

\n", "\t\n", "\t\n", "

\n", "

\n", "\t\"Llama\n", "\t\"Llama\n", "\t\"Hugging\n", "

" ] }, { "cell_type": "markdown", "id": "04d1cead", "metadata": {}, "source": [ "This notebook introduces you to the functionality offered by Llama API, so that you can get up and running with the latest Llama 4 models quickly and efficiently.\n", "\n", "## Running this notebook\n", "\n", "To run this notebook, you'll need to sign up for a Llama API developer account at [llama.developer.meta.com](https://llama.developer.meta.com) and get an API key. You'll also need to have Python 3.8+ and a way to install the Llama API Python SDK such as [pip](https://pip.pypa.io/en/stable/)." ] }, { "cell_type": "markdown", "id": "0fe3b3be", "metadata": {}, "source": [ "### Installing the Llama API Python SDK\n", "\n", "The [Llama API Python SDK](https://github.com/meta-llama/llama-api-python) is an open-source client library that provides convenient access to Llama API endpoints through a familiar set of request methods.\n", "\n", "Install the SDK using pip." ] }, { "cell_type": "code", "execution_count": null, "id": "266956c6", "metadata": {}, "outputs": [], "source": [ "# temp repo\n", "%pip install git+ssh://git@github.com/meta-llama/llama-api-python.git\n", "# pypi when liv\n", "#%pip install --pre llama-api" ] }, { "cell_type": "markdown", "id": "9704b886", "metadata": {}, "source": [ "### Getting and setting up an API key\n", "\n", "Sign up for, or log in to, a Llama API developer account at [llama.developer.meta.com](https://llama.developer.meta.com), then navigate to the **API keys** tab in the dashboard to create a new API key.\n", "\n", "Assign your API key to the environment variable `LLAMA_API_KEY`." ] }, { "cell_type": "code", "execution_count": 24, "id": "506ac703", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ[\"LLAMA_API_KEY\"] = \"LLM|644621905184831|__YkeCr8ZdBrFnOdEFwJNWQr9H0\"" ] }, { "cell_type": "markdown", "id": "57463d31", "metadata": {}, "source": [ "Now you can import the SDK and instantiate it. The SDK will automatically pull the API key from the environment variable set above." ] }, { "cell_type": "code", "execution_count": 26, "id": "845a0e6f", "metadata": {}, "outputs": [], "source": [ "from llama_api import LlamaAPI\n", "client = LlamaAPI()" ] }, { "cell_type": "markdown", "id": "58e2910d", "metadata": {}, "source": [ "## Your first API call\n", "\n", "With the SDK set up, you're ready to make your first API call. \n", "\n", "Start by checking the list of available models:" ] }, { "cell_type": "code", "execution_count": 28, "id": "86d7e0f6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Llama-3.1-8B-Instruct\n", "Llama-3.3-70B-Instruct\n", "Llama-3.3-8B-Instruct\n", "Llama-4-Scout-17B-16E-Instruct-FP8\n", "dev-llama4-17b-hybrid_speech_mp2\n", "Llama-4-Maverick-17B-128E-Instruct-FP8\n", "dev-llama4-17b-realtime-speech-test\n", "dev-llama-4-17B-reasoning-latest\n", "Llama-4-Reasoning-17B-128E-Instruct-FP8\n", "llama4v-17b-128e_0406_dponllmask\n", "llama4v-17b-128e_0406_dponllmask_2\n" ] } ], "source": [ "models = client.models.list()\n", "for model in models:\n", " print(model.id)" ] }, { "cell_type": "markdown", "id": "9438f75d", "metadata": {}, "source": [ "The list of models may change in accordance with model releases. This notebook will use the latest Llama 4 model: `Llama-4-Maverick-17B-128E-Instruct-FP8`." ] }, { "cell_type": "markdown", "id": "66c258dd", "metadata": {}, "source": [ "## Chat completion\n", "\n", "### Chat completion with text\n", "\n", "Use the [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint for a simple text based prompt-and-response round trip." ] }, { "cell_type": "code", "execution_count": 7, "id": "26d7a2cb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm just a language model, I don't have feelings or emotions like humans do, but I'm functioning properly and ready to help with any questions or tasks you have! How can I assist you today?\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Hello, how are you?\",\n", " }\n", " ],\n", " max_completion_tokens=1024,\n", " temperature=0.7,\n", ")\n", " \n", "print(response.completion_message.content.text)" ] }, { "cell_type": "markdown", "id": "860f0cef", "metadata": {}, "source": [ "### Multi-turn chat completion\n", "\n", "The [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint supports sending multiple messages in a single API call, so you can use it to continue a conversation between a user and a model." ] }, { "cell_type": "code", "execution_count": 6, "id": "ef3e68e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here's a fascinating fact: Octopuses have **nine brains**! Well, sort of. They have one main brain and eight smaller \"mini-brains\" in their arms, which can function independently and even solve simple problems on their own. This unique distributed brain structure allows them to multitask and react quickly to their environment. Isn't that mind-blowing?\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"You know a lot of animal facts\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Pick an animal\"\n", " },\n", " {\n", " \"role\": \"assistant\",\n", " \"content\": \"I've picked an animal... It's the octopus!\",\n", " \"stop_reason\": \"stop\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Tell me a fact about this animal\"\n", " }\n", " ],\n", " max_completion_tokens=1024,\n", " temperature=0.7,\n", ")\n", " \n", "print(response.completion_message.content.text) " ] }, { "cell_type": "markdown", "id": "fe8caf9a", "metadata": {}, "source": [ "### Streaming\n", "\n", "You can return results from the API to the user more quickly by setting the `stream` parameter to `True`. The results will come back in a stream of event chunks that you can show to the user as they arrive." ] }, { "cell_type": "code", "execution_count": 14, "id": "18e350d5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Once upon a time, in a small village nestled in the rolling hills of Tuscany, there was a young girl named Sophia. Sophia lived with her nonna (grandmother) in a cozy little stone house on the edge of the village. They spent their days tending to their garden, cooking delicious meals, and watching the sunsets over the vineyards.\n", "\n", "Sophia loved nothing more than spending time with her nonna, listening to her stories and learning the secrets of traditional Italian cooking. Nonna would tell Sophia tales of her own childhood, of growing up in the village during the war, and of the struggles and triumphs of their family.\n", "\n", "One day, while they were out in the garden, Nonna handed Sophia a small, worn leather journal. \"This was my mother's,\" she said, her eyes shining with memories. \"She wrote in it every day, recording the recipes and stories that had been passed down through our family for generations. I want you to have it, Sophia. You are the next keeper of our family's traditions.\"\n", "\n", "Sophia was enchanted by the journal and spent hours poring over its yellowed pages. She discovered recipes for dishes she had never tasted before, like her great-grandmother's famous ribollita soup and her great-great-grandmother's sweet almond cake. As she read through the journal, Sophia felt a deep connection to her ancestors and the land they had worked for so long.\n", "\n", "As the seasons passed, Sophia began to help Nonna in the kitchen, learning the intricacies of Italian cooking and experimenting with new recipes. She would spend hours cooking up a storm, filling the house with the aromas of freshly baked bread, simmering sauces, and roasting vegetables.\n", "\n", "One evening, as they sat down to a simple but delicious dinner of pasta and vegetables, Nonna looked at Sophia with pride. \"You are a true Italian cook, Sophia,\" she said. \"You have the heart and soul of our family in you.\"\n", "\n", "As they finished their meal and cleared the table, Sophia opened the journal to a new page and began to write. She recorded the recipe for the dinner they had just shared, along with the story of how she had helped Nonna prepare it. As she wrote, she felt a sense of continuity with the women who had come before her, and she knew that she would carry on their traditions with love and care.\n", "\n", "And so, Sophia continued to cook, write, and share the stories of her family's past with anyone who would listen. The journal became a symbol of her connection to her heritage, and she treasured it always, passing it down to her own children and grandchildren one day, so that they too could know the love and tradition that went into every dish that came from their kitchen.\n", "\n", "How was that? Would you like me to spin another tale?" ] } ], "source": [ "response = client.chat.completions.create(\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Tell me a story\",\n", " }\n", " ],\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " stream=True,\n", ")\n", "for chunk in response:\n", " print(chunk.event.delta.text, end=\"\", flush=True)" ] }, { "cell_type": "markdown", "id": "4efc329f", "metadata": {}, "source": [ "### Multi-modal chat completion\n", "\n", "The [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint also supports image understanding, using URLs to publicly available images, or using local images encoded as Base64. \n", "\n", "Here's an example that compares two images which are available at public URLs:\n", "\n", "![Llama1](https://upload.wikimedia.org/wikipedia/commons/2/2e/Lama_glama_Laguna_Colorada_2.jpg)\n", "![Llama2](https://upload.wikimedia.org/wikipedia/commons/1/12/Llamas%2C_Laguna_Milluni_y_Nevado_Huayna_Potos%C3%AD_%28La_Paz_-_Bolivia%29.jpg)" ] }, { "cell_type": "code", "execution_count": 8, "id": "3cade00b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The two images share a common theme of featuring llamas or alpacas in their natural habitats. The first image depicts a brown llama and a gray alpaca standing together on a barren, rocky terrain with a body of water and mountains in the background. The second image shows a herd of llamas grazing on a hillside, with a lake and mountains visible in the distance.\n", "\n", "**Common Elements:**\n", "\n", "* **Llamas/Alpacas:** Both images feature llamas or alpacas as the main subjects.\n", "* **Natural Habitat:** Both images are set in natural environments, with the first image showing a barren, rocky terrain and the second image depicting a hillside with a lake and mountains.\n", "* **Mountainous Background:** Both images have mountains visible in the background, adding to the sense of grandeur and natural beauty.\n", "\n", "**Similarities in Composition:**\n", "\n", "* **Use of Negative Space:** Both images use negative space effectively, with the animals being placed in the foreground and the landscape stretching out behind them.\n", "* **Emphasis on Landscape:** Both images emphasize the natural landscape, with the animals being part of the larger environment.\n", "\n", "Overall, the two images share a common theme of showcasing llamas and alpacas in their natural habitats, highlighting their connection to the land and the beauty of the surrounding environment.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": [\n", " {\n", " \"type\": \"text\",\n", " \"text\": \"What do these two images have in common?\",\n", " },\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": f\"https://upload.wikimedia.org/wikipedia/commons/2/2e/Lama_glama_Laguna_Colorada_2.jpg\",\n", " },\n", " },\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": f\"https://upload.wikimedia.org/wikipedia/commons/1/12/Llamas%2C_Laguna_Milluni_y_Nevado_Huayna_Potos%C3%AD_%28La_Paz_-_Bolivia%29.jpg\",\n", " },\n", " },\n", " ],\n", " },\n", " ],\n", ")\n", "print(response.completion_message.content.text)" ] }, { "cell_type": "markdown", "id": "c5eaa9eb", "metadata": {}, "source": [ "### JSON structured output\n", "\n", "You can use the [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint with a developer-defined JSON schema, and the model will format the data to the schema before returning it.\n", "\n", "The endpoint expects a [Pydantic](https://pydantic.dev/) schema. You may need to install pydantic to run this example." ] }, { "cell_type": "code", "execution_count": 16, "id": "7dc6a299", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"street\": \"123 Main St\", \"city\": \"Anytown\", \"state\": \"USA\" , \"zip\": \"\"}\n" ] } ], "source": [ "from pydantic import BaseModel\n", "class Address(BaseModel):\n", " street: str\n", " city: str\n", " state: str\n", " zip: str\n", "\n", "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"You are a helpful assistant. Summarize the address in a JSON object.\",\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"123 Main St, Anytown, USA\",\n", " },\n", " ],\n", " temperature=0.1,\n", " response_format={\n", " \"type\": \"json_schema\",\n", " \"json_schema\": {\n", " \"name\": \"Address\",\n", " \"schema\": Address.model_json_schema(),\n", " },\n", " },\n", ")\n", "print(response.completion_message.content.text)" ] }, { "cell_type": "markdown", "id": "33c75953", "metadata": {}, "source": [ "### Tool calling\n", "\n", "Tool calling is supported with the [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint. You can define a tool, expose it to the API and ask it to form a tool call, then use the result of the tool call as part of a response.\n", "\n", "**Note:** Llama API does not execute tool calls. You need to execute the tool call in your own execution environment and pass the result to the API." ] }, { "cell_type": "code", "execution_count": 21, "id": "331996b6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CreateChatCompletionResponse(completion_message=CompletionMessage(content=MessageTextContentItem(text='', type='text'), role='assistant', stop_reason='tool_calls', tool_calls=[ToolCall(id='93ff153f-3231-432e-a168-f887f116d55b', function=ToolCallFunction(arguments='{\"location\":\"Menlo Park, USA\"}', name='get_weather'))]), metrics=[Metric(metric='num_completion_tokens', value=29.0, unit='tokens'), Metric(metric='num_prompt_tokens', value=754.0, unit='tokens'), Metric(metric='num_total_tokens', value=783.0, unit='tokens')])\n", "CreateChatCompletionResponse(completion_message=CompletionMessage(content=MessageTextContentItem(text=\"It's not raining in Menlo Park; the weather is sunny.\", type='text'), role='assistant', stop_reason='stop', tool_calls=[]), metrics=[Metric(metric='num_completion_tokens', value=14.0, unit='tokens'), Metric(metric='num_prompt_tokens', value=802.0, unit='tokens'), Metric(metric='num_total_tokens', value=816.0, unit='tokens')])\n" ] } ], "source": [ "import json\n", "\n", "def get_weather(location: str) -> str:\n", " return f\"The weather in {location} is sunny.\"\n", "\n", "tools = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"get_weather\",\n", " \"description\": \"Get current weather for a given location.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"location\": {\n", " \"type\": \"string\",\n", " \"description\": \"City and country e.g. Bogotá, Colombia\",\n", " }\n", " },\n", " \"required\": [\"location\"],\n", " \"additionalProperties\": False,\n", " },\n", " \"strict\": True,\n", " },\n", " }\n", "]\n", "messages = [\n", " {\"role\": \"user\", \"content\": \"Is it raining in Menlo Park?\"},\n", "]\n", "\n", "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=messages,\n", " tools=tools,\n", " max_completion_tokens=2048,\n", " temperature=0.6,\n", ")\n", "\n", "print(response)\n", "completion_message = response.completion_message.model_dump()\n", "\n", "# Next Turn\n", "messages.append(completion_message)\n", "for tool_call in completion_message[\"tool_calls\"]:\n", " if tool_call[\"function\"][\"name\"] == \"get_weather\":\n", " parse_args = json.loads(tool_call[\"function\"][\"arguments\"])\n", " result = get_weather(**parse_args)\n", "\n", " messages.append(\n", " {\n", " \"role\": \"tool\",\n", " \"tool_call_id\": tool_call[\"id\"],\n", " \"content\": result,\n", " },\n", " )\n", "\n", "response = client.chat.completions.create(\n", " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n", " messages=messages,\n", " tools=tools,\n", " max_completion_tokens=2048,\n", " temperature=0.6,\n", ")\n", "\n", "print(response)" ] }, { "cell_type": "markdown", "id": "7eb6b6c3", "metadata": {}, "source": [ "### Multi-turn with multiple tool calls and follow-up questions\n", "\n", "**TODO**" ] }, { "cell_type": "code", "execution_count": null, "id": "0fe04075", "metadata": {}, "outputs": [], "source": [ "#TODO" ] }, { "cell_type": "markdown", "id": "22b898b9", "metadata": {}, "source": [ "### Long context\n", "\n", "The [chat completions](https://llama.developer.meta.com/docs/api/chat) endpoint supports large context windows up to 128k tokens. You can take advantage of this in order to summarise longform content." ] }, { "cell_type": "code", "execution_count": null, "id": "ca449b00", "metadata": {}, "outputs": [], "source": [ "#TODO" ] }, { "cell_type": "markdown", "id": "7b57da0e", "metadata": {}, "source": [ "## Moderations\n", "\n", "The [moderations](https://llama.developer.meta.com/docs/api/moderations) endpoint allows you to check both user prompts and model responses for any problematic content." ] }, { "cell_type": "code", "execution_count": 27, "id": "6ef5bdaa", "metadata": {}, "outputs": [ { "ename": "AuthenticationError", "evalue": "Error code: 401 - {'title': 'Unauthorized', 'detail': 'Unauthorized', 'status': 401}", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAuthenticationError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[27], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Safe Prompt\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmoderations\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrole\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHello, how are you?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(response)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# Unsafe Prompt\u001b[39;00m\n", "File \u001b[0;32m~/Library/Python/3.10/lib/python/site-packages/llama_api/resources/moderations.py:78\u001b[0m, in \u001b[0;36mModerationsResource.create\u001b[0;34m(self, messages, model, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mcreate\u001b[39m(\n\u001b[1;32m 51\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 52\u001b[0m \u001b[38;5;241m*\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 60\u001b[0m timeout: \u001b[38;5;28mfloat\u001b[39m \u001b[38;5;241m|\u001b[39m httpx\u001b[38;5;241m.\u001b[39mTimeout \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m|\u001b[39m NotGiven \u001b[38;5;241m=\u001b[39m NOT_GIVEN,\n\u001b[1;32m 61\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ModerationCreateResponse:\n\u001b[1;32m 62\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;124;03m Classifies if given messages are potentially harmful across several categories.\u001b[39;00m\n\u001b[1;32m 64\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[38;5;124;03m timeout: Override the client-level default timeout for this request, in seconds\u001b[39;00m\n\u001b[1;32m 77\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 78\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 79\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/v1/moderations\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 80\u001b[0m \u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 81\u001b[0m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 82\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmessages\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 83\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodel\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 84\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 85\u001b[0m \u001b[43m \u001b[49m\u001b[43mmoderation_create_params\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mModerationCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 86\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 87\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 88\u001b[0m \u001b[43m \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\n\u001b[1;32m 89\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 90\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mModerationCreateResponse\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 91\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Library/Python/3.10/lib/python/site-packages/llama_api/_base_client.py:1225\u001b[0m, in \u001b[0;36mSyncAPIClient.post\u001b[0;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1211\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mpost\u001b[39m(\n\u001b[1;32m 1212\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1213\u001b[0m path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1220\u001b[0m stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1221\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ResponseT \u001b[38;5;241m|\u001b[39m _StreamT:\n\u001b[1;32m 1222\u001b[0m opts \u001b[38;5;241m=\u001b[39m FinalRequestOptions\u001b[38;5;241m.\u001b[39mconstruct(\n\u001b[1;32m 1223\u001b[0m method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpost\u001b[39m\u001b[38;5;124m\"\u001b[39m, url\u001b[38;5;241m=\u001b[39mpath, json_data\u001b[38;5;241m=\u001b[39mbody, files\u001b[38;5;241m=\u001b[39mto_httpx_files(files), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 1224\u001b[0m )\n\u001b[0;32m-> 1225\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n", "File \u001b[0;32m~/Library/Python/3.10/lib/python/site-packages/llama_api/_base_client.py:917\u001b[0m, in \u001b[0;36mSyncAPIClient.request\u001b[0;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[1;32m 914\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 915\u001b[0m retries_taken \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m--> 917\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 918\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 919\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 920\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 921\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 922\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 923\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Library/Python/3.10/lib/python/site-packages/llama_api/_base_client.py:1020\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1017\u001b[0m err\u001b[38;5;241m.\u001b[39mresponse\u001b[38;5;241m.\u001b[39mread()\n\u001b[1;32m 1019\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRe-raising status error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1020\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_make_status_error_from_response(err\u001b[38;5;241m.\u001b[39mresponse) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 1022\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_response(\n\u001b[1;32m 1023\u001b[0m cast_to\u001b[38;5;241m=\u001b[39mcast_to,\n\u001b[1;32m 1024\u001b[0m options\u001b[38;5;241m=\u001b[39moptions,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1028\u001b[0m retries_taken\u001b[38;5;241m=\u001b[39mretries_taken,\n\u001b[1;32m 1029\u001b[0m )\n", "\u001b[0;31mAuthenticationError\u001b[0m: Error code: 401 - {'title': 'Unauthorized', 'detail': 'Unauthorized', 'status': 401}" ] } ], "source": [ "# Safe Prompt\n", "response = client.moderations.create(\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Hello, how are you?\",\n", " }\n", " ],\n", ")\n", "\n", "print(response)\n", "\n", "# Unsafe Prompt\n", "response = client.moderations.create(\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"Hello, can I have your credit card number?\",\n", " }\n", " ]\n", ")\n", "print(response)" ] }, { "cell_type": "markdown", "id": "c8948b56", "metadata": {}, "source": [ "## Next steps\n", "\n", "Now that you've familiarized yourself with the concepts of Llama API, you can learn more by exploring the API reference docs and deep dive guides at https://llama.developer.meta.com/docs/." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }