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

XFEstudio/gpt4free

Fix generate Images with OpenaiChat Add "flux"as alias in HuggingSpace providers Choice a random space provider in HuggingSpace provider Add "Selecting a Provider" Documentation Update requirements list in pypi packages Fix label of CablyAI and DeepInfraChat provider

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

代码差异

12 个文件 +176 -27
Modified README.md +13 -3
@@ -34,9 +34,18 @@ docker pull hlohaus789/g4f
34 34 ```
35 35
36 36 ## 🆕 What's New
37 - **For comprehensive details on new features and updates, please refer to our** [Releases](https://github.com/xtekky/gpt4free/releases) **page**
38 - **Join our Telegram Channel:** 📨 [telegram.me/g4f_channel](https://telegram.me/g4f_channel)
39 - **Join our Discord Group:** 💬🆕️ [https://discord.gg/5E39JUWUFa](https://discord.gg/5E39JUWUFa)
37
38 - **Explore the latest features and updates**
39 Find comprehensive details on our [Releases Page](https://github.com/xtekky/gpt4free/releases).
40
41 - **Stay updated with our Telegram Channel** 📨
42 Join us at [telegram.me/g4f_channel](https://telegram.me/g4f_channel).
43
44 - **Get support in our Discord Community** 🤝💻
45 Reach out for help in our [Support Group: discord.gg/qXA4Wf4Fsm](https://discord.gg/qXA4Wf4Fsm).
46
47 - **Subscribe to our Discord News Channel** 💬🆕️
48 Stay informed about updates via our [News Channel: discord.gg/5E39JUWUFa](https://discord.gg/5E39JUWUFa).
40 49
41 50 ## 🔻 Site Takedown
42 51
@@ -218,6 +227,7 @@ The **Interference API** enables seamless integration with OpenAI's services thr
218 227 - **Documentation**: [Interference API Docs](docs/interference-api.md)
219 228 - **Endpoint**: `http://localhost:1337/v1`
220 229 - **Swagger UI**: Explore the OpenAPI documentation via Swagger UI at `http://localhost:1337/docs`
230 - **Provider Selection**: [How to Specify a Provider?](docs/selecting_a_provider.md)
221 231
222 232 This API is designed for straightforward implementation and enhanced compatibility with other OpenAI integrations.
223 233
Modified docs/interference-api.md +7 -1
@@ -10,9 +10,9 @@
10 10 - [Basic Usage](#basic-usage)
11 11 - [With OpenAI Library](#with-openai-library)
12 12 - [With Requests Library](#with-requests-library)
13 - [Selecting a Provider](#selecting-a-provider)
13 14 - [Key Points](#key-points)
14 15 - [Conclusion](#conclusion)
15
16 16
17 17 ## Introduction
18 18 The G4F Interference API is a powerful tool that allows you to serve other OpenAI integrations using G4F (Gpt4free). It acts as a proxy, translating requests intended for the OpenAI API into requests compatible with G4F providers. This guide will walk you through the process of setting up, running, and using the Interference API effectively.
@@ -149,6 +149,12 @@ for choice in json_response:
149 149
150 150 ```
151 151
152 ## Selecting a Provider
153
154 **Provider Selection**: [How to Specify a Provider?](docs/selecting_a_provider.md)
155
156 Selecting the right provider is a key step in configuring the G4F Interference API to suit your needs. Refer to the guide linked above for detailed instructions on choosing and specifying a provider.
157
152 158 ## Key Points
153 159 - The Interference API translates OpenAI API requests into G4F provider requests.
154 160 - It can be run from either the PyPI package or the cloned repository.
Added docs/selecting_a_provider.md +132 -0
@@ -0,0 +1,132 @@
1
2 ### Selecting a Provider
3
4 **The Interference API also allows you to specify which provider(s) to use for processing requests. This is done using the `provider` parameter, which can be included alongside the `model` parameter in your API requests. Providers can be specified as a space-separated string of provider IDs.**
5
6 #### How to Specify a Provider
7
8 To select one or more providers, include the `provider` parameter in your request body. This parameter accepts a string of space-separated provider IDs. Each ID represents a specific provider available in the system.
9
10 #### Example: Getting a List of Available Providers
11
12 Use the following Python code to fetch the list of available providers:
13
14 ```python
15 import requests
16
17 url = "http://localhost:1337/v1/providers"
18
19 response = requests.get(url, headers={"accept": "application/json"})
20 providers = response.json()
21
22 for provider in providers:
23 print(f"ID: {provider['id']}, URL: {provider['url']}")
24 ```
25
26 #### Example: Getting Detailed Information About a Specific Provider
27
28 Retrieve details about a specific provider, including supported models and parameters:
29
30 ```python
31 provider_id = "HuggingChat"
32 url = f"http://localhost:1337/v1/providers/{provider_id}"
33
34 response = requests.get(url, headers={"accept": "application/json"})
35 provider_details = response.json()
36
37 print(f"Provider ID: {provider_details['id']}")
38 print(f"Supported Models: {provider_details['models']}")
39 print(f"Parameters: {provider_details['params']}")
40 ```
41
42 #### Example: Using a Single Provider in Text Generation
43
44 Specify a single provider (`HuggingChat`) in the request body:
45
46 ```python
47 import requests
48
49 url = "http://localhost:1337/v1/chat/completions"
50
51 payload = {
52 "model": "gpt-4o-mini",
53 "provider": "HuggingChat",
54 "messages": [
55 {"role": "user", "content": "Write a short story about a robot"}
56 ]
57 }
58
59 response = requests.post(url, json=payload, headers={"Content-Type": "application/json"})
60 data = response.json()
61
62 if "choices" in data:
63 for choice in data["choices"]:
64 print(choice["message"]["content"])
65 else:
66 print("No response received")
67 ```
68
69 #### Example: Using Multiple Providers in Text Generation
70
71 Specify multiple providers by separating their IDs with a space:
72
73 ```python
74 import requests
75
76 url = "http://localhost:1337/v1/chat/completions"
77
78 payload = {
79 "model": "gpt-4o-mini",
80 "provider": "HuggingChat AnotherProvider",
81 "messages": [
82 {"role": "user", "content": "What are the benefits of AI in education?"}
83 ]
84 }
85
86 response = requests.post(url, json=payload, headers={"Content-Type": "application/json"})
87 data = response.json()
88
89 if "choices" in data:
90 for choice in data["choices"]:
91 print(choice["message"]["content"])
92 else:
93 print("No response received")
94 ```
95
96 #### Example: Using a Provider for Image Generation
97
98 You can also use the `provider` parameter for image generation:
99
100 ```python
101 import requests
102
103 url = "http://localhost:1337/v1/images/generate"
104
105 payload = {
106 "prompt": "a futuristic cityscape at sunset",
107 "model": "flux",
108 "provider": "HuggingSpace",
109 "response_format": "url"
110 }
111
112 response = requests.post(url, json=payload, headers={"Content-Type": "application/json"})
113 data = response.json()
114
115 if "data" in data:
116 for item in data["data"]:
117 print(f"Image URL: {item['url']}")
118 else:
119 print("No response received")
120 ```
121
122 ### Key Points About Providers
123 - **Flexibility:** Use the `provider` parameter to select one or more providers for your requests.
124 - **Discoverability:** Fetch available providers using the `/providers` endpoint.
125 - **Compatibility:** Check provider details to ensure support for the desired models and parameters.
126
127 By specifying providers in a space-separated string, you can efficiently target specific providers or combine multiple providers in a single request. This approach gives you fine-grained control over how your requests are processed.
128
129
130 ---
131
132 [Go to Interference API Docs](docs/interference-api.md)
Modified g4f/Provider/CablyAI.py +1 -0
@@ -4,6 +4,7 @@ from ..typing import AsyncResult, Messages
4 4 from .needs_auth import OpenaiAPI
5 5
6 6 class CablyAI(OpenaiAPI):
7 label = __name__
7 8 url = "https://cablyai.com"
8 9 login_url = None
9 10 needs_auth = False
Modified g4f/Provider/DeepInfraChat.py +1 -0
@@ -4,6 +4,7 @@ from ..typing import AsyncResult, Messages
4 4 from .needs_auth import OpenaiAPI
5 5
6 6 class DeepInfraChat(OpenaiAPI):
7 label = __name__
7 8 url = "https://deepinfra.com/chat"
8 9 login_url = None
9 10 needs_auth = False
Modified g4f/Provider/hf_space/BlackForestLabsFlux1Dev.py +2 -2
@@ -16,9 +16,9 @@ class BlackForestLabsFlux1Dev(AsyncGeneratorProvider, ProviderModelMixin):
16 16
17 17 default_model = 'black-forest-labs-flux-1-dev'
18 18 default_image_model = default_model
19 image_models = [default_image_model]
19 model_aliases = {"flux-dev": default_model, "flux": default_model}
20 image_models = [default_image_model, *model_aliases.keys()]
20 21 models = image_models
21 model_aliases = {"flux-dev": default_model}
22 22
23 23 @classmethod
24 24 async def create_async_generator(
Modified g4f/Provider/hf_space/BlackForestLabsFlux1Schnell.py +2 -2
@@ -17,9 +17,9 @@ class BlackForestLabsFlux1Schnell(AsyncGeneratorProvider, ProviderModelMixin):
17 17
18 18 default_model = "black-forest-labs-flux-1-schnell"
19 19 default_image_model = default_model
20 image_models = [default_image_model]
20 model_aliases = {"flux-schnell": default_model, "flux": default_model}
21 image_models = [default_image_model, *model_aliases.keys()]
21 22 models = image_models
22 model_aliases = {"flux-schnell": default_model}
23 23
24 24 @classmethod
25 25 async def create_async_generator(
Modified g4f/Provider/hf_space/CohereForAI.py +1 -4
@@ -1,7 +1,6 @@
1 1 from __future__ import annotations
2 2
3 3 import json
4 import uuid
5 4 from aiohttp import ClientSession, FormData
6 5
7 6 from ...typing import AsyncResult, Messages
@@ -24,12 +23,10 @@ class CohereForAI(AsyncGeneratorProvider, ProviderModelMixin):
24 23 "command-r",
25 24 "command-r7b-12-2024",
26 25 ]
27
28 26 model_aliases = {
29 27 "command-r-plus": "command-r-plus-08-2024",
30 28 "command-r": "command-r-08-2024",
31 29 "command-r7b": "command-r7b-12-2024",
32
33 30 }
34 31
35 32 @classmethod
@@ -99,4 +96,4 @@ class CohereForAI(AsyncGeneratorProvider, ProviderModelMixin):
99 96 elif data["type"] == "title":
100 97 yield TitleGeneration(data["title"])
101 98 elif data["type"] == "finalAnswer":
102 break
99 break
Modified g4f/Provider/hf_space/VoodoohopFlux1Schnell.py +3 -3
@@ -12,14 +12,14 @@ from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
12 12 class VoodoohopFlux1Schnell(AsyncGeneratorProvider, ProviderModelMixin):
13 13 url = "https://voodoohop-flux-1-schnell.hf.space"
14 14 api_endpoint = "https://voodoohop-flux-1-schnell.hf.space/call/infer"
15
15
16 16 working = True
17 17
18 18 default_model = "voodoohop-flux-1-schnell"
19 19 default_image_model = default_model
20 image_models = [default_image_model]
20 model_aliases = {"flux-schnell": default_model, "flux": default_model}
21 image_models = [default_image_model, *model_aliases.keys()]
21 22 models = image_models
22 model_aliases = {"flux-schnell": default_model}
23 23
24 24 @classmethod
25 25 async def create_async_generator(
Modified g4f/Provider/hf_space/__init__.py +5 -4
@@ -1,5 +1,7 @@
1 1 from __future__ import annotations
2 2
3 import random
4
3 5 from ...typing import AsyncResult, Messages, ImagesType
4 6 from ...errors import ResponseError
5 7 from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
@@ -15,15 +17,13 @@ from .StableDiffusion35Large import StableDiffusion35Large
15 17 class HuggingSpace(AsyncGeneratorProvider, ProviderModelMixin):
16 18 url = "https://huggingface.co/spaces"
17 19 parent = "HuggingFace"
18
20
19 21 working = True
20
22
21 23 default_model = Qwen_Qwen_2_72B_Instruct.default_model
22 24 default_image_model = BlackForestLabsFlux1Dev.default_model
23 25 default_vision_model = Qwen_QVQ_72B.default_model
24 26 providers = [BlackForestLabsFlux1Dev, BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell, CohereForAI, Qwen_QVQ_72B, Qwen_Qwen_2_72B_Instruct, StableDiffusion35Large]
25
26
27 27
28 28 @classmethod
29 29 def get_parameters(cls, **kwargs) -> dict:
@@ -57,6 +57,7 @@ class HuggingSpace(AsyncGeneratorProvider, ProviderModelMixin):
57 57 if not model and images is not None:
58 58 model = cls.default_vision_model
59 59 is_started = False
60 random.shuffle(cls.providers)
60 61 for provider in cls.providers:
61 62 if model in provider.model_aliases:
62 63 async for chunk in provider.create_async_generator(provider.model_aliases[model], messages, **kwargs):
Modified g4f/Provider/needs_auth/OpenaiChat.py +4 -4
@@ -264,7 +264,7 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
264 264 return messages
265 265
266 266 @classmethod
267 async def get_generated_image(cls, auth_result: AuthResult, session: StreamSession, element: dict, prompt: str = None) -> ImageResponse:
267 async def get_generated_image(cls, session: StreamSession, auth_result: AuthResult, element: dict, prompt: str = None) -> ImageResponse:
268 268 try:
269 269 prompt = element["metadata"]["dalle"]["prompt"]
270 270 file_id = element["asset_pointer"].split("file-service://", 1)[1]
@@ -452,7 +452,7 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
452 452 await raise_for_status(response)
453 453 buffer = u""
454 454 async for line in response.iter_lines():
455 async for chunk in cls.iter_messages_line(session, line, conversation, sources):
455 async for chunk in cls.iter_messages_line(session, auth_result, line, conversation, sources):
456 456 if isinstance(chunk, str):
457 457 chunk = chunk.replace("\ue203", "").replace("\ue204", "").replace("\ue206", "")
458 458 buffer += chunk
@@ -500,7 +500,7 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
500 500 yield FinishReason(conversation.finish_reason)
501 501
502 502 @classmethod
503 async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: Conversation, sources: Sources) -> AsyncIterator:
503 async def iter_messages_line(cls, session: StreamSession, auth_result: AuthResult, line: bytes, fields: Conversation, sources: Sources) -> AsyncIterator:
504 504 if not line.startswith(b"data: "):
505 505 return
506 506 elif line.startswith(b"data: [DONE]"):
@@ -546,7 +546,7 @@ class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
546 546 generated_images = []
547 547 for element in c.get("parts"):
548 548 if isinstance(element, dict) and element.get("content_type") == "image_asset_pointer":
549 image = cls.get_generated_image(session, cls._headers, element)
549 image = cls.get_generated_image(session, auth_result, element)
550 550 generated_images.append(image)
551 551 for image_response in await asyncio.gather(*generated_images):
552 552 if image_response is not None:
Modified setup.py +5 -4
@@ -48,11 +48,11 @@ EXTRA_REQUIRE = {
48 48 'slim': [
49 49 "curl_cffi>=0.6.2",
50 50 "certifi",
51 "browser_cookie3",
51 52 "duckduckgo-search>=5.0" ,# internet.search
52 53 "beautifulsoup4", # internet.search and bing.create_images
53 54 "aiohttp_socks", # proxy
54 55 "pillow", # image
55 "cairosvg", # svg image
56 56 "werkzeug", "flask", # gui
57 57 "fastapi", # api
58 58 "uvicorn", # api
@@ -68,7 +68,8 @@ EXTRA_REQUIRE = {
68 68 "webview": [
69 69 "pywebview",
70 70 "platformdirs",
71 "cryptography"
71 "plyer",
72 "cryptography",
72 73 ],
73 74 "api": [
74 75 "loguru", "fastapi",
@@ -79,10 +80,10 @@ EXTRA_REQUIRE = {
79 80 "werkzeug", "flask",
80 81 "beautifulsoup4", "pillow",
81 82 "duckduckgo-search>=5.0",
82 "browser_cookie3",
83 83 ],
84 84 "search": [
85 "beautifulsoup4", "pillow",
85 "beautifulsoup4",
86 "pillow",
86 87 "duckduckgo-search>=5.0",
87 88 ],
88 89 "local": [