Skip to content

Commit 42b6943

Browse files
authored
Merge pull request #2695 from Shivansh-Jain-github/cont1
Chatbot using OpenAI API
2 parents 034c8d4 + fabfd2c commit 42b6943

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
```markdown
2+
# ChatGPT Usage Example
3+
4+
This repository contains an example of how to use OpenAI's GPT-3.5-turbo model for interactive text-based conversations. In this example, you'll learn how to use both the OpenAI Python Package and the API Endpoint to engage in a conversation with the language model.
5+
6+
## Installation
7+
8+
You'll need to install the OpenAI Python package to use the GPT-3.5-turbo model. You can do this using the following command:
9+
10+
```bash
11+
pip install -q openai
12+
```
13+
14+
## Usage
15+
16+
### Method 1 - Using OpenAI Python Package
17+
18+
```python
19+
import openai
20+
21+
# Set your OpenAI API key
22+
openai.api_key = 'your-api-key'
23+
24+
# Initialize the conversation with a system message
25+
messages = [
26+
{"role": "system", "content": "You are a kind helpful assistant."},
27+
]
28+
29+
while True:
30+
message = input("User : ")
31+
if message:
32+
messages.append(
33+
{"role": "user", "content": message},
34+
)
35+
chat = openai.ChatCompletion.create(
36+
model="gpt-3.5-turbo", messages=messages
37+
)
38+
39+
reply = chat.choices[0].message.content
40+
print(f"ChatGPT: {reply}")
41+
messages.append({"role": "assistant", "content": reply})
42+
```
43+
44+
Example conversation:
45+
46+
```
47+
User : What is your name?
48+
ChatGPT: I am a language model developed by OpenAI, and I don't have a specific name. You can call me OpenAI if you'd like. How can I assist you further?
49+
User : Can you call me Shivansh?
50+
ChatGPT: Sure, Shivansh. Is there anything else I can help you with?
51+
User : What is my name?
52+
ChatGPT: Your name is Shivansh.
53+
```
54+
55+
### Method 2 - Using API Endpoint
56+
57+
```python
58+
import requests
59+
60+
# Set your OpenAI API key
61+
api_key = 'your-api-key'
62+
63+
URL = "https://api.openai.com/v1/chat/completions"
64+
65+
payload = {
66+
"model": "gpt-3.5-turbo",
67+
"messages": [{"role": "user", "content": f"What is the first computer in the world?"}],
68+
"temperature": 1.0,
69+
"top_p": 1.0,
70+
"n": 1,
71+
"stream": False,
72+
"presence_penalty": 0,
73+
"frequency_penalty": 0,
74+
}
75+
76+
headers = {
77+
"Content-Type": "application/json",
78+
"Authorization": f"Bearer {api_key}"
79+
}
80+
81+
response = requests.post(URL, headers=headers, json=payload, stream=False)
82+
result = response.json()
83+
assistant_reply = result["choices"][0]["message"]["content"]
84+
print(f"Assistant's reply: {assistant_reply}")
85+
```
86+
87+
Example response:
88+
89+
```
90+
Assistant's reply: The first computer in the world was the Electronic Numerical Integrator and Computer (ENIAC), created in 1945 at the University of Pennsylvania.
91+
```
92+
93+
Feel free to experiment with the provided code and adapt it to your specific use case. For more information about the OpenAI API and its capabilities, refer to the [official documentation](https://beta.openai.com/docs/).
94+
```
95+
96+
Please replace `'your-api-key'` with your actual OpenAI API key before using the code.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"\n",
10+
"!pip install -q openai "
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"import openai"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 3,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"openai.api_key = 'sk-'"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 4,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"messages = [\n",
38+
" {\"role\": \"system\", \"content\": \"You are a kind helpful assistant.\"},\n",
39+
"]"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"\n",
49+
"while True:\n",
50+
" message = input(\"User : \")\n",
51+
" if message:\n",
52+
" messages.append(\n",
53+
" {\"role\": \"user\", \"content\": message},\n",
54+
" )\n",
55+
" chat = openai.ChatCompletion.create(\n",
56+
" model=\"gpt-3.5-turbo\", messages=messages\n",
57+
" )\n",
58+
" \n",
59+
" reply = chat.choices[0].message.content\n",
60+
" print(f\"ChatGPT: {reply}\")\n",
61+
" messages.append({\"role\": \"assistant\", \"content\": reply})"
62+
]
63+
}
64+
],
65+
"metadata": {
66+
"kernelspec": {
67+
"display_name": "Python 3",
68+
"language": "python",
69+
"name": "python3"
70+
},
71+
"language_info": {
72+
"codemirror_mode": {
73+
"name": "ipython",
74+
"version": 3
75+
},
76+
"file_extension": ".py",
77+
"mimetype": "text/x-python",
78+
"name": "python",
79+
"nbconvert_exporter": "python",
80+
"pygments_lexer": "ipython3",
81+
"version": "3.11.4"
82+
},
83+
"orig_nbformat": 4
84+
},
85+
"nbformat": 4,
86+
"nbformat_minor": 2
87+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import requests\n",
10+
"\n",
11+
"URL = \"https://api.openai.com/v1/chat/completions\"\n",
12+
"\n",
13+
"payload = {\n",
14+
"\"model\": \"gpt-3.5-turbo\",\n",
15+
"\"messages\": [{\"role\": \"user\", \"content\": f\"What is the first computer in the world?\"}],\n",
16+
"\"temperature\" : 1.0,\n",
17+
"\"top_p\":1.0,\n",
18+
"\"n\" : 1,\n",
19+
"\"stream\": False,\n",
20+
"\"presence_penalty\":0,\n",
21+
"\"frequency_penalty\":0,\n",
22+
"}\n",
23+
"\n",
24+
"headers = {\n",
25+
"\"Content-Type\": \"application/json\",\n",
26+
"\"Authorization\": f\"Bearer {openai.api_key}\"\n",
27+
"}\n",
28+
"\n",
29+
"response = requests.post(URL, headers=headers, json=payload, stream=False)"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"\n",
39+
"response.content"
40+
]
41+
}
42+
],
43+
"metadata": {
44+
"language_info": {
45+
"name": "python"
46+
},
47+
"orig_nbformat": 4
48+
},
49+
"nbformat": 4,
50+
"nbformat_minor": 2
51+
}

0 commit comments

Comments
 (0)