Skip to content

Commit

Permalink
Merge pull request #275 from Samagra-Development/gpt4turbo
Browse files Browse the repository at this point in the history
addedgpt4turbo
  • Loading branch information
Gautam-Rajeev authored Nov 9, 2023
2 parents 1b58a99 + 2e33d92 commit edaeeea
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim

WORKDIR /app

#install requirements
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . /app/
EXPOSE 8000
# Set the entrypoint for the container
CMD ["hypercorn", "--bind", "0.0.0.0:8000", "api:app"]

2 changes: 2 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .request import ModelRequest
from .request import Model
20 changes: 20 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from model import Model
from request import ModelRequest
from quart import Quart, request
import aiohttp

#from fastapi import FastAPI, Body
app = Quart(__name__)
#app.client = aiohttp.ClientSession()
#app = FastAPI()

@app.before_serving
async def startup():
app.client = aiohttp.ClientSession()

@app.route('/', methods=['POST'])
async def answer():
data = await request.get_json()
req = ModelRequest(**data)
model = Model(app)
return await model.inference(req)
28 changes: 28 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import openai
import openai_async
from cache import AsyncTTL
from request import ModelRequest
from tenacity import retry, wait_random_exponential, stop_after_attempt

openai.api_key = os.getenv("OPENAI_API_KEY")

class Model:
def __new__(cls, context):
cls.context = context
if not hasattr(cls, 'instance'):
cls.instance = super(Model, cls).__new__(cls)
return cls.instance

@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
async def inference(self, request: ModelRequest):
response = await openai_async.chat_complete(
openai.api_key,
timeout=20000,
payload={
"model": "gpt-4-1106-preview",
"temperature": 0,
"messages": request.prompt,
},
)
return response.json()
10 changes: 10 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json


class ModelRequest():
def __init__(self, prompt):
self.prompt = prompt

def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
7 changes: 7 additions & 0 deletions src/llm/openai/chatgpt4turbo_preview/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
aiohttp==3.8.4
quart
async-cache==1.1.1
requests
openai==0.28
openai_async==0.0.3
tenacity

0 comments on commit edaeeea

Please sign in to comment.