Conversational AI Design and Practice
GitHubAuthor
  • Preface
    • Syllabus
    • Schedule
  • 0. Getting Started
    • 0.1. Environment Setup
    • 0.2. Quiz
  • 1. Exploration
    • 1.1. Overview
    • 1.2. Project Ideas
    • 1.3. Quiz
  • 2. Dialogue Graph
    • 2.1. Emora STDM
    • 2.2. State Transition
    • 2.3. Matching Strategy
    • 2.4. Multi-turn Dialogue
    • 2.5. Quiz
  • 3. Contextual Understanding
    • 3.1. Natex
    • 3.2. Ontology
    • 3.4. Regular Expression
    • 3.5. Macro
    • 3.5. Quiz
  • 4. Interaction Design
    • 4.1. State Referencing
    • 4.2. Advanced Interaction
    • 4.3. Compound States
    • 4.4. Global Transition
    • 4.5. Saving and Loading
    • 4.6. Quiz
  • 5. LM-based Matching
    • 5.1. Language Models
    • 5.2. Quickstart with GPT
    • 5.3. Information Extraction
    • 5.4. Quiz
  • 6. Conversational Analysis
    • 6.1. H2H vs. H2M
    • 6.2. Team Evaluation
    • 6.3. Quiz
  • Project
    • Projects
    • Proposal Guidelines
    • Final Report Guidelines
  • Supplements
    • LINC Course
    • Page 1
Powered by GitBook

©2023 Emory University - All rights reserved

On this page
  • API Key
  • Using GPT API

Was this helpful?

Export as PDF
  1. 5. LM-based Matching

5.2. Quickstart with GPT

Previous5.1. Language ModelsNext5.3. Information Extraction

Last updated 2 years ago

Was this helpful?

API Key

Create an account for and log in to your account.

Click your icon on the top-right corner and select "View API keys":

Click the "+ Create new secret key" button and copy the API key:

Make sure to save this key in a local file. If you close the dialog without saving, you cannot retrieve the key again, in which case, you have to create a new one.

.idea/
venv/
/resources/openai_api.txt

Do not share this key with anyone or push it to any remote repository (including your private GitHub repository).

Using GPT API

Open the terminal in PyCharm and install the OpenAI package:

(venv) $ pip install openai

Create a function called api_key() as follow:

import openai

PATH_API_KEY = 'resources/openai_api.txt'
openai.api_key_path = PATH_API_KEY
  • #4: specifies the path of the file containing the OpenAI API key.

model = 'gpt-3.5-turbo'
content = 'Say something inspiring'
response = openai.ChatCompletion.create(
    model=model,
    messages=[{'role': 'user', 'content': content}]
)
  • #2: the content to be sent to the GPT model.

  • #3: creates the chat completion model and retrieves the response.

  • #5: messages are stored in a list of dictionaries where each dictionary contains content from either the user or the system.

Print the type of the response and the response itself that is in the JSON format:

<class 'openai.openai_object.OpenAIObject'>
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\n\"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle.\"",
        "role": "assistant"
      }
    }
  ],
  "created": 1678893623,
  "id": "chatcmpl-6uNDL6Qfh7MjxpLxH3NW7UPJXS3tN",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 26,
    "prompt_tokens": 10,
    "total_tokens": 36
  }
}
  • #1: the response type.

  • #2: the response in the JSON format.

Print only the content from the output:

output = response['choices'][0]['message']['content'].strip()
print(output)
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle."

Create a file openai_api.txt under the directory and paste the API key to the file such that it contains only one line showing the key.

Add openai_api.txt to the file:

Retrieve a response by creating a module:

#1: the to use.

resources
.gitignore
ChatCompletition
GPT model
OpenAI
OpenAI account menu.
OpenAI API key generator.