-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(alephalpha): add beginner-friendly LLM tracing example (closes #4069) #4369
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
KochC
wants to merge
2
commits into
traceloop:main
Choose a base branch
from
KochC:feat/alephalpha-beginner-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.
+275
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,115 @@ | ||
| """ | ||
| Beginner-friendly example: LLM tracing with Aleph Alpha and OpenLLMetry | ||
| ======================================================================== | ||
|
|
||
| This script shows, step by step, how to: | ||
| 1. Initialise the Traceloop SDK (which sets up the OpenTelemetry tracer). | ||
| 2. Instrument the Aleph Alpha client so every completion call is traced | ||
| automatically. | ||
| 3. Make a basic text-completion request. | ||
| 4. Observe the trace that is captured. | ||
|
|
||
| Prerequisites | ||
| ------------- | ||
| Install the required packages:: | ||
|
|
||
| pip install traceloop-sdk opentelemetry-instrumentation-alephalpha aleph_alpha_client python-dotenv | ||
|
|
||
| Set the environment variables (or create a .env file):: | ||
|
|
||
| AA_TOKEN=<your-aleph-alpha-api-token> | ||
| TRACELOOP_API_KEY=<your-traceloop-api-key> # optional – omit to print traces locally | ||
|
|
||
| Expected trace | ||
| -------------- | ||
| After running this script you should see a single span exported with: | ||
|
|
||
| - span name : "alephalpha.completion" | ||
| - gen_ai.system : "AlephAlpha" | ||
| - llm.request.type : "completion" | ||
| - gen_ai.request.model: "luminous-base" | ||
| - gen_ai.prompt.0.content : <your prompt text> | ||
| - gen_ai.completion.0.content : <the model's reply> | ||
| - gen_ai.usage.input_tokens : <number of prompt tokens> | ||
| - gen_ai.usage.output_tokens : <number of generated tokens> | ||
| - llm.usage.total_tokens : input + output tokens | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from aleph_alpha_client import Client, CompletionRequest, Prompt | ||
| from dotenv import load_dotenv | ||
| from traceloop.sdk import Traceloop | ||
| from traceloop.sdk.decorators import task, workflow | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 1 – Load environment variables from a .env file (if present) | ||
| # --------------------------------------------------------------------------- | ||
| load_dotenv() | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 2 – Initialise Traceloop / OpenTelemetry | ||
| # | ||
| # Traceloop.init() sets up an OpenTelemetry TracerProvider and automatically | ||
| # instruments every supported LLM library that is installed, including the | ||
| # Aleph Alpha client. No additional call to AlephAlphaInstrumentor is needed | ||
| # when using the SDK. | ||
| # | ||
| # If TRACELOOP_API_KEY is set the traces are sent to Traceloop's cloud. | ||
| # Otherwise they are printed to stdout (great for local development). | ||
| # --------------------------------------------------------------------------- | ||
| Traceloop.init(app_name="alephalpha_beginner_example") | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 3 – Create the Aleph Alpha client | ||
| # --------------------------------------------------------------------------- | ||
| aleph_alpha_client = Client(token=os.environ.get("AA_TOKEN", "")) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 4 – Define a traced task that performs a single completion | ||
| # | ||
| # The @task decorator wraps the function in an OpenTelemetry span named | ||
| # "generate_joke". The AlephAlpha instrumentation automatically adds a | ||
| # child span "alephalpha.completion" containing all LLM-specific attributes. | ||
| # --------------------------------------------------------------------------- | ||
| @task(name="generate_joke") | ||
| def generate_joke(prompt_text: str) -> str: | ||
| """Send a prompt to Aleph Alpha and return the generated completion.""" | ||
| request = CompletionRequest( | ||
| prompt=Prompt.from_text(prompt_text), | ||
| maximum_tokens=200, | ||
| ) | ||
| response = aleph_alpha_client.complete(request, model="luminous-base") | ||
| completion = response.completions[0].completion | ||
| print(f"\nCompletion received:\n{completion}\n") | ||
| return completion | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 5 – Define a top-level workflow that calls the task | ||
| # | ||
| # The @workflow decorator creates the root span for this execution. All | ||
| # child spans (tasks, LLM calls) are nested inside it, giving you a clear | ||
| # view of the full execution tree in your tracing backend. | ||
| # --------------------------------------------------------------------------- | ||
| @workflow(name="joke_generator") | ||
| def joke_generator(): | ||
| """Top-level workflow: ask the model for a joke and print it.""" | ||
| prompt = "Tell me a short, funny joke about observability." | ||
| print(f"Prompt: {prompt}") | ||
| joke = generate_joke(prompt) | ||
| return joke | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Step 6 – Run the workflow | ||
| # --------------------------------------------------------------------------- | ||
| if __name__ == "__main__": | ||
| print("=" * 60) | ||
| print("Aleph Alpha LLM Tracing – Beginner Example") | ||
| print("=" * 60) | ||
| result = joke_generator() | ||
| print("=" * 60) | ||
| print("Done! Check your Traceloop dashboard (or stdout) for the trace.") | ||
| print("=" * 60) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.