forked from Niek/chatgpt-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Emil Elgaard
committed
Mar 24, 2023
1 parent
fddde94
commit c96c9f1
Showing
5 changed files
with
1,735 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM python:3.10-slim-buster | ||
WORKDIR /work | ||
|
||
RUN pip install fastapi uvicorn lorem-text | ||
COPY mocked_api/mock_api.py . | ||
COPY mocked_api/models_response.json . | ||
|
||
CMD ["uvicorn", "mock_api:app", "--host", "0.0.0.0", "--port", "5174"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
import re | ||
import time | ||
from lorem_text import lorem | ||
|
||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
app = FastAPI() | ||
|
||
# add CORS middleware to allow requests from any origin | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
# Define a route to handle POST requests | ||
@app.post("/v1/chat/completions") | ||
async def post_data(data: dict): | ||
"""Returns mock responses for testing purposes.""" | ||
|
||
messages = data['messages'] | ||
instructions = messages[-1]['content'] | ||
|
||
delay = 0 | ||
lines = None | ||
answer = 'Default mock answer from mocked API' | ||
|
||
try: | ||
delay = re.findall(r'(?<=d)\d+',instructions)[0] | ||
except: | ||
pass | ||
|
||
try: | ||
lines = re.findall(r'(?<=l)\d+',instructions)[0] | ||
except: | ||
pass | ||
|
||
|
||
if delay: | ||
time.sleep(int(delay)) | ||
|
||
if lines: | ||
answer = "\n".join([lorem.sentence() for _ in range(int(lines))]) | ||
|
||
response = { | ||
"id": 0, | ||
"choices": [{ | ||
"index": 0, | ||
"finish_reason": "stop", | ||
"message": {"content": answer,"role": "assistant"} | ||
}] | ||
} | ||
return response | ||
|
||
|
||
@app.get('/v1/models') | ||
async def list_models(): | ||
"""Returns a list of models to get app to work.""" | ||
with open('/work/models_response.json') as f: | ||
result = json.load(f) | ||
|
||
return result | ||
|
||
|
||
@app.post('/') | ||
async def post_data(data: dict): | ||
"""Basic route for testing the API works""" | ||
result = {"message": "Data received", "data": data} | ||
return result |
Oops, something went wrong.