Below, you can find code snippets that show how to use PyGEAI SDK’s The Lab module to create, retrieve, list, update, publish and delete Agents using High-Level Interface: AILabManager
Create a new Agent in your project. First, define the Agent (name, access_scope, Prompt, Lllmconfig, Model) and then initiate AILabManager to create it, choosing whether to publish the initial revision or keep it unpublished.
from pygeai.lab.managers import AILabManager
from pygeai.lab.models import Agent, AgentData, Prompt, LlmConfig, Model, Sampling, PromptExample, PromptOutput
agent = Agent(
status="active",
name="Private Translator V6",
access_scope="private",
# public_name="com.genexus.geai.public_translator_24",
job_description="Translates",
avatar_image="https://www.shareicon.net/data/128x128/2016/11/09/851442_logo_512x512.png",
description="Agent that translates from any language to english.",
is_draft=False,
is_readonly=False,
revision=1,
version=None,
agent_data=AgentData(
prompt=Prompt(
instructions="the user will provide a text, you must return the same text translated to english",
inputs="text", "avoid slang indicator",
outputs=
PromptOutput(key="translated_text", description="translated text, with slang or not depending on the indication. in plain text."),
PromptOutput(key="summary", description="a summary in the original language of the text to be translated, also in plain text.")
,
samples=
PromptExample(input_data="opitiiiis mundo [no-slang", output='{"translated_text":"hello world","summary":"saludo"}'),
PromptExample(input_data="esto es una prueba pincheguey keep-slang", output='{"translated_text":"this is a test pal","summary":"prueba"}')
]
),
strategy_name="Dynamic Prompting",
llm_config=LlmConfig(
max_tokens=5000,
timeout=0,
sampling=Sampling(temperature=0.5, top_k=0, top_p=0)
),
models=Model(name="gpt-4-turbo-preview")
)
)
manager = AILabManager()
result = manager.create_agent(
agent=agent,
automatic_publish=False
)
print(f"Agent: {agent.to_dict()}")
Initiate AILabManager to retrieve an existing Agent (by agent_id). Look for the Agent with default settings and its FilterSettings and retrieve it.
from pygeai.lab.managers import AILabManager
from pygeai.lab.models import FilterSettings, Agent
manager = AILabManager()
result = manager.get_agent(
agent_id="9716a0a1-5eab-4cc9-a611-fa2c3237c511"
)
print(f"Retrieved agent: {result.to_dict()}")
filter_settings = FilterSettings(
revision="0",
version="0",
allow_drafts=False
)
result = manager.get_agent(
agent_id="9716a0a1-5eab-4cc9-a611-fa2c3237c511",
filter_settings=filter_settings
)
print(f"Retrieved agent: {result.to_dict()}")
Initiate AILabManager to request a list of existing Agents that meet certain criteria (allow_external, allow_drafts, access_scope). Then, iterate through that list that returns each agent.name and agent.id.
from pygeai.lab.managers import AILabManager
from pygeai.lab.models import FilterSettings, AgentList
ai_lab_manager = AILabManager()
project_id = "2ca6883f-6778-40bb-bcc1-85451fb11107"
filter_settings = FilterSettings(
allow_external=False,
allow_drafts=True,
access_scope="private"
)
result = ai_lab_manager.get_agent_list(
filter_settings=filter_settings
)
for agent in result.agents:
print(f"Agent: {agent.name}, ID: {agent.id}")
Update an existing Agent by initiating AILabManager and redefining its settings.
from pygeai.lab.managers import AILabManager
from pygeai.lab.models import Agent, AgentData, Prompt, LlmConfig, Model, Sampling, PromptExample, PromptOutput, ModelList
updated_agent = Agent(
id="9716a0a1-5eab-4cc9-a611-fa2c3237c511",
status="active",
name="Public Translator V21 Updated",
access_scope="public",
public_name="com.genexus.geai.public_translator_21",
job_description="Translates updated",
avatar_image="https://www.shareicon.net/data/128x128/2016/11/09/851442_logo_512x512.png",
description="Updated agent that translates from any language to English.",
is_draft=True,
is_readonly=False,
revision=1,
version=None,
agent_data=AgentData(
prompt=Prompt(
instructions="the user will provide a text, you must return the same text translated to English with updates",
inputs="text", "avoid slang indicator",
outputs=
PromptOutput(key="translated_text", description="translated text, updated version"),
PromptOutput(key="summary", description="summary in original language, updated")
,
examples=
PromptExample(input_data="opitiiiis mundo [no-slang", output='{"translated_text":"hello world","summary":"saludo"}'),
PromptExample(input_data="esto es una prueba keep-slang", output='{"translated_text":"this is a test","summary":"prueba"}')
]
),
llm_config=LlmConfig(
max_tokens=6000,
timeout=0,
sampling=Sampling(temperature=0.6, top_k=0, top_p=0.0)
),
models=ModelList(models=
Model(name="gpt-4-turbo-preview"),
Model(name="xlm-roberta-large")
)
)
)
manager = AILabManager()
result = manager.update_agent(
agent=updated_agent,
automatic_publish=False,
upsert=False
)
print(fC"Updated agent: {result.to_dict()}")
Start AILabManager to publish a specific revision of an Agent. Then publish_agent_revision with its agent_id.
from pygeai.lab.managers import AILabManager
from pygeai.lab.models import Agent
manager = AILabManager()
result = manager.publish_agent_revision(
agent_id="9716a0a1-5eab-4cc9-a611-fa2c3237c511",
revision="6"
)
print(f"Published agent: {result.name}, ID: {result.id}")
print(f"Revision: {result.revision}, Draft: {result.is_draft}")
Start the AILabManager to permanently delete an Agent, specify theagent_id. If the operation succeeds, the script prints a confirmation message: "Agent deleted successfully".
from pygeai.core.base.responses import EmptyResponse
from pygeai.lab.managers import AILabManager
manager = AILabManager()
agent_id = "3c06e604-26a9-485c-b84e-8eba3ff9a218"
result = manager.delete_agent(
agent_id=agent_id
)
print(f"Agent deleted successfully")