parent
103d3937d3
commit
e95090cbba
@ -0,0 +1,77 @@
|
||||
"""
|
||||
* WORKING
|
||||
What this script does:
|
||||
Structured output example with validation function
|
||||
Requirements:
|
||||
pip install openai
|
||||
pip install pydantic
|
||||
Add the folowing API key(s) in your .env file:
|
||||
- OPENAI_API_KEY (this example works best with Openai bc it uses openai function calling structure)
|
||||
"""
|
||||
|
||||
################ Adding project root to PYTHONPATH ################################
|
||||
# If you are running playground examples in the project files directly, use this:
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.getcwd())
|
||||
|
||||
################ Adding project root to PYTHONPATH ################################
|
||||
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from typing_extensions import Annotated
|
||||
from pydantic import AfterValidator
|
||||
|
||||
|
||||
def symbol_must_exists(symbol= str) -> str:
|
||||
symbols = [
|
||||
"AAPL", "MSFT", "AMZN", "GOOGL", "GOOG", "META", "TSLA", "NVDA", "BRK.B",
|
||||
"JPM", "JNJ", "V", "PG", "UNH", "MA", "HD", "BAC", "XOM", "DIS", "CSCO"
|
||||
]
|
||||
if symbol not in symbols:
|
||||
raise ValueError(f"symbol must exists in the list: {symbols}")
|
||||
|
||||
return symbol
|
||||
|
||||
|
||||
# Initialize the schema for the person's information
|
||||
class StockInfo(BaseModel):
|
||||
"""
|
||||
To create a StockInfo, you need to return a JSON object with the following format:
|
||||
{
|
||||
"function_call": "StockInfo",
|
||||
"parameters": {
|
||||
...
|
||||
}
|
||||
}
|
||||
"""
|
||||
name: str = Field(..., title="Name of the company")
|
||||
description: str = Field(..., title="Description of the company")
|
||||
symbol: Annotated[str, AfterValidator(symbol_must_exists)] = Field(..., title="stock symbol of the company")
|
||||
|
||||
|
||||
# Define the task to generate a person's information
|
||||
task = "Generate an existing S&P500's company information"
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Stock Information Generator",
|
||||
system_prompt=(
|
||||
"Generate a public comapany's information"
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
verbose=True,
|
||||
# List of schemas that the agent can handle
|
||||
list_base_models=[StockInfo],
|
||||
output_validation=True,
|
||||
)
|
||||
|
||||
# Run the agent to generate the person's information
|
||||
generated_data = agent.run(task)
|
||||
|
||||
# Print the generated data
|
||||
print(f"Generated data: {generated_data}")
|
@ -0,0 +1,65 @@
|
||||
"""
|
||||
* WORKING
|
||||
What this script does:
|
||||
Structured output example
|
||||
Requirements:
|
||||
Add the folowing API key(s) in your .env file:
|
||||
- OPENAI_API_KEY (this example works best with Openai bc it uses openai function calling structure)
|
||||
|
||||
"""
|
||||
|
||||
################ Adding project root to PYTHONPATH ################################
|
||||
# If you are running playground examples in the project files directly, use this:
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.getcwd())
|
||||
|
||||
################ Adding project root to PYTHONPATH ################################
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
|
||||
# Initialize the schema for the person's information
|
||||
class PersonInfo(BaseModel):
|
||||
"""
|
||||
To create a PersonInfo, you need to return a JSON object with the following format:
|
||||
{
|
||||
"function_call": "PersonInfo",
|
||||
"parameters": {
|
||||
...
|
||||
}
|
||||
}
|
||||
"""
|
||||
name: str = Field(..., title="Name of the person")
|
||||
age: int = Field(..., title="Age of the person")
|
||||
is_student: bool = Field(..., title="Whether the person is a student")
|
||||
courses: list[str] = Field(
|
||||
..., title="List of courses the person is taking"
|
||||
)
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Person Information Generator",
|
||||
system_prompt=(
|
||||
"Generate a person's information"
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
verbose=True,
|
||||
# List of pydantic models that the agent can use
|
||||
list_base_models=[PersonInfo],
|
||||
output_validation=True
|
||||
)
|
||||
|
||||
# Define the task to generate a person's information
|
||||
task = "Generate a person's information for Paul Graham 56 years old and is a student at Harvard University and is taking 3 courses: Math, Science, and History."
|
||||
|
||||
# Run the agent to generate the person's information
|
||||
generated_data = agent.run(task)
|
||||
|
||||
# Print the generated data
|
||||
print(type(generated_data))
|
||||
print(f"Generated data: {generated_data}")
|
Loading…
Reference in new issue