-
Notifications
You must be signed in to change notification settings - Fork 17
add LlamaIndex zero-code OTLP example #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mesutoezdil
wants to merge
4
commits into
agentevals-dev:main
Choose a base branch
from
mesutoezdil:feature/llama-index-zero-code-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f456c3a
feat(examples): add LlamaIndex zero-code OTLP example
mesutoezdil 6167e74
fix: use standard system prompt in llama-index example
mesutoezdil 406d139
Merge remote-tracking branch 'origin/main' into feature/llama-index-z…
mesutoezdil c7d8721
fix: align system prompt with other zero-code examples
mesutoezdil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| llama-index-core>=0.14.0 | ||
| llama-index-llms-openai-like>=0.3.0 | ||
| llama-index-observability-otel>=0.1.0 | ||
|
|
||
| opentelemetry-sdk>=1.36.0 | ||
| opentelemetry-exporter-otlp-proto-http>=1.36.0 | ||
| python-dotenv>=1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Run a LlamaIndex dice agent with standard OTLP export. | ||
|
|
||
| Uses the official LlamaIndexOpenTelemetry integration to set up OTel tracing. | ||
| It handles the tracer provider setup and span export internally. | ||
| Traces stream to agentevals via OTLPSpanExporter with no agentevals SDK needed. | ||
|
|
||
| Note: LlamaIndexOpenTelemetry exports spans only. Log-based content delivery | ||
| is not part of this integration. Message content is captured via span attributes. | ||
|
|
||
| Prerequisites: | ||
| 1. pip install -r examples/zero-code-examples/llama-index/requirements.txt | ||
| 2. agentevals serve --dev | ||
| 3. export OPENAI_API_KEY="your-key-here" | ||
|
|
||
| Usage: | ||
| python examples/zero-code-examples/llama-index/run.py | ||
| """ | ||
|
|
||
| import asyncio | ||
| import os | ||
| import random | ||
|
|
||
| from dotenv import load_dotenv | ||
| from llama_index.core.agent.workflow import FunctionAgent | ||
| from llama_index.core.tools import FunctionTool | ||
| from llama_index.llms.openai_like import OpenAILike | ||
| from llama_index.observability.otel import LlamaIndexOpenTelemetry | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.sdk.resources import Resource | ||
|
|
||
| load_dotenv(override=True) | ||
|
|
||
|
|
||
| def roll_die(sides: int) -> int: | ||
| """Roll a die with the given number of sides and return the result.""" | ||
| return random.randint(1, sides) | ||
|
|
||
|
|
||
| def check_prime(number: int) -> bool: | ||
| """Return True if the number is prime, False otherwise.""" | ||
| if number < 2: | ||
| return False | ||
| return all(number % i for i in range(2, int(number**0.5) + 1)) | ||
|
|
||
|
|
||
| async def main(): | ||
| if not os.getenv("OPENAI_API_KEY"): | ||
| print("OPENAI_API_KEY not set.") | ||
| return | ||
|
|
||
| endpoint = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318") | ||
| print(f"OTLP endpoint: {endpoint}") | ||
|
|
||
| os.environ.setdefault( | ||
| "OTEL_RESOURCE_ATTRIBUTES", | ||
| "agentevals.eval_set_id=llama_index_eval,agentevals.session_name=llama-index-zero-code", | ||
| ) | ||
|
|
||
| resource = Resource.create() | ||
|
|
||
| LlamaIndexOpenTelemetry( | ||
| span_exporter=OTLPSpanExporter(), | ||
| span_processor="batch", | ||
| service_name_or_resource=resource, | ||
| ).start_registering() | ||
|
|
||
| llm = OpenAILike( | ||
| model=os.environ.get("OPENAI_MODEL", "gpt-4o-mini"), | ||
| api_base=os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1"), | ||
| is_chat_model=True, | ||
| is_function_calling_model=True, | ||
| ) | ||
| agent = FunctionAgent( | ||
| tools=[FunctionTool.from_defaults(fn=roll_die), FunctionTool.from_defaults(fn=check_prime)], | ||
| llm=llm, | ||
| system_prompt="You are a helpful assistant. You can roll dice and check if numbers are prime.", | ||
| ) | ||
|
|
||
| test_queries = [ | ||
| "Hi! Can you help me?", | ||
| "Roll a 20-sided die for me", | ||
| "Is the number you rolled prime?", | ||
| ] | ||
|
|
||
| try: | ||
| for i, query in enumerate(test_queries, 1): | ||
| print(f"\n[{i}/{len(test_queries)}] User: {query}") | ||
| result = await agent.run(query) | ||
| print(f" Agent: {result.response.content}") | ||
| finally: | ||
| print() | ||
| trace.get_tracer_provider().force_flush() | ||
| print("All traces flushed to OTLP receiver.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add docstrings to tool calls.