Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneggz committed Aug 13, 2023
1 parent e571184 commit f439dac
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 31 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: 3.10
cache: 'pip'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt
- name: Run tests
run: |
echo ">> Running all test cases"
python3 -m pytest -s tests
env:
# Set your environment variables here, if they are sensitive, use secrets.
# Example:
# MY_ENV_VAR: ${{ secrets.MY_ENV_VAR }}
Binary file modified server/__pycache__/api.cpython-310.pyc
Binary file not shown.
60 changes: 29 additions & 31 deletions server/api.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from typing import Any, Optional
from pydantic import BaseModel, Field
"App Entrypoint"
import logging
import openai
import os

app = FastAPI()
from fastapi import FastAPI, Request, HTTPException
from fastapi.templating import Jinja2Templates

from server.models.request import Message
from server.models.response import ResponseStatus, ResponseChat

app = FastAPI(title="🤖 Prompt Engineers AI - Serverless Chat")
os.environ.get('OPENAI_API_KEY')
templates = Jinja2Templates(directory="static")
logger = logging.getLogger("uvicorn.error")

#######################################################################
### Pages
#######################################################################
@app.get("/", tags=["Pages"])
@app.get("/", tags=["Pages"], include_in_schema=False)
async def chat_interface(request: Request):
"""Serves the index page."""
return templates.TemplateResponse(
"pages/index.html",
{"request": request, "current_page": "home"}
)

#######################################################################
### Status Endpoints
#######################################################################
@app.get("/status", tags=["Status"], response_model=ResponseStatus)
async def get_application_version():
"""Check the application status."""
try:
return {
"version": os.getenv("APP_VERSION", ''),
}
except Exception as err:
logger.exception(err)
raise HTTPException(status_code=500, detail="Internal Server Error")

#################################################
## ChatGPT
#################################################
class Message(BaseModel): # pylint: disable=too-few-public-methods
"""A message to send to the chatbot."""
model: Optional[str] = None
messages: Optional[Any] = None
temperature: Optional[float or int] = None

class Config: # pylint: disable=too-few-public-methods
"""A message to send to the chatbot."""
json_schema_extra = {
"example": {
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": 'Who won the 2001 world series?'},
{"role": "assistant", "content": 'The arizona diamondbacks won the 2001 world series.'},
{"role": "user", "content": 'Who were the pitchers?'},
]
}
}

@app.post("/chat", tags=["Chat"])
#######################################################################
### API Endpoints
#######################################################################
@app.post("/chat", tags=["Chat"], response_model=ResponseChat)
async def chat_endpoint(body: Message):
try:
result = openai.ChatCompletion.create(
Expand Down
26 changes: 26 additions & 0 deletions server/models/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Any, Optional
from pydantic import BaseModel, Field

#################################################
## ChatGPT
#################################################
class Message(BaseModel): # pylint: disable=too-few-public-methods
"""A message to send to the chatbot."""
model: Optional[str] = None
messages: Optional[Any] = None
temperature: Optional[float or int] = None

class Config: # pylint: disable=too-few-public-methods
"""A message to send to the chatbot."""
json_schema_extra = {
"example": {
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": 'Who won the 2001 world series?'},
{"role": "assistant", "content": 'The arizona diamondbacks won the 2001 world series.'},
{"role": "user", "content": 'Who were the pitchers?'},
]
}
}
41 changes: 41 additions & 0 deletions server/models/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Any, Optional, List
from pydantic import BaseModel, Field


class ResponseStatus(BaseModel):
version: str = Field(example='v0.0.15')

#################################################
## ChatGPT
#################################################

class ResponseChat(BaseModel):
# data: ChatCompletion

class Config: # pylint: disable=too-few-public-methods
"""A message to send to the chatbot."""
json_schema_extra = {
"example": {
"chat": {
"id": "chatcmpl-7myaL...",
"object": "chat.completion",
"created": 1691906989,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The Arizona Diamondbacks had Randy Johnson and Curt Schilling as their primary pitchers during the 2001 World Series. Both pitchers had exceptional performances throughout the series."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 52,
"completion_tokens": 32,
"total_tokens": 84
}
}
}
}
11 changes: 11 additions & 0 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from starlette.testclient import TestClient

from main import app

client = TestClient(app)

def test_ping():
response = client.get("/status")
# print(response.json())
assert response.status_code == 200
assert "version" in response.json()
11 changes: 11 additions & 0 deletions tests/unit/test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""test example module."""
import unittest


class TestExampleCase(unittest.TestCase):

@unittest.skip("Example Test Case")
def test_retrieve_files(self):
"""Test that the files are retrieved."""
token = "test"
assert token == "test"

0 comments on commit f439dac

Please sign in to comment.