You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
5
5
## Proxy Metadata
6
6
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.
8
8
9
-
For the Javascript library, here's the sample code
9
+
For the JavaScript library, use the following sample code:
10
10
11
11
```javascript
12
12
import { Configuration, OpenAIApi } from"openai";
@@ -16,54 +16,57 @@ const configuration = new Configuration({
16
16
basePath:"https://api.portkey.ai/v1/proxy", // Replace openai with portkey's endpoint
17
17
baseOptions: {
18
18
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
22
22
}
23
23
}
24
24
});
25
25
constopenai=newOpenAIApi(configuration);
26
26
```
27
27
28
-
For Python
28
+
For Python:
29
+
29
30
```python
30
31
import json
31
32
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
33
34
34
35
response = openai.ChatCompletion.create(
35
-
model="gpt-3.5-turbo",
36
-
messages=[
36
+
model="gpt-3.5-turbo",
37
+
messages=[
37
38
{"role": "system", "content": "You are a helpful assistant"},
38
-
{"role": "user", "content": "Create a 5day trip plan for Laos."},
39
+
{"role": "user", "content": "Create a 5-day trip plan for Laos."},
39
40
],
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
#### NOTE : When using the **_user** predefined key, the following behavior applies:
68
71
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