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

XFEstudio/gpt4free

feat(g4f/Provider/AmigoChat.py): add new AmigoChat text and image models

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

代码差异

1 个文件 +178 -0
Added g4f/Provider/AmigoChat.py +178 -0
@@ -0,0 +1,178 @@
1 from __future__ import annotations
2
3 import json
4 import uuid
5 from aiohttp import ClientSession, ClientTimeout
6
7 from ..typing import AsyncResult, Messages
8 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
9 from .helper import format_prompt
10 from ..image import ImageResponse
11
12 class AmigoChat(AsyncGeneratorProvider, ProviderModelMixin):
13 url = "https://amigochat.io/chat/"
14 chat_api_endpoint = "https://api.amigochat.io/v1/chat/completions"
15 image_api_endpoint = "https://api.amigochat.io/v1/images/generations"
16 working = True
17 supports_gpt_4 = True
18 supports_stream = True
19 supports_system_message = True
20 supports_message_history = True
21
22 default_model = 'gpt-4o-mini'
23
24 chat_models = [
25 'gpt-4o',
26 default_model,
27 'o1-preview',
28 'o1-mini',
29 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo',
30 'meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo',
31 'claude-3-sonnet-20240229',
32 'gemini-1.5-pro',
33 ]
34
35 image_models = [
36 'flux-pro/v1.1',
37 'flux-realism',
38 'flux-pro',
39 'dalle-e-3',
40 ]
41
42 models = [*chat_models, *image_models]
43
44 model_aliases = {
45 "o1": "o1-preview",
46 "llama-3.1-405b": "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",
47 "llama-3.2-90b": "meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo",
48 "claude-3.5-sonnet": "claude-3-sonnet-20240229",
49 "gemini-pro": "gemini-1.5-pro",
50
51 "flux-pro": "flux-pro/v1.1",
52 "dalle-3": "dalle-e-3",
53 }
54
55 persona_ids = {
56 'gpt-4o': "gpt",
57 'gpt-4o-mini': "amigo",
58 'o1-preview': "openai-o-one",
59 'o1-mini': "openai-o-one-mini",
60 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo': "llama-three-point-one",
61 'meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo': "llama-3-2",
62 'claude-3-sonnet-20240229': "claude",
63 'gemini-1.5-pro': "gemini-1-5-pro",
64 'flux-pro/v1.1': "flux-1-1-pro",
65 'flux-realism': "flux-realism",
66 'flux-pro': "flux-pro",
67 'dalle-e-3': "dalle-three",
68 }
69
70 @classmethod
71 def get_model(cls, model: str) -> str:
72 if model in cls.models:
73 return model
74 elif model in cls.model_aliases:
75 return cls.model_aliases[model]
76 else:
77 return cls.default_chat_model if model in cls.chat_models else cls.default_image_model
78
79 @classmethod
80 def get_personaId(cls, model: str) -> str:
81 return cls.persona_ids[model]
82
83 @classmethod
84 async def create_async_generator(
85 cls,
86 model: str,
87 messages: Messages,
88 proxy: str = None,
89 stream: bool = False,
90 **kwargs
91 ) -> AsyncResult:
92 model = cls.get_model(model)
93
94 device_uuid = str(uuid.uuid4())
95
96 headers = {
97 "accept": "*/*",
98 "accept-language": "en-US,en;q=0.9",
99 "authorization": "Bearer", # You need to implement proper authorization
100 "cache-control": "no-cache",
101 "content-type": "application/json",
102 "origin": cls.url,
103 "pragma": "no-cache",
104 "priority": "u=1, i",
105 "referer": f"{cls.url}/",
106 "sec-ch-ua": '"Chromium";v="129", "Not=A?Brand";v="8"',
107 "sec-ch-ua-mobile": "?0",
108 "sec-ch-ua-platform": '"Linux"',
109 "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
110 "x-device-language": "en-US",
111 "x-device-platform": "web",
112 "x-device-uuid": device_uuid,
113 "x-device-version": "1.0.32"
114 }
115
116 async with ClientSession(headers=headers) as session:
117 if model in cls.chat_models:
118 # Chat completion
119 data = {
120 "messages": [{"role": m["role"], "content": m["content"]} for m in messages],
121 "model": model,
122 "personaId": cls.get_personaId(model),
123 "frequency_penalty": 0,
124 "max_tokens": 4000,
125 "presence_penalty": 0,
126 "stream": stream,
127 "temperature": 0.5,
128 "top_p": 0.95
129 }
130
131 timeout = ClientTimeout(total=300) # 5 minutes timeout
132 async with session.post(cls.chat_api_endpoint, json=data, proxy=proxy, timeout=timeout) as response:
133 if response.status not in (200, 201):
134 error_text = await response.text()
135 raise Exception(f"Error {response.status}: {error_text}")
136
137 async for line in response.content:
138 line = line.decode('utf-8').strip()
139 if line.startswith('data: '):
140 if line == 'data: [DONE]':
141 break
142 try:
143 chunk = json.loads(line[6:]) # Remove 'data: ' prefix
144 if 'choices' in chunk and len(chunk['choices']) > 0:
145 choice = chunk['choices'][0]
146 if 'delta' in choice:
147 content = choice['delta'].get('content')
148 elif 'text' in choice:
149 content = choice['text']
150 else:
151 content = None
152 if content:
153 yield content
154 except json.JSONDecodeError:
155 pass
156 else:
157 # Image generation
158 prompt = messages[0]['content']
159 data = {
160 "prompt": prompt,
161 "model": model,
162 "personaId": cls.get_personaId(model)
163 }
164 async with session.post(cls.image_api_endpoint, json=data, proxy=proxy) as response:
165 response.raise_for_status()
166
167 response_data = await response.json()
168
169 if "data" in response_data:
170 image_urls = []
171 for item in response_data["data"]:
172 if "url" in item:
173 image_url = item["url"]
174 image_urls.append(image_url)
175 if image_urls:
176 yield ImageResponse(image_urls, prompt)
177 else:
178 yield None