2023-08-29 11:50:48 +08:00
import traceback
from flask import Flask , request , jsonify , abort , Response
from flask_cors import CORS
import traceback
import litellm
from util import handle_error
2023-12-25 16:40:38 +08:00
from litellm import completion
import os , dotenv , time
2023-08-29 11:50:48 +08:00
import json
2023-12-25 16:40:38 +08:00
2023-08-29 11:50:48 +08:00
dotenv . load_dotenv ( )
# TODO: set your keys in .env or here:
# os.environ["OPENAI_API_KEY"] = "" # set your openai key here
# os.environ["ANTHROPIC_API_KEY"] = "" # set your anthropic key here
# os.environ["TOGETHER_AI_API_KEY"] = "" # set your together ai key here
# see supported models / keys here: https://litellm.readthedocs.io/en/latest/supported/
######### ENVIRONMENT VARIABLES ##########
verbose = True
# litellm.caching_with_models = True # CACHING: caching_with_models Keys in the cache are messages + model. - to learn more: https://docs.litellm.ai/docs/caching/
######### PROMPT LOGGING ##########
2023-12-25 16:40:38 +08:00
os . environ [
" PROMPTLAYER_API_KEY "
] = " " # set your promptlayer key here - https://promptlayer.com/
2023-08-29 11:50:48 +08:00
# set callbacks
litellm . success_callback = [ " promptlayer " ]
############ HELPER FUNCTIONS ###################################
2023-12-25 16:40:38 +08:00
2023-08-29 11:50:48 +08:00
def print_verbose ( print_statement ) :
if verbose :
print ( print_statement )
2023-12-25 16:40:38 +08:00
2023-08-29 11:50:48 +08:00
app = Flask ( __name__ )
CORS ( app )
2023-12-25 16:40:38 +08:00
@app.route ( " / " )
2023-08-29 11:50:48 +08:00
def index ( ) :
2023-12-25 16:40:38 +08:00
return " received! " , 200
2023-08-29 11:50:48 +08:00
def data_generator ( response ) :
for chunk in response :
yield f " data: { json . dumps ( chunk ) } \n \n "
2023-12-25 16:40:38 +08:00
@app.route ( " /chat/completions " , methods = [ " POST " ] )
2023-08-29 11:50:48 +08:00
def api_completion ( ) :
data = request . json
2023-12-25 16:40:38 +08:00
start_time = time . time ( )
if data . get ( " stream " ) == " True " :
data [ " stream " ] = True # convert to boolean
2023-08-29 11:50:48 +08:00
try :
if " prompt " not in data :
raise ValueError ( " data needs to have prompt " )
2023-12-25 16:40:38 +08:00
data [
" model "
] = " togethercomputer/CodeLlama-34b-Instruct " # by default use Together AI's CodeLlama model - https://api.together.xyz/playground/chat?model=togethercomputer%2FCodeLlama-34b-Instruct
2023-08-29 11:50:48 +08:00
# COMPLETION CALL
system_prompt = " Only respond to questions about code. Say ' I don ' t know ' to anything outside of that. "
2023-12-25 16:40:38 +08:00
messages = [
{ " role " : " system " , " content " : system_prompt } ,
{ " role " : " user " , " content " : data . pop ( " prompt " ) } ,
]
2023-08-29 11:50:48 +08:00
data [ " messages " ] = messages
print ( f " data: { data } " )
response = completion ( * * data )
## LOG SUCCESS
2023-12-25 16:40:38 +08:00
end_time = time . time ( )
if (
" stream " in data and data [ " stream " ] == True
) : # use generate_responses to stream responses
return Response ( data_generator ( response ) , mimetype = " text/event-stream " )
2023-08-29 11:50:48 +08:00
except Exception as e :
# call handle_error function
print_verbose ( f " Got Error api_completion(): { traceback . format_exc ( ) } " )
## LOG FAILURE
2023-12-25 16:40:38 +08:00
end_time = time . time ( )
2023-08-29 11:50:48 +08:00
traceback_exception = traceback . format_exc ( )
return handle_error ( data = data )
return response
2023-12-25 16:40:38 +08:00
@app.route ( " /get_models " , methods = [ " POST " ] )
2023-08-29 11:50:48 +08:00
def get_models ( ) :
try :
return litellm . model_list
except Exception as e :
traceback . print_exc ( )
response = { " error " : str ( e ) }
return response , 200
2023-12-25 16:40:38 +08:00
2023-08-29 11:50:48 +08:00
if __name__ == " __main__ " :
2023-12-25 16:40:38 +08:00
from waitress import serve
2023-08-29 11:50:48 +08:00
2023-12-25 16:40:38 +08:00
serve ( app , host = " 0.0.0.0 " , port = 4000 , threads = 500 )