forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured.py
More file actions
45 lines (34 loc) · 1.07 KB
/
structured.py
File metadata and controls
45 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import asyncio
import json
import sys
import traceback
from pydantic import BaseModel, Field
from beeai_framework.backend import ChatModel, UserMessage
from beeai_framework.errors import FrameworkError
async def main() -> None:
model = ChatModel.from_name("ollama:llama3.1")
class ProfileSchema(BaseModel):
first_name: str = Field(..., min_length=1)
last_name: str = Field(..., min_length=1)
address: str
age: int = Field(..., min_length=1)
hobby: str
class ErrorSchema(BaseModel):
error: str
class SchemUnion(ProfileSchema, ErrorSchema):
pass
response = await model.create_structure(
schema=SchemUnion,
messages=[UserMessage("Generate a profile of a citizen of Europe.")],
)
print(
json.dumps(
response.object.model_dump() if isinstance(response.object, BaseModel) else response.object, indent=4
)
)
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
traceback.print_exc()
sys.exit(e.explain())