This guide will walk you through the process of creating a Julep task and executing it.
This guide is based on the Trip Planning task. For a detailed explanation of the task’s workflow, please check out the corresponding Trip Planning tutorial.
In this step, you will define the task that the Julep agent will execute. This involves specifying the task’s name, description, input schema, tools, and the main workflow. The task definition is written in YAML format and includes details about the integrations and the logic for processing the input data.
import yamltask_def = yaml.safe_load("""# yaml-language-server: $schema=https://raw.githubusercontent.com/julep-ai/julep/refs/heads/dev/schemas/create_task_request.jsonname: Julep Trip Planning Taskdescription: A Julep agent that can generate a detailed itinerary for visiting tourist attractions in some locations, considering the current weather conditions.########################################################################### INPUT SCHEMA ###############################################################################input_schema:type:object properties: locations:type: array items:type: string description: The locations to search for.########################################################################### TOOLS ######################################################################################tools:- name: wikipediatype: integration integration: provider: wikipedia- name: weathertype: integration integration: provider: weather setup: openweathermap_api_key:"YOUR_OPENWEATHERMAP_API_KEY"- name: internet_searchtype: integration integration: provider: brave setup: api_key:"YOUR_BRAVE_API_KEY"########################################################################### MAIN WORKFLOW ##############################################################################main:- over: $ steps[0].input.locationsmap: tool: weather arguments: location: $ _- over: $ steps[0].input.locationsmap: tool: internet_search arguments: query: $ 'tourist attractions in '+ _# Zip locations, weather, and attractions into a list of tuples [(location, weather, attractions)]- evaluate: zipped:|- $ list(zip( steps[0].input.locations,[output['result']for output in steps[0].output], steps[1].output))- over: $ _['zipped']parallelism:3# Inside the map step, each `_` represents the current element in the list# which is a tuple of (location, weather, attractions)map: prompt:- role: system content:>- $ f'''You are {agent.name}. Your task is to create a detailed itineraryfor visiting tourist attractions in some locations. The user will give you the following information for each location:- The location- The current weather condition- The top tourist attractions'''- role: user content:>- $ f'''Location:"{_[0]}" Weather:"{_[1]}" Attractions:"{_[2]}"''' unwrap: true- evaluate: final_plan:|- $ '\\n---------------\\n'.join(activity for activity in _)""")# Create a tasktask = julep.tasks.create( agent_id=agent.id,**task_def)
Once the task is created, you can execute it by providing the necessary input that matches the task’s input schema. This step involves calling the execute method on the task, which will start the task execution process.
# Execute the task with specific input that matches the task's input schemaexecution = julep.tasks.execute( task_id=task.id,input={"locations":["New York","Paris","Tokyo"]})
After executing the task, you can retrieve the results by checking the execution status and output. This step involves polling the execution status until it reaches a terminal state (succeeded, failed, or cancelled) and printing the current output. Alternatively, you can fetch the execution transitions for a more detailed view of the task’s progress, which is useful for debugging.
import time# Fetch the execution status & current outputexecution = julep.executions.get(execution_id=execution.id)while execution.status notin["succeeded","failed","cancelled"]: execution = julep.executions.get(execution_id=execution.id)print(f"Execution status: {execution.status}")print(f"Execution output: {execution.output}")print("************************************************")# Wait for 5 seconds before polling again time.sleep(5)
import time# Fetch the execution transitionstransitions = julep.executions.transitions.list(execution_id=execution.id)# Wait until the execution is either finished, errored, or canceledwhile transitions[0].items.typenotin["finish","error","canceled"]: transitions = julep.executions.transitions.list(execution_id=execution.id)# Transitions are ordered from the latest to the oldestfor transition inreversed(transitions.items):print(f"Transition type: {transition.type}")print(f"Transition output: {transition.output}")print("************************************************")# Wait for 5 seconds before fetching the next set of transitions time.sleep(5)
from julep import Julepimport yamlimport time# Initialize the Julep clientjulep = Julep(api_key="your_api_key")# Create an agentagent = julep.agents.create( name="Task Agent", model="gpt-4o")task_def = yaml.safe_load("""# yaml-language-server: $schema=https://raw.githubusercontent.com/julep-ai/julep/refs/heads/dev/schemas/create_task_request.jsonname: Julep Trip Planning Taskdescription: A Julep agent that can generate a detailed itinerary for visiting tourist attractions in some locations, considering the current weather conditions.########################################################################### INPUT SCHEMA ###############################################################################input_schema:type:object properties: locations:type: array items:type: string description: The locations to search for.########################################################################### TOOLS ######################################################################################tools:- name: wikipediatype: integration integration: provider: wikipedia- name: weathertype: integration integration: provider: weather setup: openweathermap_api_key:"YOUR_OPENWEATHERMAP_API_KEY"- name: internet_searchtype: integration integration: provider: brave setup: api_key:"YOUR_BRAVE_API_KEY"########################################################################### MAIN WORKFLOW ##############################################################################main:- over: $ steps[0].input.locationsmap: tool: weather arguments: location: $ _- over: $ steps[0].input.locationsmap: tool: internet_search arguments: query: $ 'tourist attractions in '+ _# Zip locations, weather, and attractions into a list of tuples [(location, weather, attractions)]- evaluate: zipped:|- $ list(zip( steps[0].input.locations,[output['result']for output in steps[0].output], steps[1].output))- over: $ _['zipped']parallelism:3# Inside the map step, each `_` represents the current element in the list# which is a tuple of (location, weather, attractions)map: prompt:- role: system content:>- $ f'''You are {agent.name}. Your task is to create a detailed itineraryfor visiting tourist attractions in some locations. The user will give you the following information for each location:- The location- The current weather condition- The top tourist attractions'''- role: user content:>- $ f'''Location:"{_[0]}" Weather:"{_[1]}" Attractions:"{_[2]}"''' unwrap: true- evaluate: final_plan:|- $ '\\n---------------\\n'.join(activity for activity in _)""")# Create a tasktask = julep.tasks.create( agent_id=agent.id,**task_def)# Execute the task with specific input that matches the task's input schemaexecution = julep.tasks.execute( task_id=task.id,input={"locations":["New York","Paris","Tokyo"]})# Fetch the execution status & current outputexecution = julep.executions.get(execution_id=execution.id)while execution.status notin["succeeded","failed","cancelled"]: execution = julep.executions.get(execution_id=execution.id)print(f"Execution status: {execution.status}")print(f"Execution output: {execution.output}")print("************************************************")# Wait for 5 seconds before polling again time.sleep(5)
This guide provided a comprehensive overview of how to create and execute a Julep task. It covered the necessary steps, including initializing the Julep client, creating an agent, defining the task, executing it, and retrieving the results. By following these steps, you can effectively use Julep’s capabilities to automate complex workflows and achieve your goals.