|
| 1 | +import base64 |
1 | 2 | from langtrace_python_sdk.utils.llm import ( |
2 | 3 | get_langtrace_attributes, |
3 | 4 | get_llm_request_attributes, |
|
14 | 15 |
|
15 | 16 | from typing import Iterator |
16 | 17 |
|
| 18 | +def capture_input_data(contents): |
| 19 | + input_data = [] |
| 20 | + |
| 21 | + if isinstance(contents, list): |
| 22 | + content_parts = [] |
| 23 | + for content in contents: |
| 24 | + if hasattr(content, 'parts'): |
| 25 | + for part in content.parts: |
| 26 | + if hasattr(part, 'text') and part.text: |
| 27 | + content_parts.append({ |
| 28 | + "type": "text", |
| 29 | + "text": part.text |
| 30 | + }) |
| 31 | + if hasattr(part, 'inline_data') and part.inline_data: |
| 32 | + content_parts.append({ |
| 33 | + "type": "image_url", |
| 34 | + "image_url": f"data:{part.inline_data.mime_type};base64,{base64.b64encode(part.inline_data.data).decode('utf-8')}" |
| 35 | + }) |
| 36 | + else: |
| 37 | + if isinstance(content, str): |
| 38 | + content_parts.append({ |
| 39 | + "type": "text", |
| 40 | + "text": content |
| 41 | + }) |
| 42 | + elif hasattr(content, 'text') and content.text: |
| 43 | + content_parts.append({ |
| 44 | + "type": "text", |
| 45 | + "text": content.text |
| 46 | + }) |
| 47 | + elif hasattr(content, 'inline_data') and content.inline_data: |
| 48 | + content_parts.append({ |
| 49 | + "type": "image_url", |
| 50 | + "image_url": f"data:{content.inline_data.mime_type};base64,{base64.b64encode(content.inline_data.data).decode('utf-8')}" |
| 51 | + }) |
| 52 | + else: |
| 53 | + content_parts.append({ |
| 54 | + "type": "text", |
| 55 | + "text": content |
| 56 | + }) |
| 57 | + input_data.append({ |
| 58 | + "role": "user", |
| 59 | + "content": content_parts |
| 60 | + }) |
| 61 | + else: |
| 62 | + input_data.append({ |
| 63 | + "role": "user", |
| 64 | + "content": contents |
| 65 | + }) |
| 66 | + |
| 67 | + return input_data |
| 68 | + |
17 | 69 |
|
18 | 70 | def patch_google_genai(tracer: Tracer, version: str): |
19 | 71 | def traced_method(wrapped, instance, args, kwargs): |
20 | | - prompt = [ |
21 | | - { |
22 | | - "role": "user", |
23 | | - "content": kwargs["contents"], |
24 | | - } |
25 | | - ] |
26 | 72 | span_attributes = { |
27 | 73 | **get_langtrace_attributes( |
28 | 74 | service_provider="google_genai", version=version |
29 | 75 | ), |
30 | | - **get_llm_request_attributes(kwargs=kwargs, prompts=prompt), |
| 76 | + **get_llm_request_attributes(kwargs=kwargs, prompts=capture_input_data(kwargs["contents"])), |
31 | 77 | } |
32 | 78 | with tracer.start_as_current_span( |
33 | 79 | name="google.genai.generate_content", |
|
0 commit comments