(self, index: SampleIndex, agent: AgentClient)
| 52 | return concurrency |
| 53 | |
| 54 | def run_sample(self, index: SampleIndex, agent: AgentClient) -> TaskClientOutput: |
| 55 | try: |
| 56 | result = requests.post( |
| 57 | self.controller_address + "/start_sample", |
| 58 | json=StartSampleRequest(name=self.name, index=index).dict(), |
| 59 | ) |
| 60 | except Exception as e: |
| 61 | return TaskClientOutput(error=TaskError.NETWORK_ERROR.value, info=str(e)) |
| 62 | if result.status_code == 406: |
| 63 | return TaskClientOutput( |
| 64 | error=TaskError.NOT_AVAILABLE.value, info=result.text |
| 65 | ) |
| 66 | if result.status_code != 200: |
| 67 | return TaskClientOutput( |
| 68 | error=TaskError.START_FAILED.value, info=result.text |
| 69 | ) |
| 70 | result = result.json() |
| 71 | sid = result["session_id"] |
| 72 | latest_result = result |
| 73 | while SampleStatus(result["output"]["status"]) == SampleStatus.RUNNING: |
| 74 | try: |
| 75 | content = agent.inference(result["output"]["history"]) |
| 76 | response = AgentOutput(content=content) |
| 77 | except AgentContextLimitException: |
| 78 | response = AgentOutput(status=AgentOutputStatus.AGENT_CONTEXT_LIMIT) |
| 79 | except Exception as e: |
| 80 | if hasattr(agent, "model_name"): |
| 81 | model_name = agent.model_name |
| 82 | elif hasattr(agent, "name"): |
| 83 | model_name = agent.name |
| 84 | else: |
| 85 | model_name = agent.__class__.__name__ |
| 86 | print(f"ERROR: {model_name}/{self.name} agent error", e) |
| 87 | requests.post( |
| 88 | self.controller_address + "/cancel", |
| 89 | json=CancelRequest(session_id=sid).dict(), |
| 90 | ) |
| 91 | return TaskClientOutput( |
| 92 | error=TaskError.AGENT_FAILED.value, |
| 93 | info=str(e), |
| 94 | output=latest_result, |
| 95 | ) |
| 96 | |
| 97 | try: |
| 98 | result = requests.post( |
| 99 | self.controller_address + "/interact", |
| 100 | json=InteractRequest( |
| 101 | session_id=sid, |
| 102 | agent_response=response, |
| 103 | ).dict(), |
| 104 | ) |
| 105 | except Exception as e: |
| 106 | return TaskClientOutput( |
| 107 | error=TaskError.NETWORK_ERROR.value, |
| 108 | info=str(e), |
| 109 | output=latest_result, |
| 110 | ) |
| 111 | if result.status_code != 200: |
no test coverage detected