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

XFEstudio/gpt4free

Add Documentaion for PydanticAI support

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

代码差异

3 个文件 +106 -40
Added docs/pydantic_ai.md +95 -0
@@ -0,0 +1,95 @@
1 # PydanticAI Integration with G4F Client
2
3 This README provides an overview of how to integrate PydanticAI with the G4F client to create an agent that interacts with a language model. With this setup, you'll be able to apply patches to use PydanticAI models, enable debugging, and run simple agent-based interactions synchronously. However, please note that tool calls within AI requests are currently **not fully supported** in this environment.
4
5 ## Requirements
6
7 Before starting, make sure you have the following Python dependencies installed:
8
9 - `g4f`: A client that interfaces with various LLMs.
10 - `pydantic_ai`: A module that provides integration with Pydantic-based models.
11
12 ### Installation
13
14 To install these dependencies, you can use `pip`:
15
16 ```bash
17 pip install g4f pydantic_ai
18 ```
19
20 ## Step-by-Step Setup
21
22 ### 1. Patch G4F to Use PydanticAI Models
23
24 In order to use PydanticAI models with G4F, you need to apply the necessary patch to the client. This can be done by importing `apply_patch` from `g4f.tools.pydantic_ai`. The `api_key` parameter is optional, so if you have one, you can provide it. If not, the system will proceed without it.
25
26 ```python
27 from g4f.tools.pydantic_ai import apply_patch
28
29 apply_patch(api_key="your_api_key_here") # Optional
30 ```
31
32 If you don't have an API key, simply omit the `api_key` argument.
33
34 ### 2. Enable Debug Logging
35
36 For troubleshooting and monitoring purposes, you may want to enable debug logging. This can be achieved by setting `g4f.debug.logging` to `True`.
37
38 ```python
39 import g4f.debug
40
41 g4f.debug.logging = True
42 ```
43
44 This will log detailed information about the internal processes and interactions.
45
46 ### 3. Create a Simple Agent
47
48 Now you are ready to create a simple agent that can interact with the LLM. The agent is initialized with a model, and you can also define a system prompt. Here's an example where a basic agent is created with the model `g4f:Gemini:Gemini` and a simple system prompt:
49
50 ```python
51 from g4f import Agent
52
53 # Define the agent
54 agent = Agent(
55 'g4f:Gemini:Gemini',
56 system_prompt='Be concise, reply with one sentence.',
57 )
58 ```
59
60 ### 4. Run the Agent Synchronously
61
62 Once the agent is set up, you can run it synchronously to interact with the LLM. The `run_sync` method sends a query to the LLM and returns the result.
63
64 ```python
65 # Run the agent synchronously with a user query
66 result = agent.run_sync('Where does "hello world" come from?')
67
68 # Output the response
69 print(result.data)
70 ```
71
72 In this example, the agent will send the system prompt along with the user query (`"Where does 'hello world' come from?"`) to the LLM. The LLM will process the request and return a concise answer.
73
74 ### Example Output
75
76 ```bash
77 The phrase "hello world" is commonly used in programming tutorials to demonstrate basic syntax and the concept of outputting text to the screen.
78 ```
79
80 ## Tool Calls and Limitations
81
82 **Important**: Tool calls (such as applying external functions or calling APIs within the AI request itself) are **currently not fully supported**. If your system relies on invoking specific external tools or functions during the conversation with the model, you will need to implement this functionality outside the agent's context or handle it before or after the agent's request.
83
84 For example, you can process your query or interact with external systems before passing the data to the agent.
85
86 ## Conclusion
87
88 By following these steps, you have successfully integrated PydanticAI models into the G4F client, created an agent, and enabled debugging. This allows you to conduct conversations with the language model, pass system prompts, and retrieve responses synchronously.
89
90 ### Notes:
91 - The `api_key` parameter when calling `apply_patch` is optional. If you don’t provide it, the system will still work without an API key.
92 - Modify the agent’s `system_prompt` to suit the nature of the conversation you wish to have.
93 - **Tool calls within AI requests are not fully supported** at the moment. Use the agent's basic functionality for generating responses and handle external calls separately.
94
95 For further customization and advanced use cases, refer to the G4F and PydanticAI documentation.
Modified g4f/Provider/needs_auth/DeepSeekAPI.py +10 -39
@@ -6,58 +6,27 @@ import time
6 6 from typing import AsyncIterator
7 7 import asyncio
8 8
9 from ..base_provider import AsyncAuthedProvider
9 from ..base_provider import AsyncAuthedProvider, ProviderModelMixin
10 10 from ...providers.helper import get_last_user_message
11 from ... import requests
12 from ...errors import MissingAuthError
13 11 from ...requests import get_args_from_nodriver, get_nodriver
14 12 from ...providers.response import AuthResult, RequestLogin, Reasoning, JsonConversation, FinishReason
15 13 from ...typing import AsyncResult, Messages
16 14 try:
17 from curl_cffi import requests
18 from dsk.api import DeepSeekAPI, AuthenticationError, DeepSeekPOW
19
20 class DeepSeekAPIArgs(DeepSeekAPI):
21 def __init__(self, args: dict):
22 self.auth_token = args.pop("api_key")
23 if not self.auth_token or not isinstance(self.auth_token, str):
24 raise AuthenticationError("Invalid auth token provided")
25 self.args = args
26 self.pow_solver = DeepSeekPOW()
27
28 def _make_request(self, method: str, endpoint: str, json_data: dict, pow_required: bool = False, **kwargs):
29 url = f"{self.BASE_URL}{endpoint}"
30 headers = self._get_headers()
31 if pow_required:
32 challenge = self._get_pow_challenge()
33 pow_response = self.pow_solver.solve_challenge(challenge)
34 headers = self._get_headers(pow_response)
35
36 response = requests.request(
37 method=method,
38 url=url,
39 json=json_data, **{
40 **self.args,
41 "headers": {**headers, **self.args["headers"]},
42 "timeout":None,
43 },
44 **kwargs
45 )
46 if response.status_code == 403:
47 raise MissingAuthError()
48 response.raise_for_status()
49 return response.json()
15 from dsk.api import DeepSeekAPI as DskAPI
50 16 has_dsk = True
51 17 except ImportError:
52 18 has_dsk = False
53 19
54 class DeepSeekAPI(AsyncAuthedProvider):
20 class DeepSeekAPI(AsyncAuthedProvider, ProviderModelMixin):
55 21 url = "https://chat.deepseek.com"
56 22 working = has_dsk
57 23 needs_auth = True
58 24 use_nodriver = True
59 25 _access_token = None
60 26
27 default_model = "deepseek-v3"
28 models = ["deepseek-v3", "deepseek-r1"]
29
61 30 @classmethod
62 31 async def on_auth_async(cls, proxy: str = None, **kwargs) -> AsyncIterator:
63 32 if not hasattr(cls, "browser"):
@@ -82,10 +51,11 @@ class DeepSeekAPI(AsyncAuthedProvider):
82 51 messages: Messages,
83 52 auth_result: AuthResult,
84 53 conversation: JsonConversation = None,
54 web_search: bool = False,
85 55 **kwargs
86 56 ) -> AsyncResult:
87 57 # Initialize with your auth token
88 api = DeepSeekAPIArgs(auth_result.get_dict())
58 api = DskAPI(auth_result.get_dict())
89 59
90 60 # Create a new chat session
91 61 if conversation is None:
@@ -97,7 +67,8 @@ class DeepSeekAPI(AsyncAuthedProvider):
97 67 for chunk in api.chat_completion(
98 68 conversation.chat_id,
99 69 get_last_user_message(messages),
100 thinking_enabled=True
70 thinking_enabled="deepseek-r1" in model,
71 search_enabled=web_search
101 72 ):
102 73 if chunk['type'] == 'thinking':
103 74 if not is_thinking:
Modified g4f/gui/server/backend_api.py +1 -1
@@ -371,7 +371,7 @@ class Backend_Api(Api):
371 371 return jsonify({"error": {"message": f"Error uploading file: {str(e)}"}}), 500
372 372
373 373 @app.route('/backend-api/v2/upload_cookies', methods=['POST'])
374 def upload_cookies(self):
374 def upload_cookies():
375 375 file = None
376 376 if "file" in request.files:
377 377 file = request.files['file']