{
  "slug": "Open-AI-tutorial-bbd89b9bd242",
  "title": "Open AI tutorial",
  "subtitle": "https://platform.openai.com/api-keys",
  "excerpt": "https://platform.openai.com/api-keys",
  "date": "2024-01-22",
  "tags": [
    "API",
    "Performance",
    "Machine Learning"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/open-ai-tutorial-bbd89b9bd242",
  "hero": "https://cdn-images-1.medium.com/max/800/1*trw0TJQHI8UAOG2zgOV2eA.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Open AI tutorial"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://platform.openai.com/api-keys\" target=\"_blank\" rel=\"noreferrer noopener\">https://platform.openai.com/api-keys</a>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*trw0TJQHI8UAOG2zgOV2eA.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 787,
      "height": 653
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://platform.openai.com/docs/libraries\" target=\"_blank\" rel=\"noreferrer noopener\">https://platform.openai.com/docs/libraries</a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install open AI"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "jupyter labjupyter server list"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "pip install openai"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Colab"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Load env file"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import os\nfrom dotenv\nimport load_dotenvload_dotenv()MY_ENV_VAR = os.getenv('MY_ENV_VAR')"
    },
    {
      "type": "paragraph",
      "html": ".env file"
    },
    {
      "type": "code",
      "lang": "properties",
      "code": "MY_ENV_VAR=\"This is my env var content.\""
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Set openAI key"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "openai.api_key = key"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Model of Chat GPT"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*6QUz4N54_7Hi_wdNsDAN-Q.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 881,
      "height": 626
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*cQmTlzWLMvdqKP5J5djU4Q.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 754,
      "height": 665
    },
    {
      "type": "paragraph",
      "html": "<strong>Famous model is gpt-3.5-turbo</strong>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Error code: 429 - {'error': {'message':\n'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://platform.openai.com/account/limits\" target=\"_blank\" rel=\"noreferrer noopener\">https://platform.openai.com/account/limits</a>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*8KpDXmKHQ-DG3vDE6UsXxQ.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 798,
      "height": 481
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*fRJ2YLqlmROXu5DmnZ-3mg.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 687,
      "height": 325
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*zKchMBMdg_ft5ceKZOTjTw.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 536,
      "height": 228
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*3O6nHPSUCZl4fYZNW4E2Mw.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 782,
      "height": 368
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*wwHqK0-zKt99DQNnL00tdg.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 951,
      "height": 374
    },
    {
      "type": "heading",
      "level": 2,
      "text": "First promp API send to Chat GPT"
    },
    {
      "type": "paragraph",
      "html": "First, increase the balance in Chatgpt."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "pip install openai"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "from openai\nimport OpenAI\nimport oskey = \"sk-Fh ...\"prompt = 'what is the capital of USA?'client = OpenAI(\n    api_key = key)chat_completion = client.chat.completions.create(\n    model=\"gpt-3.5-turbo\",\n    messages=[{\"role\": \"user\", \"content\": prompt}])"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "print(chat_completion)"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "ChatCompletion(id='chatcmpl-9aYk3coc90XEJ5NrPNhFQAZZW0uJe', choices=[Choice(finish_reason='stop',\nindex=0, logprobs=None,\nmessage=ChatCompletionMessage(content='The capital of the United States of America is Washington, D.C.', role='assistant', function_call=None, tool_calls=None))], created=1705392343, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=14, prompt_tokens=14, total_tokens=28))"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "How you can have your own ChatGPT chatbot?"
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/9AXP7tCI9PI?feature=oembed"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "List of AI assistant"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://theresanaiforthat.com/\" target=\"_blank\" rel=\"noreferrer noopener\">https://newsletter.theresanaiforthat.com/</a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Q&A with RAG"
    },
    {
      "type": "paragraph",
      "html": "One of the most powerful applications enabled by LLMs is sophisticated question-answering (Q&amp;A) chatbots. These are applications that can answer questions about specific source information. These applications use a technique known as Retrieval Augmented Generation, or RAG."
    },
    {
      "type": "paragraph",
      "html": "LLMs can reason about wide-ranging topics, but their knowledge is limited to the public data up to a specific point in time that they were trained on. If you want to build AI applications that can reason about private data or data introduced after a model’s cutoff date, you need to augment the knowledge of the model with the specific information it needs. The process of bringing the appropriate information and inserting it into the model prompt is known as Retrieval Augmented Generation (RAG)."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*mIPVdAPuorQleAXMWV5uLA.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 1288,
      "height": 403
    },
    {
      "type": "paragraph",
      "html": "Conversing with LLMs is a great way to demonstrate their capabilities. Adding chat history and external context can exponentially increase the complexity of the conversation. In this example, we’ll show how to use <code>Runnables</code> to construct a conversational QA system that can answer questions, remember previous chats, and utilize external context."
    },
    {
      "type": "paragraph",
      "html": "The first step is to load our context (in this example we’ll use the State Of The Union speech from 2022). This is also a good place to instantiate our retriever, and memory classes."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Python quick start"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "RAG retrieval augmented generation"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.youtube.com/watch?v=tcqEUSNCn8I\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.youtube.com/watch?v=tcqEUSNCn8I</a>"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "prepare your data",
        "load your data into python",
        "make chunk of data",
        "using chroma database to store data",
        "store data in sqlite"
      ]
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*mq4dDs9byQfSYbxjigNuqw.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 1440,
      "height": 508
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Private GPT"
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/XFiof0V3nhA?feature=oembed"
    },
    {
      "type": "paragraph",
      "html": "LLM"
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/zBj4eWZFBzs?feature=oembed"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*5fjFVU4jr_9Qwk-JlkIvdA.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 1457,
      "height": 575
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*qMYAzqEOM1H_jyiP4WPneg.png",
      "alt": "Open AI tutorial",
      "caption": "",
      "width": 1215,
      "height": 513
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/aywZrzNaKjs?feature=oembed"
    },
    {
      "type": "paragraph",
      "html": "Pinecone vector database is <strong>a vector-based database that offers high-performance search and similarity matching</strong>. It can deal with high-dimensional vector data at a higher scale, easy integration, and faster query results."
    },
    {
      "type": "paragraph",
      "html": "pinecone.io"
    },
    {
      "type": "paragraph",
      "html": "Visit us at <a href=\"https://www.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>DataDrivenInvestor.com</em></a>"
    },
    {
      "type": "paragraph",
      "html": "Subscribe to DDIntel <a href=\"https://www.ddintel.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>here</em></a>."
    },
    {
      "type": "paragraph",
      "html": "Have a unique story to share? Submit to DDIntel <a href=\"https://datadriveninvestor.com/ddintelsubmission\" target=\"_blank\" rel=\"noreferrer noopener\"><em>here</em></a>."
    },
    {
      "type": "paragraph",
      "html": "Join our creator ecosystem <a href=\"https://join.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>here</em></a>."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://ddintel.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>DDIntel</em> </a>captures the more notable pieces from our <a href=\"https://www.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>main site</em></a> and our popular <a href=\"https://medium.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>DDI Medium publication</em></a>. Check us out for more insightful work from our community."
    },
    {
      "type": "paragraph",
      "html": "DDI Official Telegram Channel: <a href=\"https://t.me/+tafUp6ecEys4YjQ1\" target=\"_blank\" rel=\"noreferrer noopener\">https://t.me/+tafUp6ecEys4YjQ1</a>"
    },
    {
      "type": "paragraph",
      "html": "Follow us on <a href=\"https://www.linkedin.com/company/data-driven-investor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>LinkedIn</em></a>, <a href=\"https://twitter.com/@DDInvestorHQ\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Twitter</em></a>, <a href=\"https://www.youtube.com/c/datadriveninvestor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>YouTube</em></a>, and <a href=\"https://www.facebook.com/datadriveninvestor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Facebook</em></a>."
    }
  ]
}
