hunk.code_window:         embedding = bedrock_embedding.get_text_embedding(text="foo bar baz")
           self.bedrock_stubber.deactivate()
           self.bedrock_stubber.assert_no_pending_responses()
           self.assertEqual(embedding, mock_response["embeddings"])
  
hunk.labels: keep keep keep keep replace
hunk.after_edit:         self.assertEqual(embedding, mock_response["embeddings"][0])
hunk.file_path: tests/embeddings/test_bedrock.py
hunk.type: replace
file: {
   "cells": [
    {
     "cell_type": "markdown",
     "id": "9fd54a32",
     "metadata": {},
     "source": [
      "<a href=\"https://colab.research.google.com/github/jerryjliu/llama_index/blob/main/docs/examples/llm/openai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "9e3a8796-edc8-43f2-94ad-fe4fb20d70ed",
     "metadata": {},
     "source": [
      "# OpenAI"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "081d07d2",
     "metadata": {},
     "source": [
      "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "83ea30ee",
     "metadata": {},
     "outputs": [],
     "source": [
      "!pip install llama-index"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "b007403c-6b7a-420c-92f1-4171d05ed9bb",
     "metadata": {},
     "source": [
      "## Basic Usage"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "8ead155e-b8bd-46f9-ab9b-28fc009361dd",
     "metadata": {},
     "source": [
      "#### Call `complete` with a prompt"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "60be18ae-c957-4ac2-a58a-0652e18ee6d6",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "resp = OpenAI().complete(\"Paul Graham is \")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "ac2cbebb-a444-4a46-9d85-b265a3483d68",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "a computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of Y Combinator, a startup accelerator and seed capital firm. Graham has also written several influential essays on startups and entrepreneurship, which have gained a large following in the tech community. He has been involved in the founding and funding of numerous successful startups, including Dropbox, Airbnb, and Reddit. Graham is considered a thought leader in the startup world and has been recognized for his contributions to the tech industry.\n"
       ]
      }
     ],
     "source": [
      "print(resp)"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "14831268-f90f-499d-9d86-925dbc88292b",
     "metadata": {},
     "source": [
      "#### Call `chat` with a list of messages"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "bbe29574-4af1-48d5-9739-f60652b6ce6c",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import ChatMessage, OpenAI\n",
      "\n",
      "messages = [\n",
      "    ChatMessage(\n",
      "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
      "    ),\n",
      "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
      "]\n",
      "resp = OpenAI().chat(messages)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "9cbd550a-0264-4a11-9b2c-a08d8723a5ae",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "assistant: Ahoy there, matey! The name be Captain Crimsonbeard, the most colorful pirate to sail the seven seas!\n"
       ]
      }
     ],
     "source": [
      "print(resp)"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "2ed5e894-4597-4911-a623-591560f72b82",
     "metadata": {},
     "source": [
      "## Streaming"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "4cb7986f-aaed-42e2-abdd-f274f6d4fc59",
     "metadata": {},
     "source": [
      "Using `stream_complete` endpoint"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "d43f17a2-0aeb-464b-a7a7-732ba5e8ef24",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "llm = OpenAI()\n",
      "resp = llm.stream_complete(\"Paul Graham is \")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "0214e911-cf0d-489c-bc48-9bb1d8bf65d8",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "a computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of the startup accelerator Y Combinator. Graham has also written several influential essays on startups and entrepreneurship, which have gained a large following in the tech community. He has been involved in the founding and funding of numerous successful startups, including Reddit, Dropbox, and Airbnb. Graham is known for his insightful and often controversial opinions on various topics, including education, inequality, and the future of technology."
       ]
      }
     ],
     "source": [
      "for r in resp:\n",
      "    print(r.delta, end=\"\")"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "40350dd8-3f50-4a2f-8545-5723942039bb",
     "metadata": {},
     "source": [
      "Using `stream_chat` endpoint"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "bc636e65-a67b-4dcd-ac60-b25abc9d8dbd",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import OpenAI, ChatMessage\n",
      "\n",
      "llm = OpenAI()\n",
      "messages = [\n",
      "    ChatMessage(\n",
      "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
      "    ),\n",
      "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
      "]\n",
      "resp = llm.stream_chat(messages)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "4475a6bc-1051-4287-abce-ba83324aeb9e",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "Ahoy there, matey! The name be Captain Crimsonbeard, the most colorful pirate to sail the seven seas!"
       ]
      }
     ],
     "source": [
      "for r in resp:\n",
      "    print(r.delta, end=\"\")"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "009d3f1c-ef35-4126-ae82-0b97adb746e3",
     "metadata": {},
     "source": [
      "## Configure Model"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "e973e3d1-a3c9-43b9-bee1-af3e57946ac3",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "llm = OpenAI(model=\"text-davinci-003\")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "e2c9bcf6-c950-4dfc-abdc-598d5bdedf40",
     "metadata": {},
     "outputs": [],
     "source": [
      "resp = llm.complete(\"Paul Graham is \")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "2edc85ca-df17-4774-a3ea-e80109fa1811",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "\n",
        "\n",
        "Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work in the startup world, having co-founded the accelerator Y Combinator and investing in hundreds of startups. He is also a prolific writer, having authored several books on topics such as startups, programming, and technology.\n"
       ]
      }
     ],
     "source": [
      "print(resp)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "026fdb77-b61f-4571-8eaf-4a51e8415458",
     "metadata": {},
     "outputs": [],
     "source": [
      "messages = [\n",
      "    ChatMessage(\n",
      "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
      "    ),\n",
      "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
      "]\n",
      "resp = llm.chat(messages)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "2286a16c-188b-437f-a1a3-4efe299b759d",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "assistant: \n",
        "My name is Captain Jack Sparrow.\n"
       ]
      }
     ],
     "source": [
      "print(resp)"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "90f07f7e-927f-47a2-9797-de5a86d61e1f",
     "metadata": {},
     "source": [
      "## Function Calling"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "212bb2d2-2bed-4188-85ad-3cd497d4b864",
     "metadata": {},
     "outputs": [],
     "source": [
      "from pydantic import BaseModel\n",
      "from llama_index.llms.openai_utils import to_openai_tool\n",
      "\n",
      "\n",
      "class Song(BaseModel):\n",
      "    \"\"\"A song with name and artist\"\"\"\n",
      "\n",
      "    name: str\n",
      "    artist: str\n",
      "\n",
      "\n",
      "song_fn = to_openai_tool(Song)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "fdacb943-bab8-442a-a6db-aee935658340",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "[{'id': 'call_cW8rD7s9cm48YSvY3j93hjqC', 'function': {'arguments': '{\\n  \"name\": \"Sunshine\",\\n  \"artist\": \"John Smith\"\\n}', 'name': 'Song'}, 'type': 'function'}]\n"
       ]
      }
     ],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "response = OpenAI().complete(\"Generate a song\", tools=[song_fn])\n",
      "tool_calls = response.additional_kwargs[\"tool_calls\"]\n",
      "print(tool_calls)"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "df5fa1ab-f598-46da-80f3-f6af5dd2fe83",
     "metadata": {},
     "source": [
      "## Async"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "3e088b90-12b6-4211-a9ca-696e9897e9ae",
     "metadata": {},
     "outputs": [],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "llm = OpenAI(model=\"text-davinci-003\")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "8f649683-896a-439e-b14b-e5df5d803b82",
     "metadata": {},
     "outputs": [],
     "source": [
      "resp = await llm.acomplete(\"Paul Graham is \")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "d05d8b7c-07b3-4ce8-9a8c-fcd1e830316c",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "\n",
        "\n",
        "Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work as a co-founder of the Y Combinator startup accelerator, and for his essays on technology and business. He is also the author of several books, including Hackers & Painters and On Lisp.\n"
       ]
      }
     ],
     "source": [
      "print(resp)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "abc72d09-3bcd-4d48-9bb7-0920c56310c1",
     "metadata": {},
     "outputs": [],
     "source": [
      "resp = await llm.astream_complete(\"Paul Graham is \")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "b28d7a06-1518-4ec0-b3cc-6364395b3561",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "\n",
        "\n",
        "Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work in the startup world, having co-founded the accelerator Y Combinator and investing in many successful startups such as Airbnb, Dropbox, and Stripe. He is also a prolific writer, having authored several books on topics such as startups, programming, and technology."
       ]
      }
     ],
     "source": [
      "async for delta in resp:\n",
      "    print(delta.delta, end=\"\")"
     ]
    },
    {
     "cell_type": "markdown",
     "id": "a2782f06",
     "metadata": {},
     "source": [
      "## Set API Key at a per-instance level\n",
      "If desired, you can have separate LLM instances use separate API keys."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "id": "015c2d39",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "a computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of the startup accelerator Y Combinator. Graham has also written several influential essays on startups and entrepreneurship, which have gained a wide following in the tech industry. He has been involved in the founding and funding of numerous successful startups, including Reddit, Dropbox, and Airbnb. Graham is known for his insightful and often controversial opinions on various topics, including education, inequality, and the future of technology.\n"
       ]
      }
     ],
     "source": [
      "from llama_index.llms import OpenAI\n",
      "\n",
      "llm = OpenAI(model=\"text-davinci-003\", api_key=\"BAD_KEY\")\n",
      "resp = OpenAI().complete(\"Paul Graham is \")\n",
      "print(resp)"
     ]
    }
   ],
   "metadata": {
    "colab": {
     "provenance": []
    },
    "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"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 5
  }
  