@@ -79,8 +79,9 @@ import com.azure.core.http.rest.PagedIterable;
7979import com.azure.identity.DefaultAzureCredentialBuilder ;
8080import java.util.Arrays ;
8181
82- public class Main {
82+ public class AgentSample {
8383
84+ // A helper function to print messages from the agent
8485 public static void printRunMessages (MessagesClient messagesClient , String threadId ) {
8586
8687 PagedIterable<ThreadMessage > runMessages = messagesClient. listMessages(threadId);
@@ -97,11 +98,11 @@ public class Main {
9798 }
9899 }
99100 }
101+
102+ // a helper function to wait until a run has completed running
100103 public static void waitForRunCompletion (String threadId , ThreadRun threadRun , RunsClient runsClient )
101104 throws InterruptedException {
102105
103- // BEGIN: com.azure.ai.agents.persistent.SampleUtils.waitForRunCompletion
104-
105106 do {
106107 Thread . sleep(500 );
107108 threadRun = runsClient. getRun(threadId, threadRun. getId());
@@ -114,14 +115,14 @@ public class Main {
114115 if (threadRun. getStatus() == RunStatus . FAILED ) {
115116 System . out. println(threadRun. getLastError(). getMessage());
116117 }
117-
118- // END: com.azure.ai.agents.persistent.SampleUtils.waitForRunCompletion
119118 }
120- public static void main (String [] args ) {
121119
120+ public static void main (String [] args ) {
121+ // variables for authenticating requests to the agent service
122122 String projectEndpoint = System . getenv(" PROJECT_ENDPOINT" );
123123 String modelName = System . getenv(" MODEL_DEPLOYMENT_NAME" );
124124
125+ // initialize clients to manage various aspects of agent runtime
125126 PersistentAgentsClientBuilder clientBuilder = new PersistentAgentsClientBuilder ()
126127 .endpoint(projectEndpoint)
127128 .credential(new DefaultAzureCredentialBuilder (). build());
@@ -130,32 +131,33 @@ public class Main {
130131 ThreadsClient threadsClient = agentsClient. getThreadsClient();
131132 MessagesClient messagesClient = agentsClient. getMessagesClient();
132133 RunsClient runsClient = agentsClient. getRunsClient();
133-
134- String agentName = " my-agent" ;
134+
135+
136+ String agentName = " my-agent" ; // the name of the agent
135137 CreateAgentOptions createAgentOptions = new CreateAgentOptions (modelName)
136138 .setName(agentName)
137- .setInstructions(" You are a helpful agent" )
139+ .setInstructions(" You are a helpful agent" ) // system insturctions
138140 .setTools(Arrays . asList(new CodeInterpreterToolDefinition ()));
139141 PersistentAgent agent = administrationClient. createAgent(createAgentOptions);
140142
141143 PersistentAgentThread thread = threadsClient. createThread();
142144 ThreadMessage createdMessage = messagesClient. createMessage(
143145 thread. getId(),
144146 MessageRole . USER ,
145- " I need to solve the equation `3x + 11 = 14`. Can you help me?" );
147+ " I need to solve the equation `3x + 11 = 14`. Can you help me?" ); // The message to the agent
146148
147149 try {
148- // run agent
150+ // run the agent
149151 CreateRunOptions createRunOptions = new CreateRunOptions (thread. getId(), agent. getId())
150152 .setAdditionalInstructions(" " );
151153 ThreadRun threadRun = runsClient. createRun(createRunOptions);
152-
154+ // wait for the run to complete before printing the message
153155 waitForRunCompletion(thread. getId(), threadRun, runsClient);
154156 printRunMessages(messagesClient, thread. getId());
155157 } catch (InterruptedException e) {
156158 throw new RuntimeException (e);
157159 } finally {
158- // cleanup
160+ // cleanup - remove or comment out these lines if you want to keep the agent
159161 threadsClient. deleteThread(thread. getId());
160162 administrationClient. deleteAgent(agent. getId());
161163 }
0 commit comments