diff --git a/src/llm/openai/chatgpt4turbo_preview/Dockerfile b/src/llm/openai/chatgpt4turbo_preview/Dockerfile new file mode 100644 index 0000000..7cebb27 --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/Dockerfile @@ -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"] + diff --git a/src/llm/openai/chatgpt4turbo_preview/__init__.py b/src/llm/openai/chatgpt4turbo_preview/__init__.py new file mode 100644 index 0000000..55963ef --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/__init__.py @@ -0,0 +1,2 @@ +from .request import ModelRequest +from .request import Model \ No newline at end of file diff --git a/src/llm/openai/chatgpt4turbo_preview/api.py b/src/llm/openai/chatgpt4turbo_preview/api.py new file mode 100644 index 0000000..12f4a8b --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/api.py @@ -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) diff --git a/src/llm/openai/chatgpt4turbo_preview/model.py b/src/llm/openai/chatgpt4turbo_preview/model.py new file mode 100644 index 0000000..8ac0f7b --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/model.py @@ -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() diff --git a/src/llm/openai/chatgpt4turbo_preview/request.py b/src/llm/openai/chatgpt4turbo_preview/request.py new file mode 100644 index 0000000..9753785 --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/request.py @@ -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) diff --git a/src/llm/openai/chatgpt4turbo_preview/requirements.txt b/src/llm/openai/chatgpt4turbo_preview/requirements.txt new file mode 100644 index 0000000..ef90739 --- /dev/null +++ b/src/llm/openai/chatgpt4turbo_preview/requirements.txt @@ -0,0 +1,7 @@ +aiohttp==3.8.4 +quart +async-cache==1.1.1 +requests +openai==0.28 +openai_async==0.0.3 +tenacity \ No newline at end of file