Skip to content
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

Add some utils for getting traces from the langfuse client #414

Merged
merged 4 commits into from
Sep 18, 2024

Conversation

evangriffiths
Copy link
Contributor

@evangriffiths evangriffiths commented Sep 18, 2024

Example usage:

api_keys = APIKeys()
assert api_keys.bet_from_address == Web3.to_checksum_address(
    "0xe7aa88a1d044e5c987ecce55ae8d2b562a41b72d"  # prophetgpt4
)
start_time = datetime(2024, 9, 13)
langfuse = Langfuse(
    secret_key=api_keys.langfuse_secret_key.get_secret_value(),
    public_key=api_keys.langfuse_public_key,
    host=api_keys.langfuse_host,
)

traces = get_traces_for_agent(
    agent_name="DeployablePredictionProphetGPT4TurboFinalAgent",
    from_timestamp=start_time,
    has_output=True,
    client=langfuse,
)

bets: list[ResolvedBet] = OmenAgentMarket.get_resolved_bets_made_since(
    better_address=api_keys.bet_from_address,
    start_time=start_time,
    end_time=None,
)


for bet in bets:
    trace = get_trace_for_bet(bet, traces)
    market = trace_to_omen_agent_market(trace)
    answer = trace_to_answer(trace)
    trades = trace_to_trades(trace)

Copy link

coderabbitai bot commented Sep 18, 2024

Walkthrough

The changes introduce a new file, langfuse_client_utils.py, which contains utility functions for managing Langfuse traces within prediction markets. Key functionalities include fetching traces for specific agents, converting traces into structured data models, and retrieving relevant trades and answers. The implementation supports pagination and filtering, and ensures trace management aligns with bet creation times.

Changes

Files Change Summary
prediction_market_agent_tooling/tools/langfuse_client_utils.py - Added functions to fetch and manage Langfuse traces, including pagination and filtering.
- Functions to convert traces into market data, answers, and trades.
- Utility functions for datetime matching and trace retrieval based on bet creation.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between deefeb5 and 42814c6.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/langfuse_client_utils.py (1 hunks)
Additional comments not posted (6)
prediction_market_agent_tooling/tools/langfuse_client_utils.py (6)

16-47: LGTM!

The get_traces_for_agent function is well-implemented and follows best practices:

  • It uses pagination to fetch traces efficiently.
  • It filters the traces based on the provided parameters.
  • The function logic is correct and easy to understand.
  • The function is well-documented with a clear docstring.
  • The function name and parameter names are descriptive.
  • The function is modular and reusable.

Great job!


50-54: LGTM!

The trace_to_omen_agent_market function is well-implemented:

  • It uses assertions to validate the trace input format.
  • It extracts the relevant data from the trace input and validates it using the model_validate method.
  • The function logic is correct and follows best practices.
  • The function name is descriptive and clear.

Good work!


57-60: LGTM!

The trace_to_answer function is well-implemented:

  • It uses assertions to validate the trace output format.
  • It extracts the relevant data from the trace output and validates it using the model_validate method.
  • The function logic is correct and follows best practices.
  • The function name is descriptive and clear.

Nice work!


63-66: LGTM!

The trace_to_trades function is well-implemented:

  • It uses assertions to validate the trace output format.
  • It extracts the relevant data from the trace output and validates each trade using the model_validate method.
  • The function logic is correct and follows best practices.
  • The function name is descriptive and clear.

Good job!


69-77: LGTM!

The get_closest_datetime_from_list function is well-implemented:

  • It handles the edge case when the list has only one datetime.
  • It uses the min function with a custom key to efficiently find the closest datetime.
  • The function logic is correct and follows best practices.
  • The function name is descriptive and clear.
  • The function is well-documented with a clear docstring.

Great work!


80-102: LGTM!

The get_trace_for_bet function is well-implemented:

  • It filters the traces based on the market id of the bet to only consider relevant traces.
  • It uses the get_closest_datetime_from_list function to efficiently find the index of the closest trace to the bet.
  • The sanity check ensures that the closest trace is after the bet, which is a valid assumption.
  • The function handles the case when no trace is found for the bet by returning None and logging a warning.
  • The function logic is correct and follows best practices.
  • The function name is descriptive and clear.

Excellent work!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

agent_traces = [
t
for t in traces.data
if t.session_id is not None and agent_name in t.session_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, shall we change session_id to just the agent's name? Or we could add agent's name as a tag to filter it by that.

Screenshot by Dropbox Capture

(not as part of this PR, just thinking that fetching all the traces won't be very efficient in future)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In UI, they allow to filter by session id startswith and similar fancy stuff, it didn't cross my mind to check if they allow that in Python SDK as well 😮‍💨

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was surprised by that too. I would have guessed that the sdk they expose would be the same as what they're using for the web backend, but I guess not!

Or we could add agent's name as a tag to filter it by that.

I was going to suggest this! Will make a ticket

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ticket for us here: #417

Also made an issue on the langfuse github https://github.com/orgs/langfuse/discussions/3395

from prediction_market_agent_tooling.tools.utils import add_utc_timezone_validator


def get_traces_for_agent(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of getting traces, it might be more future-proof to get observations with name process_market, right now, it's the same thing (trace of trader agent === observation of process_market). But it could be changed in the future and one trace could contain many process_market observations: gnosis/prediction-market-agent#444 (comment).

However it works now, it's already implement and it's just "maybe" so good by me, just FYI.

Copy link
Contributor Author

@evangriffiths evangriffiths Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried this (client.get_observations(name="process_market", ...)) but it returns an empty list - observations have the names of spans (in red), whereas process_market is the name of the trace.

So I've added the trace_name="process_market" arg. But atm the only traces containing agent names (e.g. DeployablePredictionProphetGPT4TurboFinalAgent) are already the "process_market" traces. But I guess as you say it's more future proof.

Screenshot 2024-09-18 at 15 26 13

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But both trace and span are created using @observe()! That behaviour doesn't make sense! 😄

Thanks for trying and sorry for killing your time then 😢

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries - it might be something I'm doing wrong. But I'll leave it for now if that's okay :)

@evangriffiths evangriffiths merged commit 7f0770c into main Sep 18, 2024
8 checks passed
@evangriffiths evangriffiths deleted the evan/langfuse-client-utils branch September 18, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants