Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP via server_chat() #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 162 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,166 @@
# Project related
*.json
*.txt
dist
venv
__pycache__
.DS_Store
*egg-info

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
*egg-info
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ from pychatgpt import Chat
chat = Chat(email="email", password="password")
chat.cli_chat()
```
Start a HTTP server Session
```python
from pychatgpt import Chat

chat = Chat(email="email", password="password")
chat.server_chat() # host:str="127.0.0.1", port:int=8000
```

<details>
<summary>Click to toggle HTTP requests and answers >></summary>

1. GET `http://host:port/{prompt}` returns a `JSONResponse`

```json
{
"answer": "{answer}",
"conversation": "{conversation_id}"
}
```

2. POST `http://host:port/` with an `application/json` body of
```json
{
"prompt": "{prompt}",
"conversation": "{conversation_id}" (optional)
}
```
returns a `JSONResponse`

```json
{
"answer": "{answer}",
"conversation": "{conversation_id}"
}
```

*Note: conversation not implemented yet*
</details>

Ask a one time question
```python
Expand Down
95 changes: 94 additions & 1 deletion src/pychatgpt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
# Fancy stuff
import colorama
from colorama import Fore

from typing import Union
from fastapi import FastAPI, APIRouter
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import uvicorn

app = FastAPI() # A fast api framework
colorama.init(autoreset=True)

class Options:
Expand All @@ -26,6 +33,12 @@ def __init__(self):
self.chat_log: str or None = None
self.id_log: str or None = None

class PostItem(BaseModel):
prompt: str
conversation: Union[int, None] = None # TODO: conversation

colorama.init(autoreset=True)
# @cbv(router) # to embed api in a class
class Chat:
def __init__(self,
email: str,
Expand All @@ -40,6 +53,12 @@ def __init__(self,
self.conversation_id = conversation_id
self.previous_convo_id = previous_convo_id

self.router = APIRouter() # to resolve the issue of fastapi decorations
self.router.add_api_route("/{prompt}", self.http_get, methods=["GET"])
self.router.add_api_route("/", self.http_get_default, methods=["GET"])
self.router.add_api_route("/", self.http_post, methods=["POST"])
app.include_router(self.router) # manually add the router to the app, althoug not elegant

self.__auth_access_token: str or None = None
self.__auth_access_token_expiry: int or None = None
self.__chat_history: list or None = None
Expand Down Expand Up @@ -224,6 +243,80 @@ def save_data(self):
finally:
self.__chat_history = []

# Respond to HTTP GET requests at /
async def http_get_default(self) -> JSONResponse:
"""
GET: "http://host:port/"
Returns: JSONResponse {
"error": "Missing query. e.g {host:port/{your_query}"
}
"""
return jsonable_encoder({"error": "Missing query. e.g {host:port/{your_query}"})


# Respond to HTTP GET requests at /{prompt}
async def http_get(self, prompt: str) -> JSONResponse:
"""
#TODO: conversation queue
GET: "http://host:port/{prompt}"
Returns: JSONResponse {
"answer": "{answer}",
"conversation": "{conversation_id}"
}
"""
if prompt is None or len(prompt) == 0 or prompt == " ":
return jsonable_encoder({"error": "Missing query. e.g {host:port/{your_query}"})
else:
print(f"GET: {prompt}")
answer = self.ask(prompt)
if answer is None:
print(f"{Fore.RED}>> Failed to get a response from the API.")
return jsonable_encoder({"error": "Failed to get a response from the API."})
else:
print(f"Chat GPT: {answer}")
if self.options.track:
self.__chat_history.append("GET: " + prompt)
self.__chat_history.append("Chat GPT: " + answer)
return jsonable_encoder({"answer": answer, "conversation":self.conversation_id})

# Respond to HTTP POST requests
async def http_post(self, request: PostItem) -> JSONResponse:
"""
#TODO: conversation queue
POST: "http://host:port/" with a application/json {
"prompt": "{prompt}",
"conversation": "{conversation_id}" (optional)
}
Returns: JSONResponse {
"answer": "{answer}",
"conversation": "{conversation_id}"
}
"""
prompt = request.prompt
if prompt is None or len(prompt) == 0 or prompt == " ":
return jsonable_encoder({"error": "Missing or invalid query. e.g POST {'prompt': 'your query'} to host:port"})
else:
print(f"POST: {prompt}")
answer = self.ask(prompt)
if answer is None:
print(f"{Fore.RED}>> Failed to get a response from the API.")
return jsonable_encoder({"error": "Failed to get a response from the API."})
else:
print(f"Chat GPT: {answer}")
if self.options.track:
self.__chat_history.append("POST: " + prompt)
self.__chat_history.append("Chat GPT: " + answer)
return jsonable_encoder({"answer": answer, "conversation": self.conversation_id})

def server_chat(self, host:str="127.0.0.1", port:int=8000)->None:
"""
Start a CLI chat session.
:param rep_queue: A queue to put the prompt and response in.
:return:
"""
print(f"{Fore.GREEN}>> Server running at {host}:{port}...")
uvicorn.run(app, host=host, port=port)

def cli_chat(self, rep_queue: Queue or None = None):
"""
Start a CLI chat session.
Expand Down
4 changes: 3 additions & 1 deletion src/pychatgpt/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ colorama~=0.4.4
svglib~=1.4.1
bs4~=0.0.1
beautifulsoup4~=4.10.0
reportlab~=3.6.12
reportlab~=3.6.12
fastapi~=0.88.0
uvicorn~=0.20.0