├── .gitignore
├── README.md
├── config.json
├── fastApi.png
├── https_redirect.py
├── main.py
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | env
2 | __pycache__
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # revChatGPTServer Fast API for chatGPT
2 |
3 | This server only contains one endpoint.
4 |
5 | [localhost:8000/docs](localhost:8000/docs)
6 |
7 | # Querying
8 |
9 | Querying a prompt
10 |
11 | ```bash
12 | curl -X 'POST' \
13 | 'http://localhost:8000' \
14 | -H 'accept: application/json' \
15 | -H 'Content-Type: application/json' \
16 | -d '{
17 | "prompt": "What is the meaning of life?"
18 | }'
19 | ```
20 |
21 | This will return some return a JSON that looks like this
22 |
23 | ```json
24 | {
25 | "response": {
26 | "message": "The meaning of life is obvious. It is 42.",
27 | "conversation_id": "a2b190cb-bf3a-4c9e-a45a-fa9fe52d549e",
28 | "parent_id": "4ca08547-eed2-4208-9f81-db8ctc58b904"
29 | }
30 | }
31 | ```
32 |
33 | This can then be posted back up to the AI like this
34 |
35 | ```bash
36 | curl -X 'POST' \
37 | 'http://localhost:8000' \
38 | -H 'accept: application/json' \
39 | -H 'Content-Type: application/json' \
40 | -d '{
41 | "prompt": "What number did you just say?",
42 | "conversation_id": "a2b190cb-bf3a-4c9e-a45a-fa9fe52d549e",
43 | "parent_id": "4ca08547-eed2-4208-9f81-db8ctc58b904"
44 | }'
45 | ```
46 |
47 | ```json
48 | {
49 | "response": {
50 | "message": "42",
51 | "conversation_id": "a2b190cb-bf3a-4c9e-a45a-fa9fe52d549e",
52 | "parent_id": "4ca08547-eed2-4208-9f81-db8ctc58b904"
53 | }
54 | }
55 | ```
56 |
57 | # Setup
58 |
59 | Enter your config file with your user name and password
60 |
61 | ```
62 | {
63 | "email": "",
64 | "password": ""
65 | }
66 | ```
67 |
68 | ```
69 | virtualenv env
70 | source env/bin/activate
71 |
72 | pip install -r requirements.txt
73 |
74 | # Run locally
75 | uvicorn main:app --reload
76 |
77 | # Run in prod
78 | python main.py
79 | ```
80 |
--------------------------------------------------------------------------------
/config.json:
--------------------------------------------------------------------------------
1 | {
2 | "email": "",
3 | "password": ""
4 | }
5 |
--------------------------------------------------------------------------------
/fastApi.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chitalian/revChatGPTServer/fa95b8449cdfea35029bccb77770cbfb938fa134/fastApi.png
--------------------------------------------------------------------------------
/https_redirect.py:
--------------------------------------------------------------------------------
1 | import uvicorn
2 | from fastapi import FastAPI
3 | from starlette.requests import Request
4 | from starlette.responses import RedirectResponse
5 |
6 | app = FastAPI()
7 |
8 |
9 | @app.route('/{_:path}')
10 | async def https_redirect(request: Request):
11 | return RedirectResponse(request.url.replace(scheme='https'))
12 |
13 | if __name__ == '__main__':
14 | uvicorn.run('https_redirect:app', port=80, host='0.0.0.0')
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from subprocess import Popen
2 | from typing import Optional
3 | from revChatGPT.revChatGPT import Chatbot
4 | from fastapi import FastAPI, Response, status
5 | import json
6 |
7 | import uvicorn
8 |
9 |
10 | app = FastAPI()
11 |
12 | from pydantic import BaseModel
13 |
14 | class Item(BaseModel):
15 | prompt: str
16 | conversation_id: Optional[str]
17 | parent_id: Optional[str]
18 |
19 | with open("config.json", "r") as f:
20 | config = json.load(f)
21 | chatbot = Chatbot(config)
22 | @app.post("/")
23 | async def read_root(body: Item, response: Response):
24 |
25 | data ={
26 | "action":"next",
27 | "messages":[
28 | {"id":str(chatbot.generate_uuid()),
29 | "role":"user",
30 | "content":{"content_type":"text","parts":[body.prompt]}
31 | }],
32 | "conversation_id": body.conversation_id if body.conversation_id else None,
33 | "parent_message_id":body.parent_id if body.parent_id else chatbot.generate_uuid(),
34 | "model":"text-davinci-002-render"
35 | }
36 |
37 | try:
38 | result = chatbot.get_chat_text(data)
39 | if result:
40 | return {"response": result}
41 | else:
42 | raise Exception("No response")
43 | except Exception as e:
44 | chatbot.refresh_session()
45 | try:
46 | result = chatbot.get_chat_text(data)
47 | if result:
48 | return {"response": result}
49 | else:
50 | raise Exception("No response")
51 | except Exception as e:
52 | response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
53 | return {"response": "Error"}
54 |
55 |
56 | if __name__ == '__main__':
57 | Popen(['python', '-m', 'https_redirect']) # Add this
58 | uvicorn.run(
59 | 'main:app', port=443, host='0.0.0.0',
60 | reload=True, reload_dirs=['html_files'],
61 | ssl_keyfile='/etc/letsencrypt/live/my_domain/privkey.pem',
62 | ssl_certfile='/etc/letsencrypt/live/my_domain/fullchain.pem')
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | anyio==3.6.2
2 | beautifulsoup4==4.11.1
3 | bs4==0.0.1
4 | certifi==2022.9.24
5 | charset-normalizer==2.1.1
6 | click==8.1.3
7 | fastapi==0.88.0
8 | h11==0.14.0
9 | httptools==0.5.0
10 | idna==3.4
11 | lxml==4.9.1
12 | pydantic==1.10.2
13 | python-dotenv==0.21.0
14 | PyYAML==6.0
15 | requests==2.28.1
16 | revChatGPT==0.0.25
17 | sniffio==1.3.0
18 | soupsieve==2.3.2.post1
19 | starlette==0.22.0
20 | tls-client==0.1.5
21 | typing_extensions==4.4.0
22 | urllib3==1.26.13
23 | uvicorn==0.20.0
24 | uvloop==0.17.0
25 | watchfiles==0.18.1
26 | websockets==10.4
27 |
--------------------------------------------------------------------------------