Another001 commited on
Commit
699f895
1 Parent(s): 55e2afc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -17
main.py CHANGED
@@ -3,6 +3,7 @@ from typing import List
3
  from fastapi import FastAPI, HTTPException, Response, Query, Request
4
  from typing import Optional
5
  from pydantic import BaseModel
 
6
  import g4f
7
  import asyncio
8
 
@@ -43,20 +44,25 @@ async def clean_chat_history():
43
  del chat_history[session_id]
44
  await asyncio.sleep(60)
45
  #-------------------------------------------------------------------------------
46
- @app.get("/AnotherAPI/GPT/Noir/{prompt}")
47
- async def chat(request: Request, prompt: str):
48
- session_id = request.client.host
49
- if session_id not in chat_history:
50
- chat_history[session_id] = [noir_characteristic]
51
-
52
- messages = chat_history[session_id]
53
- messages.append({"role": "user", "content": prompt})
54
-
55
- responses = await run_all(messages)
56
- if responses[0]:
57
- asyncio.create_task(clean_chat_history())
58
- messages.append({"role": "assistant", "content": responses[0]})
59
- return {"response": responses[0]}
60
- else:
61
- asyncio.create_task(clean_chat_history())
62
- raise HTTPException(status_code=500, detail="Provider failed to generate a response")
 
 
 
 
 
 
3
  from fastapi import FastAPI, HTTPException, Response, Query, Request
4
  from typing import Optional
5
  from pydantic import BaseModel
6
+ from characterai import PyCAI
7
  import g4f
8
  import asyncio
9
 
 
44
  del chat_history[session_id]
45
  await asyncio.sleep(60)
46
  #-------------------------------------------------------------------------------
47
+ @app.get("/AnotherAPI/GPT/Noir/{api_key}/{prompt}")
48
+ async def chat(request: Request, api_key: str, prompt: str):
49
+ try:
50
+ client = PyCAI(api_key)
51
+ client.chat.new_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
52
+ session_id = request.client.host
53
+ if session_id not in chat_history:
54
+ chat_history[session_id] = [noir_characteristic]
55
+
56
+ messages = chat_history[session_id]
57
+ messages.append({"role": "user", "content": prompt})
58
+
59
+ responses = await run_all(messages)
60
+ if responses[0]:
61
+ asyncio.create_task(clean_chat_history())
62
+ messages.append({"role": "assistant", "content": responses[0]})
63
+ return {"response": responses[0]}
64
+ else:
65
+ asyncio.create_task(clean_chat_history())
66
+ raise HTTPException(status_code=500, detail="Provider failed")
67
+ except Exception as e:
68
+ raise HTTPException(status_code=401, detail=f"Invalid API key: {str(e)}")