-
Notifications
You must be signed in to change notification settings - Fork 34
Genie agent metadata #67
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
manfredcalvo
wants to merge
5
commits into
databricks:main
Choose a base branch
from
manfredcalvo:genie_agent_metadata
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
600ca71
Adding the option of returning metadata in genie agent langchain inte…
manfredcalvo f1740ce
Change span tracing
manfredcalvo 4ba0b0c
Using agent_name as the name it will be used in the agent trace
manfredcalvo 537bd23
Adding doc string to GenieAgent class.
manfredcalvo 4a7113e
Rename function that calls the api from query_genie_as_agent to call_…
manfredcalvo 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
118 changes: 81 additions & 37 deletions
118
integrations/langchain/src/databricks_langchain/genie.py
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 |
|---|---|---|
| @@ -1,53 +1,97 @@ | ||
| import mlflow | ||
| from langchain_core.messages import AIMessage, BaseMessage | ||
|
|
||
| from databricks_ai_bridge.genie import Genie | ||
|
|
||
| from langchain_core.runnables import RunnableLambda | ||
|
|
||
| from typing import Dict, Any | ||
|
|
||
|
|
||
| class GenieAgent(RunnableLambda): | ||
| """ | ||
| A class that implements an agent to send user questions to Genie Space in Databricks through the Genie API. | ||
|
|
||
| This class implements an agent that uses the GenieAPI to send user questions to Genie Space in Databricks. | ||
| If return_metadata is False, the agent's response will be a dictionary containing a single key, 'messages', | ||
| which holds the result of the SQL query executed by the Genie Space. | ||
| If `return_metadata` is set to True, the agent's response will be a dictionary containing two keys: `messages` | ||
| and `metadata`. The `messages` key will contain only one element, similar to the previous case. | ||
| The `metadata` key will include the `GenieResponse` from the API, which will consist of the result of the SQL query, | ||
| the SQL query itself, and a brief description of what the query is doing. | ||
|
|
||
| Attributes: | ||
| genie_space_id (str): The ID of the Genie space created in Databricks will be called by the Genie API. | ||
| description (str): Description of the Genie space created in Databricks that will be accessed by the GenieAPI. | ||
| genie_agent_name (str): The name of the genie agent that will be displayed in the trace. | ||
| return_metadata (bool): Whether to return the GenieResponse generated by the GenieAPI when the agent is called. | ||
| genie (Genie): The Genie API class. | ||
|
|
||
| Methods: | ||
| invoke(state): Returns a dictionary with two possible keys: "messages" and "metadata," which contain the results | ||
| of the query executed by Genie Space and the associated metadata. | ||
|
|
||
| Examples: | ||
| >>> genie_agent = GenieAgent("01ef92421857143785bb9e765454520f") | ||
| >>> genie_agent.invoke({"messages": [{"role": "user", "content": "What is the average total invoice across the different customers?"}]}) | ||
| {'messages': [AIMessage(content='| | average_total_invoice |\n|---:|------------------------:|\n| 0 | 195.648 |', | ||
| additional_kwargs={}, response_metadata={})]} | ||
| >>> genie_agent = GenieAgent("01ef92421857143785bb9e765454520f", return_metadata=True) | ||
| >>> genie_agent.invoke({"messages": [{"role": "user", "content": "What is the average total invoice across the different customers?"}]}) | ||
| {'messages': [AIMessage(content='| | avg_total_invoice |\n|---:|--------------------:|\n| 0 | 195.648 |', | ||
| additional_kwargs={}, response_metadata={})], | ||
| 'metadata': GenieResponse(result='| | avg_total_invoice |\n|---:|--------------------:|\n| 0 | 195.648 |', | ||
| query='SELECT AVG(`total_invoice`) AS avg_total_invoice FROM `finance`.`external_customers`.`invoices`', | ||
| description='This query calculates the average total invoice amount from all customer invoices, providing insight into overall billing trends.')} | ||
| """ | ||
| def __init__(self, genie_space_id: str, | ||
| genie_agent_name: str = "Genie", | ||
| description: str = "", | ||
| return_metadata: bool = False): | ||
| self.genie_space_id = genie_space_id | ||
| self.genie_agent_name = genie_agent_name | ||
| self.description = description | ||
| self.return_metadata = return_metadata | ||
| self.genie = Genie(genie_space_id) | ||
| super().__init__(self._call_genie_api, name=genie_agent_name) | ||
|
|
||
| @mlflow.trace() | ||
| def _concat_messages_array(messages): | ||
| concatenated_message = "\n".join( | ||
| [ | ||
| f"{message.get('role', message.get('name', 'unknown'))}: {message.get('content', '')}" | ||
| if isinstance(message, dict) | ||
| else f"{getattr(message, 'role', getattr(message, 'name', 'unknown'))}: {getattr(message, 'content', '')}" | ||
| for message in messages | ||
| ] | ||
| ) | ||
| return concatenated_message | ||
| @mlflow.trace() | ||
| def _concat_messages_array(self, messages): | ||
|
|
||
| data = [] | ||
|
|
||
| @mlflow.trace() | ||
| def _query_genie_as_agent(input, genie_space_id, genie_agent_name): | ||
| from langchain_core.messages import AIMessage | ||
| for message in messages: | ||
| if isinstance(message, dict): | ||
| data.append(f"{message.get('role', 'unknown')}: {message.get('content', '')}") | ||
| elif isinstance(message, BaseMessage): | ||
| data.append(f"{message.type}: {message.content}") | ||
| else: | ||
| data.append(f"{getattr(message, 'role', getattr(message, 'name', 'unknown'))}: {getattr(message, 'content', '')}") | ||
|
|
||
| genie = Genie(genie_space_id) | ||
| concatenated_message = "\n".join([e for e in data if e]) | ||
|
|
||
| message = f"I will provide you a chat history, where your name is {genie_agent_name}. Please help with the described information in the chat history.\n" | ||
| return concatenated_message | ||
|
|
||
| # Concatenate messages to form the chat history | ||
| message += _concat_messages_array(input.get("messages")) | ||
| @mlflow.trace() | ||
| def _call_genie_api(self, state: Dict[str, Any]): | ||
| message = (f"I will provide you a chat history, where your name is {self.genie_agent_name}. " | ||
| f"Please help with the described information in the chat history.\n") | ||
|
|
||
| # Send the message and wait for a response | ||
| genie_response = genie.ask_question(message) | ||
| # Concatenate messages to form the chat history | ||
| message += self._concat_messages_array(state.get("messages")) | ||
|
|
||
| if query_result := genie_response.result: | ||
| return {"messages": [AIMessage(content=query_result)]} | ||
| else: | ||
| return {"messages": [AIMessage(content="")]} | ||
| # Send the message and wait for a response | ||
| genie_response = self.genie.ask_question(message) | ||
|
|
||
| content = "" | ||
| metadata = None | ||
|
|
||
| @mlflow.trace(span_type="AGENT") | ||
| def GenieAgent(genie_space_id, genie_agent_name: str = "Genie", description: str = ""): | ||
| """Create a genie agent that can be used to query the API""" | ||
| from functools import partial | ||
| if genie_response.result: | ||
| content = genie_response.result | ||
| metadata = genie_response | ||
|
|
||
| from langchain_core.runnables import RunnableLambda | ||
| if self.return_metadata: | ||
| return {"messages": [AIMessage(content=content)], "metadata": metadata} | ||
|
|
||
| # Create a partial function with the genie_space_id pre-filled | ||
| partial_genie_agent = partial( | ||
| _query_genie_as_agent, | ||
| genie_space_id=genie_space_id, | ||
| genie_agent_name=genie_agent_name, | ||
| ) | ||
| return {"messages": [AIMessage(content=content)]} | ||
|
|
||
| # Use the partial function in the RunnableLambda | ||
| return RunnableLambda(partial_genie_agent) | ||
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
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.
Suggestion: add a docstring
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.
@FMurray Doc string added to the class.