python库
openai flask dataclasses typing
废话不多说直接上干货
import datetime
import json
import logging
from logging. handlers import RotatingFileHandler
from openai import OpenAI
from flask import Flask, request, jsonify, Response
from dataclasses import dataclass, asdict, is_dataclass
from typing import List, Optional
@dataclass
class ChatCompletionMessage : content: str role: str function_call: Optional[ str ] = None tool_calls: Optional[ str ] = None @dataclass
class Choice : finish_reason: str index: int message: ChatCompletionMessagelogprobs: Optional[ str ] = None @dataclass
class CompletionUsage : completion_tokens: int prompt_tokens: int total_tokens: int def custom_to_dict ( obj) : """Recursively convert an object to a dictionary.""" if is_dataclass( obj) : return asdict( obj) elif hasattr ( obj, '__dict__' ) : return { key: custom_to_dict( value) for key, value in obj. __dict__. items( ) } elif isinstance ( obj, list ) : return [ custom_to_dict( item) for item in obj] elif isinstance ( obj, dict ) : return { key: custom_to_dict( value) for key, value in obj. items( ) } else : return obj
@app. route ( '/openai/' , methods= [ 'POST' , 'GET' , 'PUT' ] , defaults= { 'subpath' : '' } )
@app. route ( '/openai/<path:subpath>' , methods= [ 'POST' , 'GET' , 'PUT' ] )
def openai_proxy ( subpath) : data = request. jsondata[ 'stream' ] = data. get( 'stream' , False ) data[ 'top_p' ] = data. get( 'top_p' , 0.7 ) data[ 'temperature' ] = data. get( 'temperature' , 0.9 ) data[ 'max_tokens' ] = data. get( 'max_tokens' , 2048 ) try : client = OpenAI( api_key= "sk-xxxxxxxxxx" , base_url= "https://api.openai.com" ) if data[ 'stream' ] == True : response = client. chat. completions. create( model= data[ 'model' ] , messages= data[ 'messages' ] , messages= data[ 'messages' ] , top_p= data[ 'top_p' ] , temperature= data[ 'temperature' ] , stream= True , max_tokens= data[ 'max_tokens' ] ) def generate ( ) : for chunk in response: chunk_dict = { 'id' : chunk. id , 'created' : chunk. created, 'model' : chunk. model, 'object' : chunk. object , 'choices' : [ { 'delta' : { 'content' : choice. delta. content, 'role' : choice. delta. role, } , 'finish_reason' : choice. finish_reason, 'index' : choice. index, } for choice in chunk. choices] , } chunk_json_str = json. dumps( chunk_dict) yield f"data: { chunk_json_str} " . encode( 'utf-8' ) return Response( generate( ) , mimetype= 'text/event-stream' ) else : response = client. chat. completions. create( model= data[ 'model' ] , messages= data[ 'messages' ] , top_p= data[ 'top_p' ] , temperature= data[ 'temperature' ] , max_tokens= data[ 'max_tokens' ] ) response_dict = custom_to_dict( response) json_result = json. dumps( response_dict, ensure_ascii= False , indent= 4 ) return jsonify( json. loads( json_result) ) except Exception as e: print ( e) return jsonify( { "error" : str ( e) } )
@app. route ( '/glm' , methods= [ 'POST' , 'GET' , 'PUT' ] , defaults= { 'subpath' : '' } )
@app. route ( '/glm/<path:subpath>' , methods= [ 'POST' , 'GET' , 'PUT' ] )
def glm_proxy ( subpath) : data = request. jsonprint ( data[ 'messages' ] ) data[ 'stream' ] = data. get( 'stream' , False ) data[ 'top_p' ] = data. get( 'top_p' , 0.7 ) data[ 'temperature' ] = data. get( 'temperature' , 0.9 ) data[ 'max_tokens' ] = data. get( 'max_tokens' , 2048 ) try : client = OpenAI( api_key= "xxxxxxx" , base_url= "https://open.bigmodel.cn/api/paas/v4/" ) if data[ 'stream' ] == True : response = client. chat. completions. create( model= "GLM-4-air" , messages= data[ 'messages' ] , top_p= data[ 'top_p' ] , temperature= data[ 'temperature' ] , stream= True , max_tokens= data[ 'max_tokens' ] ) def generate ( ) : for chunk in response: chunk_dict = { 'id' : chunk. id , 'created' : chunk. created, 'model' : chunk. model, 'object' : chunk. object , 'choices' : [ { 'delta' : { 'content' : choice. delta. content, 'role' : choice. delta. role, } , 'finish_reason' : choice. finish_reason, 'index' : choice. index, } for choice in chunk. choices] , } chunk_json_str = json. dumps( chunk_dict) yield f"data: { chunk_json_str} " . encode( 'utf-8' ) return Response( generate( ) , mimetype= 'text/event-stream' ) else : response = client. chat. completions. create( model= data[ "model" ] , messages= data[ 'messages' ] , top_p= data[ 'top_p' ] , temperature= data[ 'temperature' ] , max_tokens= data[ 'max_tokens' ] ) response_dict = custom_to_dict( response) json_result = json. dumps( response_dict, ensure_ascii= False , indent= 4 ) print ( json_result) return jsonify( json. loads( json_result) ) except Exception as e: print ( e) return jsonify( { "error" : str ( e) } )
if __name__ == '__main__' : log_handler = RotatingFileHandler( 'app.log' , maxBytes= 10000 , backupCount= 1 ) log_handler. setLevel( logging. INFO) app. logger. addHandler( log_handler) app. logger. setLevel( logging. INFO) app. run( host= "127.0.0.1" , port= 8000 )