| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import json
- from Log import logger
- from app.service.v2.app_driver.chat_base import ChatBase
- class ChatDialog(ChatBase):
- def __init__(self, token):
- self.token = token
- async def get_headers(self):
- return {
- 'Content-Type': 'application/json',
- 'Authorization': f'Bearer {self.token}'
- }
- async def chat_completions(self, url, data, headers):
- complete_response = ""
- async for line in self.http_stream(url, data, headers):
- # logger.error(line)
- if line.startswith("data:"):
- complete_response = line.strip("data:").strip()
- else:
- complete_response += line.strip()
- try:
- json_data = json.loads(complete_response)
- # 处理 JSON 数据
- # print(json_data)
- complete_response = ""
- yield json_data
- except json.JSONDecodeError as e:
- logger.info("Invalid JSON data------------------")
- # print(e)
- if __name__ == "__main__":
- async def aa():
- chat_id = "6b8ee426c67511efb1510242ac1b0006"
- token = "ragflow-YzMzE1NDRjYzMyZjExZWY5ZjkxMDI0Mm"
- base_url = "http://192.168.20.116:11080"
- url = f"{base_url}/api/v1/chats/{chat_id}/completions"
- chat = ChatDialog(token)
- data = {
- "question": "电网技术总结300字",
- "stream": True,
- "session_id": "9969c152cce411ef8a140242ac1b0002"
- }
- headers = {
- 'Content-Type': 'application/json',
- 'Authorization': f"Bearer {token}"
- }
- async for ans in chat.chat_completions(url, data, headers):
- print(ans)
- import asyncio
- asyncio.run(aa())
|