返回提交历史
Modified
docs/pydantic_ai.md
+50
-4
Modified
g4f/client/stubs.py
+7
-4
Modified
g4f/providers/base_provider.py
+3
-1
Modified
g4f/tools/pydantic_ai.py
+5
-4
XFEstudio/gpt4free
Rename apply_patch function in pydantic_ai
357a3bd4
代码差异
4 个文件
+65
-13
@@ -21,12 +21,12 @@ pip install g4f pydantic_ai
21
21
22
22
### 1. Patch PydanticAI to Use G4F Models
23
23
24
In order to use PydanticAI with G4F models, 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.
24
In order to use PydanticAI with G4F models, you need to apply the necessary patch to the client. This can be done by importing `patch_infer_model` 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
25
26
26
```python
27
from g4f.tools.pydantic_ai import apply_patch
27
from g4f.tools.pydantic_ai import patch_infer_model
28
28
29
apply_patch(api_key="your_api_key_here") # Optional
29
patch_infer_model(api_key="your_api_key_here") # Optional
30
30
```
31
31
32
32
If you don't have an API key, simply omit the `api_key` argument.
@@ -83,12 +83,58 @@ The phrase "hello world" is commonly used in programming tutorials to demonstrat
83
83
84
84
For example, you can process your query or interact with external systems before passing the data to the agent.
85
85
86
---
87
88
### Simple Example with Agent
89
90
```python
91
from pydantic_ai import Agent
92
from g4f.tools.pydantic_ai import AIModel
93
94
agent = Agent(
95
AIModel("gpt-4o"),
96
)
97
98
result = agent.run_sync('Are you gpt-4o?')
99
print(result.data)
100
```
101
102
This example shows how to initialize an agent with a specific model (`gpt-4o`) and run it synchronously.
103
104
---
105
106
### Full Example with Tool Calls:
107
108
```python
109
from pydantic import BaseModel
110
from pydantic_ai import Agent
111
from pydantic_ai.models import ModelSettings
112
from g4f.tools.pydantic_ai import apply_patch
113
114
apply_patch("your_api_key")
115
116
class MyModel(BaseModel):
117
city: str
118
country: str
119
120
agent = Agent('g4f:Groq:llama3-70b-8192', result_type=MyModel, model_settings=ModelSettings(temperature=0))
121
122
if __name__ == '__main__':
123
result = agent.run_sync('The windy city in the US of A.')
124
print(result.data)
125
print(result.usage())
126
```
127
128
This example demonstrates the use of a custom Pydantic model (`MyModel`) to capture structured data (city and country) from the response and running the agent with specific model settings.
129
130
---
131
86
132
## Conclusion
87
133
88
134
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
135
90
136
### 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.
137
- The `api_key` parameter when calling `patch_infer_model` is optional. If you don’t provide it, the system will still work without an API key.
92
138
- Modify the agent’s `system_prompt` to suit the nature of the conversation you wish to have.
93
139
- **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
140
@@ -26,12 +26,15 @@ class BaseModel(BaseModel):
26
26
return super().model_construct(**data)
27
27
return cls.construct(**data)
28
28
29
class TokenDetails(BaseModel):
30
pass
31
29
32
class UsageModel(BaseModel):
30
33
prompt_tokens: int
31
34
completion_tokens: int
32
35
total_tokens: int
33
prompt_tokens_details: Optional[Dict[str, Any]]
34
completion_tokens_details: Optional[Dict[str, Any]]
36
prompt_tokens_details: TokenDetails
37
completion_tokens_details: TokenDetails
35
38
36
39
@classmethod
37
40
def model_construct(cls, prompt_tokens=0, completion_tokens=0, total_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, **kwargs):
@@ -39,8 +42,8 @@ class UsageModel(BaseModel):
39
42
prompt_tokens=prompt_tokens,
40
43
completion_tokens=completion_tokens,
41
44
total_tokens=total_tokens,
42
prompt_tokens_details=prompt_tokens_details,
43
completion_tokens_details=completion_tokens_details,
45
prompt_tokens_details=TokenDetails.model_construct(**prompt_tokens_details) if prompt_tokens_details else None,
46
completion_tokens_details=TokenDetails.model_construct(**completion_tokens_details) if completion_tokens_details else None,
44
47
**kwargs
45
48
)
46
49
@@ -374,7 +374,9 @@ class RaiseErrorMixin():
374
374
raise ResponseError(data["error_message"])
375
375
elif "error" in data:
376
376
if "code" in data["error"]:
377
raise ResponseError(f'Error {data["error"]["code"]}: {data["error"]["message"]}')
377
raise ResponseError("\n".join(
378
[e for e in [f'Error {data["error"]["code"]}: {data["error"]["message"]}', data["error"].get("failed_generation")] if e is not None]
379
))
378
380
elif "message" in data["error"]:
379
381
raise ResponseError(data["error"]["message"])
380
382
else:
@@ -7,6 +7,9 @@ from dataclasses import dataclass, field
7
7
from pydantic_ai.models import Model, KnownModelName, infer_model
8
8
from pydantic_ai.models.openai import OpenAIModel, OpenAISystemPromptRole
9
9
10
import pydantic_ai.models.openai
11
pydantic_ai.models.openai.NOT_GIVEN = None
12
10
13
from ..client import AsyncClient
11
14
12
15
@dataclass(init=False)
@@ -62,10 +65,8 @@ def new_infer_model(model: Model | KnownModelName, api_key: str = None) -> Model
62
65
return AIModel(model)
63
66
return infer_model(model)
64
67
65
def apply_patch(api_key: str | None = None):
68
def patch_infer_model(api_key: str | None = None):
66
69
import pydantic_ai.models
67
import pydantic_ai.models.openai
68
70
69
71
pydantic_ai.models.infer_model = partial(new_infer_model, api_key=api_key)
70
pydantic_ai.models.AIModel = AIModel
71
pydantic_ai.models.openai.NOT_GIVEN = None
72
pydantic_ai.models.AIModel = AIModel