This repository was archived by the owner on Dec 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathextra_headers_demo.py
More file actions
167 lines (138 loc) · 5.2 KB
/
extra_headers_demo.py
File metadata and controls
167 lines (138 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Example demonstrating how to use custom headers on a per-request basis with the Gemini API.
This is particularly useful when working with proxy services like Helicone, Traceloop, or LiteLLM.
"""
import os
import google.generativeai as genai
from google.generativeai.types import RequestOptions
# Configure the Gemini API with the provided API key
API_KEY = "AIzaSyBgUvB3zMMxGcjSJmMYVRD1ILiUcjxxAvQ"
genai.configure(api_key=API_KEY)
# Create a model
model = genai.GenerativeModel('gemini-1.5-flash')
def simple_example():
"""Example using simple standard HTTP headers."""
print("\n=== Simple Headers Example ===")
# Basic request without custom headers
response1 = model.generate_content(
'Tell me a joke about programming in one sentence.'
)
print(f"Response without headers: {response1.text}")
# Request with custom headers
response2 = model.generate_content(
'Tell me a joke about cats in one sentence.',
request_options=RequestOptions(
extra_headers=[
('x-custom-user-id', 'user123'),
('x-custom-session', 'session456')
]
)
)
print(f"Response with custom headers: {response2.text}")
def custom_headers_example():
"""Example using custom headers."""
print("\n=== Custom Headers Example ===")
response = model.generate_content(
'Explain the concept of machine learning in one sentence.',
request_options=RequestOptions(
extra_headers=[
('x-tracking-id', 'track789'),
('x-client-version', '1.0.0')
]
)
)
print(f"Response with tracking headers: {response.text}")
def multiple_requests_example():
"""Example demonstrating multiple requests with different headers."""
print("\n=== Multiple Requests Example ===")
# First request with one set of headers
print("Request 1:")
response1 = model.generate_content(
'What is the capital of France?',
request_options=RequestOptions(
extra_headers=[
('x-request-id', 'req1'),
('x-user-id', 'user-a')
]
)
)
print(f"Response: {response1.text}")
# Second request with different headers
print("\nRequest 2:")
response2 = model.generate_content(
'What is the capital of Italy?',
request_options=RequestOptions(
extra_headers=[
('x-request-id', 'req2'),
('x-user-id', 'user-b')
]
)
)
print(f"Response: {response2.text}")
# Third request with no headers
print("\nRequest 3 (no custom headers):")
response3 = model.generate_content(
'What is the capital of Germany?'
)
print(f"Response: {response3.text}")
def timeout_example():
"""Example with timeout in RequestOptions along with headers."""
print("\n=== Timeout with Headers Example ===")
response = model.generate_content(
'Write a haiku about coding.',
request_options=RequestOptions(
timeout=30, # 30 seconds timeout
extra_headers=[
('x-request-source', 'example-script'),
('x-request-type', 'haiku')
]
)
)
print(f"Response with timeout and headers: {response.text}")
def count_tokens_example():
"""Example showing custom headers with count_tokens."""
print("\n=== Count Tokens with Headers Example ===")
# Count tokens without custom headers
content = "This is a test sentence to count tokens. It should have more than a few tokens."
token_count1 = model.count_tokens(content)
print(f"Token count without headers: {token_count1.total_tokens}")
# Count tokens with custom headers
token_count2 = model.count_tokens(
content,
request_options=RequestOptions(
extra_headers=[
('x-token-count-request-id', 'count123'),
('x-analytics-source', 'example-script')
]
)
)
print(f"Token count with headers: {token_count2.total_tokens}")
# The token counts should be the same, showing that the headers don't affect the functionality
print(f"Token counts match: {token_count1.total_tokens == token_count2.total_tokens}")
def main():
print("=== Per-Request Headers Examples ===")
print("Demonstrating how to use custom headers on a per-request basis")
# Run examples
simple_example()
custom_headers_example()
multiple_requests_example()
timeout_example()
count_tokens_example()
if __name__ == "__main__":
main()