Amazing-Python-Scripts

Форк
0

..
/
Chatbot using ChatGPT OpenAI key 
год назад
год назад
год назад
README.md
# ChatGPT Usage Example
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.
## Installation
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:
```bash
pip install -q openai

Usage

Method 1 - Using OpenAI Python Package

import openai
# Set your OpenAI API key
openai.api_key = 'your-api-key'
# Initialize the conversation with a system message
messages = [
{"role": "system", "content": "You are a kind helpful assistant."},
]
while True:
message = input("User : ")
if message:
messages.append(
{"role": "user", "content": message},
)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
print(f"ChatGPT: {reply}")
messages.append({"role": "assistant", "content": reply})

Example conversation:

User : What is your name?
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?
User : Can you call me Shivansh?
ChatGPT: Sure, Shivansh. Is there anything else I can help you with?
User : What is my name?
ChatGPT: Your name is Shivansh.

Method 2 - Using API Endpoint

import requests
# Set your OpenAI API key
api_key = 'your-api-key'
URL = "https://api.openai.com/v1/chat/completions"
payload = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": f"What is the first computer in the world?"}],
"temperature": 1.0,
"top_p": 1.0,
"n": 1,
"stream": False,
"presence_penalty": 0,
"frequency_penalty": 0,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
response = requests.post(URL, headers=headers, json=payload, stream=False)
result = response.json()
assistant_reply = result["choices"][0]["message"]["content"]
print(f"Assistant's reply: {assistant_reply}")

Example response:

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.

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.


Please replace `'your-api-key'` with your actual OpenAI API key before using the code.

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.