This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds npm cache * Has chat stream route and http chat route
- Loading branch information
Showing
14 changed files
with
279 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY= |
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,19 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: FastAPI", | ||
"type": "python", | ||
"request": "launch", | ||
"module": "uvicorn", | ||
"args": [ | ||
"server.api:app", | ||
"--log-level", | ||
"debug" | ||
], | ||
"jinja": true, | ||
"justMyCode": true, | ||
"envFile": "${workspaceFolder}/.env.local" | ||
}, | ||
] | ||
} |
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,43 @@ | ||
#!/bin/bash | ||
|
||
echo "Is this a release? (yes/no)" | ||
read answer | ||
if [ "$answer" == "yes" ]; then | ||
echo "Release confirmed." | ||
echo "" | ||
## prompt user for semantic tag | ||
echo "Please enter the semantic version: " | ||
read version | ||
echo "" | ||
TAG=$version | ||
echo "You entered $TAG" | ||
if git tag | grep -q $TAG; then | ||
echo "Tag $TAG already exists!" | ||
exit 0 | ||
fi | ||
git tag $TAG | ||
git push origin $TAG | ||
elif [ "$answer" == "no" ]; then | ||
TAG=$(git rev-parse HEAD | cut -c1-8) | ||
echo "Creating development build.." | ||
else | ||
echo "Invalid input. Please enter 'yes' or 'no'." | ||
fi | ||
|
||
## Go to root of project | ||
cd $(dirname $0) | ||
cd ../ | ||
DIR=$(pwd) | ||
|
||
IMAGE_URL="promptengineersai/chat-stream-full-stack:$TAG" | ||
|
||
docker build -t $IMAGE_URL . | ||
docker push $IMAGE_URL | ||
|
||
## Print details | ||
echo "" | ||
echo "" | ||
echo "----------------------------------------------------" | ||
echo ">> Version: $TAG" | ||
echo ">> Image: $IMAGE_URL" | ||
echo "----------------------------------------------------" |
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,24 @@ | ||
#!/bin/bash | ||
|
||
################################################################### | ||
### Add branch to changelog | ||
################################################################### | ||
# Get the current branch name | ||
branch=$(git rev-parse --abbrev-ref HEAD) | ||
|
||
# Get the current date and time | ||
date=$(date +%Y-%m-%d) | ||
|
||
# Find the first occurrence of ### Added | ||
line_number=$(grep -n "### Added" Changelog.md | head -1 | cut -d: -f1) | ||
|
||
# Check if the branch has already been added to the Changelog | ||
if grep -q " $branch " Changelog.md; then | ||
echo "Branch $branch has already been added to the Changelog." | ||
else | ||
# Add the branch name and date to the Changelog file | ||
sed -i "${line_number}a\ - $branch ($date)" Changelog.md | ||
|
||
# Commit the change to the Changelog file | ||
git add Changelog.md | ||
fi |
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,25 @@ | ||
#!/bin/bash | ||
|
||
echo "Connect to prod? (yes/no)" | ||
read answer | ||
if [ "$answer" == "yes" ]; then | ||
echo "Start Prod Env locally" | ||
echo "" | ||
echo "" | ||
ENV_FILE=.env.production | ||
elif [ "$answer" == "no" ]; then | ||
ENV_FILE=.env.local | ||
echo "Starting Dev Server.." | ||
else | ||
echo "Invalid input. Please enter 'yes' or 'no'." | ||
fi | ||
|
||
### Set Environment Variables | ||
set -a # automatically export all variables | ||
source $ENV_FILE | ||
set +a | ||
|
||
# Start the server with logging debug mode | ||
uvicorn server.api:app \ | ||
--log-level debug \ | ||
--reload |
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,17 @@ | ||
#!/bin/bash | ||
|
||
ENV_FILE=.env.test | ||
|
||
### Set Environment Variables | ||
set -a # automatically export all variables | ||
source $ENV_FILE | ||
set +a | ||
|
||
if [ -z "$1" ] | ||
then | ||
echo ">> Running all test cases" | ||
python3 -m pytest -s tests | ||
else | ||
echo ">> Running single test case" | ||
python3 -m pytest -s $@ | ||
fi |
Binary file not shown.
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
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,48 @@ | ||
import openai | ||
from typing import AsyncIterable | ||
|
||
from fastapi import HTTPException, Response | ||
|
||
from server.utils import logger, token_stream, end_stream | ||
|
||
|
||
####################################################### | ||
## Open AI Chat GPT | ||
####################################################### | ||
def send_openai_message( | ||
messages, | ||
model:str, | ||
temperature: float or int = 0.0, | ||
stream: bool = False, | ||
) -> AsyncIterable[str]: | ||
"""Send a message to the chatbot and yield the response.""" | ||
response = openai.ChatCompletion.create( | ||
model=model, | ||
messages=messages, | ||
temperature=temperature, | ||
stream=stream | ||
) | ||
return response | ||
|
||
####################################################### | ||
## Open AI Chat GPT | ||
####################################################### | ||
async def send_openai_message_stream( | ||
messages, | ||
model:str, | ||
temperature: float or int = 0.0, | ||
stream: bool = True, | ||
) -> AsyncIterable[str]: | ||
"""Send a message to the chatbot and yield the response.""" | ||
response = openai.ChatCompletion.create( | ||
model=model, | ||
messages=messages, | ||
temperature=temperature, | ||
stream=stream | ||
) | ||
logger.debug('[POST /chat/stream] Stream: %s', str(response)) | ||
for chunk in response: | ||
## Would also consider gathering data here | ||
token = chunk['choices'][0]['delta'].get('content', '') | ||
yield token_stream(token) | ||
yield end_stream() |
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,40 @@ | ||
import json | ||
import logging | ||
|
||
logger = logging.getLogger("uvicorn.error") | ||
|
||
def token_stream(token: str): | ||
""" Use server-sent-events to stream the response""" | ||
data = { | ||
'sender': 'assistant', | ||
'message': token, | ||
'type': 'stream' | ||
} | ||
logger.debug('[POST /chat] Stream: %s', str(data)) | ||
return f"data: {json.dumps(data)}\n\n" | ||
|
||
|
||
def end_stream(): | ||
"""Send the end of the stream""" | ||
end_content = { | ||
'sender': 'assistant', | ||
'message': "", | ||
'type': 'end' | ||
} | ||
logger.debug('[POST /chat] End: %s', str(end_content)) | ||
return f"data: {json.dumps(end_content)}\n\n" | ||
|
||
def retrieve_system_message(messages): | ||
"""Retrieve the system message""" | ||
try: | ||
return list( | ||
filter(lambda message: message['role'] == 'system', messages) | ||
)[0]['content'] | ||
except IndexError: | ||
return None | ||
|
||
def retrieve_chat_messages(messages): | ||
"""Retrieve the chat messages""" | ||
return [ | ||
(msg["content"]) for msg in messages if msg["role"] in ["user", "assistant"] | ||
] |