|
| 1 | +# Portkey Integration with AutoGen |
| 2 | + <img src="https://github.com/siddharthsambharia-portkey/Portkey-Product-Images/blob/main/Portkey-Autogen.png?raw=true" alt="Portkey Metrics Visualization" width=70% /> |
| 3 | + |
| 4 | +[Portkey](https://portkey.ai) is a 2-line upgrade to make your AutoGen agents reliable, cost-efficient, and fast. |
| 5 | + |
| 6 | +Portkey adds 4 core production capabilities to any AutoGen agent: |
| 7 | +1. Routing to 200+ LLMs |
| 8 | +2. Making each LLM call more robust |
| 9 | +3. Full-stack tracing & cost, performance analytics |
| 10 | +4. Real-time guardrails to enforce behavior |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +1. **Install Required Packages:** |
| 15 | +2. ```bash |
| 16 | + pip install -qU pyautogen portkey-ai |
| 17 | + ``` |
| 18 | + **Configure AutoGen with Portkey:** |
| 19 | + |
| 20 | + ```python |
| 21 | + from autogen import AssistantAgent, UserProxyAgent, config_list_from_json |
| 22 | + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders |
| 23 | + |
| 24 | + config = [ |
| 25 | + { |
| 26 | + "api_key": "OPENAI_API_KEY", |
| 27 | + "model": "gpt-3.5-turbo", |
| 28 | + "base_url": PORTKEY_GATEWAY_URL, |
| 29 | + "api_type": "openai", |
| 30 | + "default_headers": createHeaders( |
| 31 | + api_key="YOUR_PORTKEY_API_KEY", |
| 32 | + provider="openai", |
| 33 | + ) |
| 34 | + } |
| 35 | + ] |
| 36 | + ``` |
| 37 | + |
| 38 | + Generate your API key in the [Portkey Dashboard](https://app.portkey.ai/). |
| 39 | + |
| 40 | +And, that's it! With just this, you can start logging all of your AutoGen requests and make them reliable. |
| 41 | + |
| 42 | +3. **Let's Run your Agent** |
| 43 | + |
| 44 | +``` python |
| 45 | +import autogen |
| 46 | + |
| 47 | +# Create user proxy agent, coder, product manager |
| 48 | + |
| 49 | + |
| 50 | +user_proxy = autogen.UserProxyAgent( |
| 51 | + name="User_proxy", |
| 52 | + system_message="A human admin who will give the idea and run the code provided by Coder.", |
| 53 | + code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"}, |
| 54 | + human_input_mode="ALWAYS", |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +coder = autogen.AssistantAgent( |
| 59 | + name="Coder", |
| 60 | + system_message = "You are a Python developer who is good at developing games. You work with Product Manager.", |
| 61 | + llm_config={"config_list": config}, |
| 62 | +) |
| 63 | + |
| 64 | +# Create groupchat |
| 65 | +groupchat = autogen.GroupChat( |
| 66 | + agents=[user_proxy, coder], messages=[]) |
| 67 | +manager = autogen.GroupChatManager(groupchat=groupchat, llm_config={"config_list": config}) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +# Start the conversation |
| 72 | +user_proxy.initiate_chat( |
| 73 | + manager, message="Build a classic & basic pong game with 2 players in python") |
| 74 | +``` |
| 75 | +<br> |
| 76 | +Here’s the output from your Agent’s run on Portkey's dashboard<br> |
| 77 | +<img src=https://github.com/siddharthsambharia-portkey/Portkey-Product-Images/blob/main/Portkey-Dashboard.png?raw=true width=70%" alt="Portkey Dashboard" /> |
| 78 | + |
| 79 | +## Key Features |
| 80 | +Portkey offers a range of advanced features to enhance your AutoGen agents. Here’s an overview |
| 81 | + |
| 82 | +| Feature | Description | |
| 83 | +|---------|-------------| |
| 84 | +| 🌐 [Multi-LLM Integration](#interoperability) | Access 200+ LLMs with simple configuration changes | |
| 85 | +| 🛡️ [Enhanced Reliability](#reliability) | Implement fallbacks, load balancing, retries, and much more | |
| 86 | +| 📊 [Advanced Metrics](#metrics) | Track costs, tokens, latency, and 40+ custom metrics effortlessly | |
| 87 | +| 🔍 [Detailed Traces and Logs](#comprehensive-logging) | Gain insights into every agent action and decision | |
| 88 | +| 🚧 [Guardrails](#guardrails) | Enforce agent behavior with real-time checks on inputs and outputs | |
| 89 | +| 🔄 [Continuous Optimization](#continuous-improvement) | Capture user feedback for ongoing agent improvements | |
| 90 | +| 💾 [Smart Caching](#caching) | Reduce costs and latency with built-in caching mechanisms | |
| 91 | +| 🔐 [Enterprise-Grade Security](#security-and-compliance) | Set budget limits and implement fine-grained access controls | |
| 92 | + |
| 93 | + |
| 94 | +## Colab Notebook |
| 95 | + |
| 96 | +For a hands-on example of integrating Portkey with Autogen, check out our notebook<br> <br>[](https://git.new/Portkey-Autogen) . |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## Advanced Features |
| 101 | + |
| 102 | +### Interoperability |
| 103 | + |
| 104 | +Easily switch between **200+ LLMs** by changing the `provider` and API key in your configuration. |
| 105 | + |
| 106 | +#### Example: Switching from OpenAI to Azure OpenAI |
| 107 | + |
| 108 | +```python |
| 109 | +config = [ |
| 110 | + { |
| 111 | + "api_key": "api-key", |
| 112 | + "model": "gpt-3.5-turbo", |
| 113 | + "base_url": PORTKEY_GATEWAY_URL, |
| 114 | + "api_type": "openai", |
| 115 | + "default_headers": createHeaders( |
| 116 | + api_key="YOUR_PORTKEY_API_KEY", |
| 117 | + provider="azure-openai", |
| 118 | + virtual_key="AZURE_VIRTUAL_KEY" |
| 119 | + ) |
| 120 | + } |
| 121 | +] |
| 122 | +``` |
| 123 | +Note: AutoGen messages will go through Portkey's AI Gateway following OpenAI's API signature. Some language models may not work properly because messages need to be in a specific role order. |
| 124 | + |
| 125 | +### Reliability |
| 126 | + |
| 127 | +Implement fallbacks, load balancing, and automatic retries to make your agents more resilient. |
| 128 | + |
| 129 | +```python |
| 130 | +{ |
| 131 | + "strategy": { |
| 132 | + "mode": "fallback" # Options: "loadbalance" or "fallback" |
| 133 | + }, |
| 134 | + "targets": [ |
| 135 | + { |
| 136 | + "provider": "openai", |
| 137 | + "api_key": "openai-api-key", |
| 138 | + "override_params": { |
| 139 | + "top_k": "0.4", |
| 140 | + "max_tokens": "100" |
| 141 | + } |
| 142 | + }, |
| 143 | + { |
| 144 | + "provider": "anthropic", |
| 145 | + "api_key": "anthropic-api-key", |
| 146 | + "override_params": { |
| 147 | + "top_p": "0.6", |
| 148 | + "model": "claude-3-5-sonnet-20240620" |
| 149 | + } |
| 150 | + } |
| 151 | + ] |
| 152 | +} |
| 153 | +``` |
| 154 | +Learn more about [Portkey Config object here](https://docs.portkey.ai/docs/product/ai-gateway-streamline-llm-integrations/configs). |
| 155 | +Be Careful to Load-Balance/Fallback to providers that don't support tool calling when the request contains a function call. |
| 156 | +### Metrics |
| 157 | + |
| 158 | +Agent runs are complex. Portkey automatically logs **40+ comprehensive metrics** for your AI agents, including cost, tokens used, latency, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. |
| 159 | + |
| 160 | +<details> |
| 161 | + <summary><b>Portkey's Observability Dashboard</b></summary> |
| 162 | +<img src=https://github.com/siddharthsambharia-portkey/Portkey-Product-Images/blob/main/Portkey-Dashboard.png?raw=true width=70%" alt="Portkey Dashboard" /> |
| 163 | +</details> |
| 164 | + |
| 165 | +### Comprehensive Logging |
| 166 | + |
| 167 | +Access detailed logs and traces of agent activities, function calls, and errors. Filter logs based on multiple parameters for in-depth analysis. |
| 168 | + |
| 169 | +<details> |
| 170 | + <summary><b>Traces</b></summary> |
| 171 | + <img src="https://raw.githubusercontent.com/siddharthsambharia-portkey/Portkey-Product-Images/main/Portkey-Traces.png" alt="Portkey Logging Interface" width=70% /> |
| 172 | +</details> |
| 173 | + |
| 174 | +<details> |
| 175 | + <summary><b>Logs</b></summary> |
| 176 | + <img src="https://raw.githubusercontent.com/siddharthsambharia-portkey/Portkey-Product-Images/main/Portkey-Logs.png" alt="Portkey Metrics Visualization" width=70% /> |
| 177 | +</details> |
| 178 | + |
| 179 | +### Guardrails |
| 180 | +AutoGen agents, while powerful, can sometimes produce unexpected or undesired outputs. Portkey's Guardrails feature helps enforce agent behavior in real-time, ensuring your AutoGen agents operate within specified parameters. Verify both the **inputs** to and *outputs* from your agents to ensure they adhere to specified formats and content guidelines. Learn more about Portkey's Guardrails [here](https://docs.portkey.ai/product/guardrails) |
| 181 | + |
| 182 | +### Continuous Improvement |
| 183 | + |
| 184 | +Capture qualitative and quantitative user feedback on your requests to continuously enhance your agent performance. |
| 185 | + |
| 186 | +### Caching |
| 187 | + |
| 188 | +Reduce costs and latency with Portkey's built-in caching system. |
| 189 | + |
| 190 | +```python |
| 191 | +portkey_config = { |
| 192 | + "cache": { |
| 193 | + "mode": "semantic" # Options: "simple" or "semantic" |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +### Security and Compliance |
| 199 | + |
| 200 | +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both your application and the Portkey APIs. |
| 201 | + |
| 202 | +## Additional Resources |
| 203 | + |
| 204 | +- [📘 Portkey Documentation](https://docs.portkey.ai) |
| 205 | +- [🐦 Twitter](https://twitter.com/portkeyai) |
| 206 | +- [💬 Discord Community](https://discord.gg/JHPt4C7r) |
| 207 | +- [📊 Portkey App](https://app.portkey.ai) |
| 208 | + |
| 209 | +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). |
0 commit comments