zhaoqingang
2025-02-24 f13239560d9a6888d3bf95699ed5fc10395b50ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
 
from Log import logger
from app.service.v2.app_driver.chat_base import ChatBase
 
 
class ChatDialog(ChatBase):
 
    async def chat_completions(self, url, data, headers):
        complete_response = ""
        async for line in self.http_stream(url, data, headers):
            # print(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)
 
    async def chat_sessions(self, url, data, headers):
 
        res = await self.http_post(url, data, headers)
        if res.status_code == 200:
            return res.json()
        else:
            return {}
 
 
 
    @staticmethod
    async def request_data(question, session_id=""):
        return {
            "question": question,
            "stream": True,
            "session_id": session_id
        }
 
 
 
 
 
 
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())