Niansuh commited on
Commit
af14693
·
verified ·
1 Parent(s): 4daf9ee

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -20
main.py CHANGED
@@ -42,23 +42,6 @@ class ChatRequest(BaseModel):
42
  stream: Optional[bool] = False
43
 
44
 
45
- def create_chat_completion_data(content: str, model: str, finish_reason: Optional[str] = None) -> Dict[str, Any]:
46
- return {
47
- "id": f"chatcmpl-{uuid.uuid4()}",
48
- "object": "chat.completion.chunk",
49
- "created": int(datetime.now().timestamp()),
50
- "model": model,
51
- "choices": [
52
- {
53
- "index": 0,
54
- "delta": {"content": content, "role": "assistant"},
55
- "finish_reason": finish_reason,
56
- }
57
- ],
58
- "usage": None,
59
- }
60
-
61
-
62
  @app.post("/hf/v1/chat/completions")
63
  async def chat_completions(request: ChatRequest):
64
  logger.info(f"Received chat completion request for model: {request.model}")
@@ -70,16 +53,40 @@ async def chat_completions(request: ChatRequest):
70
  detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(ALLOWED_MODELS)}",
71
  )
72
 
 
73
  json_data = {
74
  'prompt': "\n".join(
75
  [f"{'User' if msg.role == 'user' else 'Assistant'}: {msg.content}" for msg in request.messages]
76
  ),
77
  'stream': request.stream,
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  'model': request.model,
 
 
 
 
 
 
 
 
 
 
 
79
  }
80
-
81
- logger.info(f"Sending request to external API with model: {request.model}")
82
- logger.debug(f"Request payload: {json.dumps(json_data, indent=2)}")
83
 
84
  async def generate():
85
  async with httpx.AsyncClient() as client:
 
42
  stream: Optional[bool] = False
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  @app.post("/hf/v1/chat/completions")
46
  async def chat_completions(request: ChatRequest):
47
  logger.info(f"Received chat completion request for model: {request.model}")
 
53
  detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(ALLOWED_MODELS)}",
54
  )
55
 
56
+ # Build the payload as per the required structure
57
  json_data = {
58
  'prompt': "\n".join(
59
  [f"{'User' if msg.role == 'user' else 'Assistant'}: {msg.content}" for msg in request.messages]
60
  ),
61
  'stream': request.stream,
62
+ 'app_name': "ChitChat_Chrome_Ext",
63
+ 'app_version': "4.28.0",
64
+ 'auto_search': False,
65
+ 'chat_models': [],
66
+ 'cid': "C082SLBVNR9J", # Adjust based on specific requirements if needed
67
+ 'extra_info': {
68
+ 'origin_url': "chrome-extension://difoiogjjojoaoomphldepapgpbgkhkb/standalone.html?from=sidebar",
69
+ 'origin_title': "Sider"
70
+ },
71
+ 'files': [],
72
+ 'filter_search_history': False,
73
+ 'from': "chat",
74
+ 'group_id': "default",
75
  'model': request.model,
76
+ 'prompt_template': {
77
+ 'key': "artifacts",
78
+ 'attributes': {
79
+ 'lang': "original"
80
+ }
81
+ },
82
+ 'search': False,
83
+ 'tools': {
84
+ 'auto': ["search", "text_to_image", "data_analysis"]
85
+ },
86
+ 'tz_name': "Asia/Karachi"
87
  }
88
+
89
+ logger.info(f"Outgoing API request payload: {json.dumps(json_data, indent=2)}")
 
90
 
91
  async def generate():
92
  async with httpx.AsyncClient() as client: