Skip to content

Commit 93315fe

Browse files
Update metadata.md
1 parent ff96fe9 commit 93315fe

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

metadata.md

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Sending metadata along with your requests
1+
# Sending Metadata Along with Your Requests
22

3-
You can send custom metadata along with your API requests which can be later be used for filtering and aggregations.
3+
You can send custom metadata along with your API requests in Portkey, which can be later used for auditing or filtering logs. Portkey provides four predefined keys: `_environment`, `_user`, `_organisation`, and `_prompt`. These predefined keys are indexed and allow for filtering data in Portkey analytics and logs sections. You can still pass any other metadata key you desire, but these four predefined keys will be indexed and will be available for filtering data in Portkey.
44

55
## Proxy Metadata
66

7-
To include metadata in the proxy requests, you can add a `x-portkey-meta` header with a JSON string in it. Portkey will parse the JSON object and make it available for filtering.
7+
To include metadata in the proxy requests, you can add an `x-portkey-meta` header with a JSON string containing your metadata. Portkey will parse the JSON object and make it available for filtering.
88

9-
For the Javascript library, here's the sample code
9+
For the JavaScript library, use the following sample code:
1010

1111
```javascript
1212
import { Configuration, OpenAIApi } from "openai";
@@ -16,54 +16,57 @@ const configuration = new Configuration({
1616
basePath: "https://api.portkey.ai/v1/proxy", // Replace openai with portkey's endpoint
1717
baseOptions: {
1818
headers: {
19-
"x-portkey-api-key": "<YOUR PORTKEY API KEY>", // Can be got from your account
20-
"x-portkey-mode": "proxy openai", // Tells portkey to proxy your request to openai
21-
"x-portkey-meta": JSON.stringify({"foo": "abc", "bar": "def"}) // Enables filtering on `foo` and `bar`
19+
"x-portkey-api-key": "<YOUR PORTKEY API KEY>", // Can be obtained from your account
20+
"x-portkey-mode": "proxy openai", // Instructs Portkey to proxy your request to OpenAI
21+
"x-portkey-meta": JSON.stringify({"_environment": "production", "foo": "abc", "bar": "def"}) //Enables filtering on _environment
2222
}
2323
}
2424
});
2525
const openai = new OpenAIApi(configuration);
2626
```
2727

28-
For Python
28+
For Python:
29+
2930
```python
3031
import json
3132

32-
openai.api_base = "https://api.portkey.ai/v1/proxy" # Replace openai with portkey's endpoint
33+
openai.api_base = "https://api.portkey.ai/v1/proxy" # Replace openai with Portkey's endpoint
3334

3435
response = openai.ChatCompletion.create(
35-
model="gpt-3.5-turbo",
36-
messages=[
36+
model="gpt-3.5-turbo",
37+
messages=[
3738
{"role": "system", "content": "You are a helpful assistant"},
38-
{"role": "user", "content": "Create a 5 day trip plan for Laos."},
39+
{"role": "user", "content": "Create a 5-day trip plan for Laos."},
3940
],
40-
temperature=0.2,
41-
headers={ # Add portkey headers for auth and proxy mode
42-
"x-portkey-api-key": "<YOUR PORTKEY API KEY>",
43-
"x-portkey-mode": "proxy openai",
44-
"x-portkey-meta": json.dumps({"foo": "abc", "bar": "def"}) # Enables filtering on `foo` and `bar`
45-
}
41+
temperature=0.2,
42+
headers={ # Add Portkey headers for authentication and proxy mode
43+
"x-portkey-api-key": "<YOUR PORTKEY API KEY>",
44+
"x-portkey-mode": "proxy openai",
45+
"x-portkey-meta": json.dumps({"_environment": "production", "foo": "abc", "bar": "def"}) # Enables filtering on _environment
46+
}
4647
)
4748
```
4849

49-
### 2.3 Using Langchain
50-
Add the following code in your langchain initialisation to enable portkey.
50+
### Using Langchain
51+
52+
To enable Portkey in your Langchain initialization, add the following code:
5153

5254
```python
5355
from langchain.llms import OpenAI
5456
import openai
5557
import json
58+
5659
openai.api_base = "https://api.portkey.ai/v1/proxy"
5760

5861
llm = OpenAI(temperature=0.2, headers={
5962
"x-portkey-api-key": "<YOUR PORTKEY API KEY>",
6063
"x-portkey-mode": "proxy openai",
61-
"x-portkey-meta": json.dumps({"foo": "abc", "bar": "def"}) # Enables filtering on `foo` and `bar`
64+
"x-portkey-meta": json.dumps({"_environment": "production", "foo": "abc", "bar": "def"}) # Enables filtering on _environment
6265
})
63-
text = "Create a 5 day trip plan for Laos."
66+
text = "Create a 5-day trip plan for Laos."
6467
print(llm(text))
6568
```
6669

67-
## Model completion metadata
70+
#### NOTE : When using the **_user** predefined key, the following behavior applies:
6871

69-
_Coming soon_
72+
If you pass the `user` key in the OpenAI request body, it will be automatically stored in `_user`. If both the OpenAI request body `user` key and the metadata `_user` key are passed, the metadata `_user` key will be stored.

0 commit comments

Comments
 (0)