Skip to content

Commit

Permalink
working code to make requests to openai gpt-3.5-turbo api
Browse files Browse the repository at this point in the history
  • Loading branch information
danasaur committed Mar 17, 2023
1 parent cc0aa12 commit e6896d8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
openai.env
*.json
*.csv
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.8-slim-buster

RUN pip install openai

COPY . /mnt/nlp/

WORKDIR /mnt/nlp/

# CMD python hello.py
# CMD python chatgpt.py
36 changes: 36 additions & 0 deletions chatgpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import json

import requests

import openai

openai.organization = "org-pyHC73IrTntMeKFF1ogHPSb4"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Model.list()


URL = "https://api.openai.com/v1/chat/completions"

payload = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": f"Write 10 haikus about cats"}],
"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 {openai.api_key}"
}

response = requests.post(URL, headers=headers, json=payload, stream=False)

print(response.content)

with open("haikus_10.json", "w+") as f:
json.dump(response.json(), f)
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'
services:
openai:
build:
context: .
env_file:
- openai.env
image: openai
volumes:
- ./:/mnt/nlp
command: tail -F anything
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e6896d8

Please sign in to comment.