|
| 1 | +# Codegen Agents - Python SDK |
| 2 | + |
| 3 | +This module provides a Python client for interacting with the Codegen AI agents API. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The Codegen Agent SDK is included as part of the Codegen package. Ensure you have the latest version installed: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install codegen |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Basic Example |
| 16 | + |
| 17 | +```python |
| 18 | +from codegen.agents.agent import Agent |
| 19 | + |
| 20 | +# Initialize the Agent with your organization ID and API token |
| 21 | +agent = Agent( |
| 22 | + org_id="11", # Your organization ID |
| 23 | + token="your_api_token_here", # Your API authentication token |
| 24 | + base_url="https://codegen-sh-rest-api.modal.run" # Optional - defaults to this URL |
| 25 | +) |
| 26 | + |
| 27 | +# Run an agent with a prompt |
| 28 | +task = agent.run(prompt="Which github repos can you currently access?") |
| 29 | + |
| 30 | +# Check the initial status |
| 31 | +print(task.status) # Returns the current status of the task (e.g., "queued", "in_progress", etc.) |
| 32 | + |
| 33 | +# Refresh the task to get updated status |
| 34 | +task.refresh() |
| 35 | + |
| 36 | +# Check the updated status |
| 37 | +print(task.status) |
| 38 | + |
| 39 | +# Once task is complete, you can access the result |
| 40 | +if task.status == "completed": |
| 41 | + print(task.result) |
| 42 | +``` |
| 43 | + |
| 44 | +### Agent Class |
| 45 | + |
| 46 | +The `Agent` class is the main entry point for interacting with Codegen AI agents: |
| 47 | + |
| 48 | +```python |
| 49 | +Agent(token: str, org_id: Optional[int] = None, base_url: Optional[str] = CODEGEN_BASE_API_URL) |
| 50 | +``` |
| 51 | + |
| 52 | +Parameters: |
| 53 | +- `token` (required): Your API authentication token |
| 54 | +- `org_id` (optional): Your organization ID. If not provided, defaults to environment variable `CODEGEN_ORG_ID` or "1" |
| 55 | +- `base_url` (optional): API base URL. Defaults to "https://codegen-sh-rest-api.modal.run" |
| 56 | + |
| 57 | +### Methods |
| 58 | + |
| 59 | +#### run() |
| 60 | + |
| 61 | +```python |
| 62 | +run(prompt: str) -> AgentTask |
| 63 | +``` |
| 64 | + |
| 65 | +Runs an agent with the given prompt. |
| 66 | + |
| 67 | +Parameters: |
| 68 | +- `prompt` (required): The instruction for the agent to execute |
| 69 | + |
| 70 | +Returns: |
| 71 | +- An `AgentTask` object representing the running task |
| 72 | + |
| 73 | +#### get_status() |
| 74 | + |
| 75 | +```python |
| 76 | +get_status() -> Optional[Dict[str, Any]] |
| 77 | +``` |
| 78 | + |
| 79 | +Gets the status of the current task. |
| 80 | + |
| 81 | +Returns: |
| 82 | +- A dictionary containing task status information (`id`, `status`, `result`), or `None` if no task has been run |
| 83 | + |
| 84 | +### AgentTask Class |
| 85 | + |
| 86 | +The `AgentTask` class represents a running or completed agent task: |
| 87 | + |
| 88 | +#### Attributes |
| 89 | + |
| 90 | +- `id`: The unique identifier for the task |
| 91 | +- `org_id`: The organization ID |
| 92 | +- `status`: Current status of the task (e.g., "queued", "in_progress", "completed", "failed") |
| 93 | +- `result`: The task result (available when status is "completed") |
| 94 | + |
| 95 | +#### Methods |
| 96 | + |
| 97 | +##### refresh() |
| 98 | + |
| 99 | +```python |
| 100 | +refresh() -> None |
| 101 | +``` |
| 102 | + |
| 103 | +Refreshes the task status from the API. |
| 104 | + |
| 105 | +## Environment Variables |
| 106 | + |
| 107 | +- `CODEGEN_ORG_ID`: Default organization ID (used if `org_id` is not provided) |
| 108 | + |
| 109 | +## Error Handling |
| 110 | + |
| 111 | +Handle potential API errors using standard try/except blocks: |
| 112 | + |
| 113 | +```python |
| 114 | +try: |
| 115 | + task = agent.run(prompt="Your prompt here") |
| 116 | + task.refresh() |
| 117 | + print(task.status) |
| 118 | +except Exception as e: |
| 119 | + print(f"Error: {e}") |
| 120 | +``` |
0 commit comments