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

XFEstudio/gpt4free

Fix Image Generation in HuggingChat Fetch model list in ThebApi provider

3c35e6d5
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

4 个文件 +26 -45
Modified g4f/Provider/local/Ollama.py +8 -4
@@ -9,15 +9,19 @@ from ...typing import AsyncResult, Messages
9 9 class Ollama(OpenaiAPI):
10 10 label = "Ollama"
11 11 url = "https://ollama.com"
12 login_url = None
12 13 needs_auth = False
13 14 working = True
14 15
15 16 @classmethod
16 def get_models(cls):
17 def get_models(cls, api_base: str = None, **kwargs):
17 18 if not cls.models:
18 host = os.getenv("OLLAMA_HOST", "127.0.0.1")
19 port = os.getenv("OLLAMA_PORT", "11434")
20 url = f"http://{host}:{port}/api/tags"
19 if api_base is None:
20 host = os.getenv("OLLAMA_HOST", "127.0.0.1")
21 port = os.getenv("OLLAMA_PORT", "11434")
22 url = f"http://{host}:{port}/api/tags"
23 else:
24 url = api_base.replace("/v1", "/api/tags")
21 25 models = requests.get(url).json()["models"]
22 26 cls.models = [model["name"] for model in models]
23 27 cls.default_model = cls.models[0]
Modified g4f/Provider/needs_auth/HuggingChat.py +2 -15
@@ -92,7 +92,7 @@ class HuggingChat(AsyncAuthedProvider, ProviderModelMixin):
92 92 @classmethod
93 93 async def on_auth_async(cls, cookies: Cookies = None, proxy: str = None, **kwargs) -> AsyncIterator:
94 94 if cookies is None:
95 cookies = get_cookies("huggingface.co")
95 cookies = get_cookies("huggingface.co", single_browser=True)
96 96 if "hf-chat" in cookies:
97 97 yield AuthResult(
98 98 cookies=cookies,
@@ -158,21 +158,9 @@ class HuggingChat(AsyncAuthedProvider, ProviderModelMixin):
158 158
159 159 headers = {
160 160 'accept': '*/*',
161 'accept-language': 'en',
162 'cache-control': 'no-cache',
163 161 'origin': 'https://huggingface.co',
164 'pragma': 'no-cache',
165 'priority': 'u=1, i',
166 162 'referer': f'https://huggingface.co/chat/conversation/{conversationId}',
167 'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
168 'sec-ch-ua-mobile': '?0',
169 'sec-ch-ua-platform': '"macOS"',
170 'sec-fetch-dest': 'empty',
171 'sec-fetch-mode': 'cors',
172 'sec-fetch-site': 'same-origin',
173 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
174 163 }
175
176 164 data = CurlMime()
177 165 data.addpart('data', data=json.dumps(settings, separators=(',', ':')))
178 166 if images is not None:
@@ -185,7 +173,6 @@ class HuggingChat(AsyncAuthedProvider, ProviderModelMixin):
185 173
186 174 response = session.post(
187 175 f'https://huggingface.co/chat/conversation/{conversationId}',
188 cookies=session.cookies,
189 176 headers=headers,
190 177 multipart=data,
191 178 stream=True
@@ -210,7 +197,7 @@ class HuggingChat(AsyncAuthedProvider, ProviderModelMixin):
210 197 elif line["type"] == "file":
211 198 url = f"https://huggingface.co/chat/conversation/{conversationId}/output/{line['sha']}"
212 199 prompt = messages[-1]["content"] if prompt is None else prompt
213 yield ImageResponse(url, alt=prompt, options={"cookies": cookies})
200 yield ImageResponse(url, alt=prompt, options={"cookies": auth_result.cookies})
214 201 elif line["type"] == "webSearch" and "sources" in line:
215 202 sources = Sources(line["sources"])
216 203 elif line["type"] == "title":
Modified g4f/Provider/needs_auth/OpenaiAPI.py +3 -2
@@ -108,10 +108,10 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin):
108 108 if api_endpoint is None:
109 109 api_endpoint = f"{api_base.rstrip('/')}/chat/completions"
110 110 async with session.post(api_endpoint, json=data) as response:
111 await raise_for_status(response)
112 if not stream:
111 if not stream or response.headers.get("content-type") == "application/json":
113 112 data = await response.json()
114 113 cls.raise_error(data)
114 await raise_for_status(response)
115 115 choice = data["choices"][0]
116 116 if "content" in choice["message"] and choice["message"]["content"]:
117 117 yield choice["message"]["content"].strip()
@@ -123,6 +123,7 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin):
123 123 yield FinishReason(choice["finish_reason"])
124 124 return
125 125 else:
126 await raise_for_status(response)
126 127 first = True
127 128 async for line in response.iter_lines():
128 129 if line.startswith(b"data: "):
Modified g4f/Provider/needs_auth/ThebApi.py +13 -24
@@ -1,61 +1,50 @@
1 1 from __future__ import annotations
2 2
3 3 from ...typing import CreateResult, Messages
4 from ..helper import filter_none
4 5 from .OpenaiAPI import OpenaiAPI
5 6
6 7 models = {
7 8 "theb-ai": "TheB.AI",
8 9 "gpt-3.5-turbo": "GPT-3.5",
9 "gpt-3.5-turbo-16k": "GPT-3.5-16K",
10 10 "gpt-4-turbo": "GPT-4 Turbo",
11 11 "gpt-4": "GPT-4",
12 "gpt-4-32k": "GPT-4 32K",
13 "claude-2": "Claude 2",
14 "claude-1": "Claude",
15 "claude-1-100k": "Claude 100K",
16 "claude-instant-1": "Claude Instant",
17 "claude-instant-1-100k": "Claude Instant 100K",
18 "palm-2": "PaLM 2",
19 "palm-2-codey": "Codey",
20 "vicuna-13b-v1.5": "Vicuna v1.5 13B",
12 "claude-3.5-sonnet": "Claude",
21 13 "llama-2-7b-chat": "Llama 2 7B",
22 14 "llama-2-13b-chat": "Llama 2 13B",
23 15 "llama-2-70b-chat": "Llama 2 70B",
24 16 "code-llama-7b": "Code Llama 7B",
25 17 "code-llama-13b": "Code Llama 13B",
26 18 "code-llama-34b": "Code Llama 34B",
27 "qwen-7b-chat": "Qwen 7B"
19 "qwen-2-72b": "Qwen"
28 20 }
29 21
30 22 class ThebApi(OpenaiAPI):
31 23 label = "TheB.AI API"
32 24 url = "https://theb.ai"
25 login_url = "https://beta.theb.ai/home"
33 26 working = True
34 27 api_base = "https://api.theb.ai/v1"
35 28 needs_auth = True
36 default_model = "gpt-3.5-turbo"
37 models = list(models)
29 default_model = "theb-ai"
30 fallback_models = list(models)
38 31
39 32 @classmethod
40 33 def create_async_generator(
41 34 cls,
42 35 model: str,
43 36 messages: Messages,
44 temperature: float = 1,
45 top_p: float = 1,
37 temperature: float = None,
38 top_p: float = None,
46 39 **kwargs
47 40 ) -> CreateResult:
48 if "auth" in kwargs:
49 kwargs["api_key"] = kwargs["auth"]
50 41 system_message = "\n".join([message["content"] for message in messages if message["role"] == "system"])
51 if not system_message:
52 system_message = "You are ChatGPT, a large language model trained by OpenAI, based on the GPT-3.5 architecture."
53 42 messages = [message for message in messages if message["role"] != "system"]
54 43 data = {
55 "model_params": {
56 "system_prompt": system_message,
57 "temperature": temperature,
58 "top_p": top_p,
59 }
44 "model_params": filter_none(
45 system_prompt=system_message,
46 temperature=temperature,
47 top_p=top_p,
48 )
60 49 }
61 50 return super().create_async_generator(model, messages, extra_data=data, **kwargs)