XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Updated g4f/Provider/AiChats.py

f1605881
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

1 个文件 +24 -9
Modified g4f/Provider/AiChats.py +24 -9
@@ -6,6 +6,7 @@ from aiohttp import ClientSession
6 6 from ..typing import AsyncResult, Messages
7 7 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
8 8 from ..image import ImageResponse
9 from .helper import format_prompt
9 10
10 11 class AiChats(AsyncGeneratorProvider, ProviderModelMixin):
11 12 url = "https://ai-chats.org"
@@ -39,10 +40,15 @@ class AiChats(AsyncGeneratorProvider, ProviderModelMixin):
39 40 "sec-fetch-mode": "cors",
40 41 "sec-fetch-site": "same-origin",
41 42 "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
43 'cookie': 'muVyak=LSFNvUWqdgKkGprbDBsfieIoEMzjOQ; LSFNvUWqdgKkGprbDBsfieIoEMzjOQ=ac28831b98143847e83dbe004404e619-1725548624-1725548621; muVyak_hits=9; ai-chat-front=9d714d5dc46a6b47607c9a55e7d12a95; _csrf-front=76c23dc0a013e5d1e21baad2e6ba2b5fdab8d3d8a1d1281aa292353f8147b057a%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22_csrf-front%22%3Bi%3A1%3Bs%3A32%3A%22K9lz0ezsNPMNnfpd_8gT5yEeh-55-cch%22%3B%7D',
42 44 }
43
45
44 46 async with ClientSession(headers=headers) as session:
45 prompt = cls.format_prompt(messages)
47 if model == 'dalle':
48 prompt = messages[-1]['content'] if messages else ""
49 else:
50 prompt = format_prompt(messages)
51
46 52 data = {
47 53 "type": "image" if model == 'dalle' else "chat",
48 54 "messagesHistory": [
@@ -52,21 +58,21 @@ class AiChats(AsyncGeneratorProvider, ProviderModelMixin):
52 58 }
53 59 ]
54 60 }
55
61
56 62 try:
57 63 async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
58 64 response.raise_for_status()
59
65
60 66 if model == 'dalle':
61 67 response_json = await response.json()
62
68
63 69 if 'data' in response_json and response_json['data']:
64 70 image_url = response_json['data'][0].get('url')
65 71 if image_url:
66 72 async with session.get(image_url) as img_response:
67 73 img_response.raise_for_status()
68 74 image_data = await img_response.read()
69
75
70 76 base64_image = base64.b64encode(image_data).decode('utf-8')
71 77 base64_url = f"data:image/png;base64,{base64_image}"
72 78 yield ImageResponse(base64_url, prompt)
@@ -80,12 +86,21 @@ class AiChats(AsyncGeneratorProvider, ProviderModelMixin):
80 86 for line in full_response.split('\n'):
81 87 if line.startswith('data: ') and line != 'data: ':
82 88 message += line[6:]
83
89
84 90 message = message.strip()
85 91 yield message
86 92 except Exception as e:
87 93 yield f"Error occurred: {str(e)}"
88 94
89 95 @classmethod
90 def format_prompt(cls, messages: Messages) -> str:
91 return messages[-1]['content'] if messages else ""
96 async def create_async(
97 cls,
98 model: str,
99 messages: Messages,
100 proxy: str = None,
101 **kwargs
102 ) -> str:
103 async for response in cls.create_async_generator(model, messages, proxy, **kwargs):
104 if isinstance(response, ImageResponse):
105 return response.images[0]
106 return response