|
| 1 | +# Automatic Retries with Portkey |
| 2 | + |
| 3 | +Portkey has an in-built retry mechanism that makes sure your requests get fulfilled. We implement a retry mechanism with exponential backoff. This can be managed by the `x-portkey-retry-count` header. |
| 4 | + |
| 5 | +``` |
| 6 | +HEADER: |
| 7 | +x-portkey-retry-count: "number of times you want the API to be retried (max 5)" |
| 8 | +``` |
| 9 | + |
| 10 | +### Adding Retries to any proxy request |
| 11 | +Pass `x-portkey-retry-count` in the request header for Portkey to start retrying requests. |
| 12 | + |
| 13 | +For Python |
| 14 | +```python |
| 15 | +response = openai.ChatCompletion.create( |
| 16 | + model="text-davinci-003", |
| 17 | + prompt="Create a 5 day trip plan for Laos." |
| 18 | + temperature=0.2, |
| 19 | + headers={ |
| 20 | + "x-portkey-api-key": "<YOUR PORTKEY API KEY>", |
| 21 | + "x-portkey-mode": "proxy openai", |
| 22 | + "x-portkey-retry-count": "3" |
| 23 | + } |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +For Javascript |
| 28 | +```javascript |
| 29 | +const configuration = new Configuration({ |
| 30 | + organization: "YOUR_ORG_ID", |
| 31 | + apiKey: process.env.OPENAI_API_KEY, |
| 32 | + basePath: "https://api.portkey.ai/v1/proxy", |
| 33 | + baseOptions: { |
| 34 | + headers: { |
| 35 | + "x-portkey-api-key": "<YOUR PORTKEY API KEY>", |
| 36 | + "x-portkey-mode": "proxy openai" |
| 37 | + "x-portkey-retry-count": "3" // Add the number of retries you want Portkey to make |
| 38 | + } |
| 39 | + } |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +### Using Tracing with Langchain Agents |
| 44 | +A common use case of tracing is to capture the flow of a langchain agent request. When using Portkey with langchain, you can pass your Trace ID in the headers and all the embeddings & completion requests will get tagged with the same Trace ID. |
| 45 | + |
| 46 | +Sample Langchain Agent Code with Tracing Enabled |
| 47 | +```python |
| 48 | +rom langchain.agents import load_tools |
| 49 | +from langchain.agents import initialize_agent |
| 50 | +from langchain.agents import AgentType |
| 51 | +from langchain.llms import OpenAI |
| 52 | +import openai |
| 53 | + |
| 54 | +openai.api_base = "https://api.portkey.ai/v1/proxy" |
| 55 | + |
| 56 | +llm = OpenAI(temperature=0, headers={ |
| 57 | + "x-portkey-api-key": "<PORTKEY API KEY>", |
| 58 | + "x-portkey-mode": "proxy openai", |
| 59 | + "x-portkey-retry-count": "3" |
| 60 | +}, user="testing_traces") |
| 61 | + |
| 62 | +tools = load_tools(["llm-math"], llm=llm) |
| 63 | + |
| 64 | +agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) |
| 65 | + |
| 66 | +agent.run("What is 56 multiplied by 17654 to the 0.43 power?") |
| 67 | +``` |
| 68 | + |
| 69 | +Note: The max value of retries available is 5. |
0 commit comments