|
8 | 8 | } |
9 | 9 | }, |
10 | 10 | "source": [ |
11 | | - "# Oracle OCI Generative AI Tracing with Openlayer\n", |
| 11 | + "[](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/oci/oci_genai_tracing.ipynb)\n", |
12 | 12 | "\n", |
13 | | - "This notebook demonstrates how to use Openlayer tracing with Oracle Cloud Infrastructure (OCI) Generative AI service.\n", |
| 13 | + "# <a id=\"top\">Oracle OCI Generative AI Tracing with Openlayer</a>\n", |
14 | 14 | "\n", |
15 | | - "## Setup\n", |
16 | | - "\n", |
17 | | - "Before running this notebook, ensure you have:\n", |
18 | | - "1. An OCI account with access to Generative AI service\n", |
19 | | - "2. OCI CLI configured or OCI config file set up\n", |
20 | | - "3. An Openlayer account with API key and inference pipeline ID\n", |
21 | | - "4. The required packages installed:\n", |
22 | | - " - `pip install oci`\n", |
23 | | - " - `pip install openlayer`\n", |
24 | | - "\n", |
25 | | - "## Configuration\n", |
26 | | - "\n", |
27 | | - "### Openlayer Setup\n", |
28 | | - "Set these environment variables before running:\n", |
29 | | - "```bash\n", |
30 | | - "export OPENLAYER_API_KEY=\"your-api-key\"\n", |
31 | | - "export OPENLAYER_INFERENCE_PIPELINE_ID=\"your-pipeline-id\"\n", |
32 | | - "```\n", |
33 | | - "\n", |
34 | | - "### OCI Setup\n", |
35 | | - "Make sure your OCI configuration is properly set up. You can either:\n", |
36 | | - "- Use the default OCI config file (`~/.oci/config`)\n", |
37 | | - "- Set up environment variables\n", |
38 | | - "- Use instance principal authentication (when running on OCI compute)\n" |
| 15 | + "This notebook demonstrates how to use Openlayer tracing with Oracle Cloud Infrastructure (OCI) Generative AI service.\n" |
39 | 16 | ] |
40 | 17 | }, |
41 | 18 | { |
|
44 | 21 | "metadata": {}, |
45 | 22 | "outputs": [], |
46 | 23 | "source": [ |
47 | | - "# Install required packages (uncomment if needed)\n", |
48 | | - "# !pip install oci openlayer\n", |
49 | | - "\n", |
50 | | - "# Set up Openlayer environment variables\n", |
51 | | - "import os\n", |
52 | | - "\n", |
53 | | - "# Configure Openlayer API credentials\n", |
54 | | - "os.environ[\"OPENLAYER_API_KEY\"] = \"your-openlayer-api-key-here\"\n", |
55 | | - "os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"your-inference-pipeline-id-here\"\n", |
56 | | - "\n", |
57 | | - "# NOTE: Remember to set your actual Openlayer API key and inference pipeline ID!" |
58 | | - ] |
59 | | - }, |
60 | | - { |
61 | | - "cell_type": "code", |
62 | | - "execution_count": null, |
63 | | - "metadata": {}, |
64 | | - "outputs": [], |
65 | | - "source": [ |
66 | | - "import oci\n", |
67 | | - "from oci.generative_ai_inference import GenerativeAiInferenceClient\n", |
68 | | - "from oci.generative_ai_inference.models import Message, ChatDetails, GenericChatRequest\n", |
69 | | - "\n", |
70 | | - "# Import the Openlayer tracer\n", |
71 | | - "from openlayer.lib.integrations import trace_oci_genai" |
| 24 | + "!pip install openlayer oci" |
72 | 25 | ] |
73 | 26 | }, |
74 | 27 | { |
75 | 28 | "cell_type": "markdown", |
76 | | - "metadata": { |
77 | | - "vscode": { |
78 | | - "languageId": "raw" |
79 | | - } |
80 | | - }, |
| 29 | + "metadata": {}, |
81 | 30 | "source": [ |
82 | | - "## Initialize OCI Client\n", |
83 | | - "\n", |
84 | | - "Set up the OCI Generative AI client with your configuration.\n" |
| 31 | + "## 1. Set the environment variables" |
85 | 32 | ] |
86 | 33 | }, |
87 | 34 | { |
|
90 | 37 | "metadata": {}, |
91 | 38 | "outputs": [], |
92 | 39 | "source": [ |
93 | | - "# Configuration - Update these values for your environment\n", |
94 | | - "COMPARTMENT_ID = \"your-compartment-ocid-here\" # Replace with your compartment OCID\n", |
95 | | - "ENDPOINT = \"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\" # Replace with your region's endpoint\n", |
96 | | - "\n", |
97 | | - "# Load OCI configuration\n", |
98 | | - "config = oci.config.from_file() # Uses default config file location\n", |
99 | | - "# Alternatively, you can specify a custom config file:\n", |
100 | | - "# config = oci.config.from_file(\"~/.oci/config\", \"DEFAULT\")\n", |
| 40 | + "import os\n", |
101 | 41 | "\n", |
102 | | - "# Create the OCI Generative AI client\n", |
103 | | - "client = GenerativeAiInferenceClient(config=config, service_endpoint=ENDPOINT)\n" |
| 42 | + "# Configure Openlayer API credentials\n", |
| 43 | + "os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n", |
| 44 | + "os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_INFERENCE_PIPELINE_ID_HERE\"" |
104 | 45 | ] |
105 | 46 | }, |
106 | 47 | { |
107 | 48 | "cell_type": "markdown", |
108 | | - "metadata": { |
109 | | - "vscode": { |
110 | | - "languageId": "raw" |
111 | | - } |
112 | | - }, |
| 49 | + "metadata": {}, |
113 | 50 | "source": [ |
114 | | - "## Apply Openlayer Tracing\n", |
115 | | - "\n", |
116 | | - "Wrap the OCI client with Openlayer tracing to automatically capture all interactions.\n", |
117 | | - "\n", |
118 | | - "The `trace_oci_genai()` function accepts an optional `estimate_tokens` parameter:\n", |
119 | | - "- `estimate_tokens=True` (default): Estimates token counts when not provided by OCI response\n", |
120 | | - "- `estimate_tokens=False`: Returns None for token fields when not available in the response\n", |
121 | | - "\n", |
122 | | - "OCI responses can be either CohereChatResponse or GenericChatResponse, both containing usage information when available.\n" |
| 51 | + "## 2. Wrap the OCI Generative AI client with the `trace_oci_genai` function" |
123 | 52 | ] |
124 | 53 | }, |
125 | 54 | { |
|
128 | 57 | "metadata": {}, |
129 | 58 | "outputs": [], |
130 | 59 | "source": [ |
131 | | - "# Apply Openlayer tracing to the OCI client\n", |
132 | | - "# With token estimation enabled (default)\n", |
133 | | - "traced_client = trace_oci_genai(client, estimate_tokens=True)\n", |
| 60 | + "import oci\n", |
| 61 | + "from oci.generative_ai_inference import GenerativeAiInferenceClient\n", |
| 62 | + "from oci.generative_ai_inference.models import (\n", |
| 63 | + " Message,\n", |
| 64 | + " ChatDetails,\n", |
| 65 | + " TextContent,\n", |
| 66 | + " BaseChatRequest,\n", |
| 67 | + " GenericChatRequest,\n", |
| 68 | + " OnDemandServingMode,\n", |
| 69 | + ")\n", |
134 | 70 | "\n", |
135 | | - "# Alternative: Disable token estimation to get None values when tokens are not available\n", |
136 | | - "# traced_client = trace_oci_genai(client, estimate_tokens=False)" |
137 | | - ] |
138 | | - }, |
139 | | - { |
140 | | - "cell_type": "markdown", |
141 | | - "metadata": { |
142 | | - "vscode": { |
143 | | - "languageId": "raw" |
144 | | - } |
145 | | - }, |
146 | | - "source": [ |
147 | | - "## Example 1: Non-Streaming Chat Completion\n", |
| 71 | + "# Point to your real files\n", |
| 72 | + "OCI_CONFIG_PATH = \"YOUR_OCI_CONFIG_PATH_HERE\"\n", |
| 73 | + "OCI_PROFILE = \"DEFAULT\"\n", |
| 74 | + "\n", |
| 75 | + "ENDPOINT = \"YOUR_ENDPOINT_HERE\"\n", |
| 76 | + "COMPARTMENT_ID = \"YOUR_COMPARTMENT_OCID_HERE\"\n", |
| 77 | + "MODEL_ID = \"YOUR_MODEL_OCID_HERE\"\n", |
148 | 78 | "\n", |
149 | | - "Simple chat completion without streaming.\n" |
| 79 | + "\n", |
| 80 | + "config = oci.config.from_file(OCI_CONFIG_PATH, OCI_PROFILE)\n", |
| 81 | + "oci.config.validate_config(config)\n" |
150 | 82 | ] |
151 | 83 | }, |
152 | 84 | { |
|
155 | 87 | "metadata": {}, |
156 | 88 | "outputs": [], |
157 | 89 | "source": [ |
158 | | - "# Create a chat request\n", |
159 | | - "chat_request = GenericChatRequest(\n", |
160 | | - " messages=[Message(role=\"user\", content=\"Hello! Can you explain what Oracle Cloud Infrastructure is?\")],\n", |
161 | | - " model_id=\"cohere.command-r-plus\",\n", |
162 | | - " max_tokens=200,\n", |
163 | | - " temperature=0.7,\n", |
164 | | - " is_stream=False, # Non-streaming\n", |
165 | | - ")\n", |
166 | | - "\n", |
167 | | - "chat_details = ChatDetails(compartment_id=COMPARTMENT_ID, chat_request=chat_request)\n", |
168 | | - "\n", |
169 | | - "# Make the request - the tracer will automatically capture it\n", |
170 | | - "response = traced_client.chat(chat_details)\n", |
171 | | - "response" |
| 90 | + "# Import the Openlayer tracer\n", |
| 91 | + "from openlayer.lib.integrations import trace_oci_genai\n", |
| 92 | + "\n", |
| 93 | + "client = trace_oci_genai(GenerativeAiInferenceClient(\n", |
| 94 | + " config=config,\n", |
| 95 | + " service_endpoint=ENDPOINT,\n", |
| 96 | + " retry_strategy=oci.retry.NoneRetryStrategy(),\n", |
| 97 | + " timeout=(10, 120),\n", |
| 98 | + "), estimate_tokens=True)" |
172 | 99 | ] |
173 | 100 | }, |
174 | 101 | { |
175 | 102 | "cell_type": "markdown", |
176 | | - "metadata": { |
177 | | - "vscode": { |
178 | | - "languageId": "raw" |
179 | | - } |
180 | | - }, |
| 103 | + "metadata": {}, |
181 | 104 | "source": [ |
182 | | - "## Example 2: Streaming Chat Completion\n", |
| 105 | + "## 3. Use the traced OCI Generative AI client normally\n", |
183 | 106 | "\n", |
184 | | - "Chat completion with streaming enabled to see tokens as they're generated.\n" |
| 107 | + "That's it! Now you can continue using the traced OpenAI client normally. The data is automatically published to Openlayer and you can start creating tests around it!" |
185 | 108 | ] |
186 | 109 | }, |
187 | 110 | { |
|
190 | 113 | "metadata": {}, |
191 | 114 | "outputs": [], |
192 | 115 | "source": [ |
193 | | - "# Create a streaming chat request\n", |
194 | | - "streaming_chat_request = GenericChatRequest(\n", |
| 116 | + "chat_request = GenericChatRequest(\n", |
| 117 | + " api_format=BaseChatRequest.API_FORMAT_GENERIC,\n", |
195 | 118 | " messages=[\n", |
196 | | - " Message(role=\"system\", content=\"You are a helpful AI assistant that provides concise, informative answers.\"),\n", |
197 | | - " Message(role=\"user\", content=\"Tell me a short story about cloud computing and AI working together.\"),\n", |
| 119 | + " Message(\n", |
| 120 | + " role=\"USER\",\n", |
| 121 | + " content=[TextContent(text=\"What is OCI in one sentence?\")]\n", |
| 122 | + " )\n", |
198 | 123 | " ],\n", |
199 | | - " model_id=\"meta.llama-3.1-70b-instruct\",\n", |
200 | | - " max_tokens=300,\n", |
201 | | - " temperature=0.8,\n", |
202 | | - " is_stream=True, # Enable streaming\n", |
| 124 | + " max_tokens=100,\n", |
| 125 | + " temperature=0.2,\n", |
| 126 | + " is_stream=False,\n", |
203 | 127 | ")\n", |
204 | 128 | "\n", |
205 | | - "streaming_chat_details = ChatDetails(compartment_id=COMPARTMENT_ID, chat_request=streaming_chat_request)\n", |
206 | | - "\n", |
207 | | - "# Make the streaming request\n", |
208 | | - "streaming_response = traced_client.chat(streaming_chat_details)\n", |
209 | | - "\n", |
210 | | - "# Process the streaming response\n", |
211 | | - "full_content = \"\"\n", |
212 | | - "for chunk in streaming_response:\n", |
213 | | - " if hasattr(chunk, \"data\") and hastr(chunk.data, \"choices\"):\n", |
214 | | - " if chunk.data.choices and hasattr(chunk.data.choices[0], \"delta\"):\n", |
215 | | - " delta = chunk.data.choices[0].delta\n", |
216 | | - " if hasattr(delta, \"content\") and delta.content:\n", |
217 | | - " full_content += delta.content\n", |
218 | | - "\n", |
219 | | - "full_content" |
| 129 | + "chat_details = ChatDetails(\n", |
| 130 | + " serving_mode=OnDemandServingMode(model_id=MODEL_ID),\n", |
| 131 | + " chat_request=chat_request,\n", |
| 132 | + " compartment_id=COMPARTMENT_ID,\n", |
| 133 | + ")\n", |
| 134 | + "\n" |
220 | 135 | ] |
221 | 136 | }, |
222 | 137 | { |
223 | | - "cell_type": "markdown", |
224 | | - "metadata": { |
225 | | - "vscode": { |
226 | | - "languageId": "raw" |
227 | | - } |
228 | | - }, |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
229 | 142 | "source": [ |
230 | | - "## Example 3: Custom Parameters and Error Handling\n", |
| 143 | + "resp = client.chat(chat_details)\n", |
231 | 144 | "\n", |
232 | | - "Demonstrate various model parameters and how tracing works with different scenarios.\n" |
| 145 | + "answer = \"\"\n", |
| 146 | + "choice = resp.data.chat_response.choices[0]\n", |
| 147 | + "for part in choice.message.content:\n", |
| 148 | + " if hasattr(part, \"text\"):\n", |
| 149 | + " answer += part.text" |
233 | 150 | ] |
234 | 151 | }, |
235 | 152 | { |
|
238 | 155 | "metadata": {}, |
239 | 156 | "outputs": [], |
240 | 157 | "source": [ |
241 | | - "# Advanced parameters example\n", |
242 | | - "advanced_request = GenericChatRequest(\n", |
243 | | - " messages=[Message(role=\"user\", content=\"Write a creative haiku about artificial intelligence.\")],\n", |
244 | | - " model_id=\"meta.llama-3.1-70b-instruct\",\n", |
245 | | - " max_tokens=100,\n", |
246 | | - " temperature=0.9, # High creativity\n", |
247 | | - " top_p=0.8,\n", |
248 | | - " frequency_penalty=0.2, # Reduce repetition\n", |
249 | | - " presence_penalty=0.1,\n", |
250 | | - " stop=[\"\\n\\n\"], # Stop at double newline\n", |
251 | | - " is_stream=False,\n", |
252 | | - ")\n", |
253 | | - "\n", |
254 | | - "advanced_details = ChatDetails(compartment_id=COMPARTMENT_ID, chat_request=advanced_request)\n", |
255 | | - "\n", |
256 | | - "response = traced_client.chat(advanced_details)\n", |
257 | | - "response" |
| 158 | + "answer" |
258 | 159 | ] |
259 | 160 | } |
260 | 161 | ], |
261 | 162 | "metadata": { |
| 163 | + "kernelspec": { |
| 164 | + "display_name": "oracle", |
| 165 | + "language": "python", |
| 166 | + "name": "python3" |
| 167 | + }, |
262 | 168 | "language_info": { |
263 | | - "name": "python" |
| 169 | + "codemirror_mode": { |
| 170 | + "name": "ipython", |
| 171 | + "version": 3 |
| 172 | + }, |
| 173 | + "file_extension": ".py", |
| 174 | + "mimetype": "text/x-python", |
| 175 | + "name": "python", |
| 176 | + "nbconvert_exporter": "python", |
| 177 | + "pygments_lexer": "ipython3", |
| 178 | + "version": "3.12.12" |
264 | 179 | } |
265 | 180 | }, |
266 | 181 | "nbformat": 4, |
|
0 commit comments