-
Notifications
You must be signed in to change notification settings - Fork 567
integration: langchain-timbr provider documentation
#1161
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
wpbarco
wants to merge
8
commits into
langchain-ai:main
Choose a base branch
from
wpbarco:main
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.
+255
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
acbcb27
Add langchain-timbr provider documentation
wpbarco e5a0318
Merge branch 'main' into main
wpbarco 099d184
Merge branch 'langchain-ai:main' into main
wpbarco 9819f10
Merge branch 'main' into main
wpbarco 4e52d37
Merge branch 'main' into main
wpbarco b4d7352
Update src/oss/python/integrations/providers/timbr.mdx
wpbarco 36ac70c
Update src/oss/python/integrations/graphs/timbr.mdx
wpbarco 3598b9b
Update src/oss/python/integrations/providers/timbr.mdx
wpbarco 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| --- | ||
| title: Timbr | ||
| --- | ||
|
|
||
| >[Timbr](https://docs.timbr.ai/doc/docs/integration/langchain-sdk/) integrates natural language inputs with Timbr's ontology-driven semantic layer. Leveraging Timbr's robust ontology capabilities, the SDK integrates with Timbr data models and leverages semantic relationships and annotations, enabling users to query data using business-friendly language. | ||
|
|
||
| >Timbr provides a pre-built SQL agent, `TimbrSqlAgent`, which can be used for end-to-end purposes from user prompt, through semantic SQL query generation and validation, to query execution and result analysis. | ||
|
|
||
| >For customizations and partial usage, you can use LangChain chains and LangGraph nodes with our 5 main tools: | ||
|
|
||
| >- `IdentifyTimbrConceptChain` & `IdentifyConceptNode` - Identify relevant concepts from user prompts | ||
| >- `GenerateTimbrSqlChain` & `GenerateTimbrSqlNode` - Generate SQL queries from natural language prompts | ||
| >- `ValidateTimbrSqlChain` & `ValidateSemanticSqlNode` - Validate SQL queries against Timbr knowledge graph schemas | ||
| >- `ExecuteTimbrQueryChain` & `ExecuteSemanticQueryNode` - Execute (semantic and regular) SQL queries against Timbr knowledge graph databases | ||
| >- `GenerateAnswerChain` & `GenerateResponseNode` - Generate human-readable answers based on a given prompt and data rows | ||
|
|
||
| >Additionally, `langchain-timbr` provides `TimbrLlmConnector` for manual integration with Timbr's semantic layer using LLM providers. | ||
|
|
||
| For a comprehensive example of the `langchain-timbr` integration, see the [demo notebook](https://github.com/WPSemantix/Timbr-GenAI/tree/main/LangChain/LangChain_Timbr_Demo.ipynb). | ||
|
|
||
| ## Setting up | ||
|
|
||
| ### Installation | ||
|
|
||
| #### Install the package | ||
|
|
||
| ```bash | ||
| pip install langchain-timbr | ||
| ``` | ||
|
|
||
| #### Optional: Install with selected LLM provider | ||
|
|
||
| Choose one of: openai, anthropic, google, azure_openai, snowflake, databricks, vertex_ai (or 'all') | ||
|
|
||
| ```bash | ||
| pip install 'langchain-timbr[<your selected providers, separated by comma without spaces>]' | ||
| ``` | ||
|
|
||
| We default to OpenAI models in this guide. | ||
|
|
||
| ```python | ||
| import getpass | ||
| import os | ||
|
|
||
| if "OPENAI_API_KEY" not in os.environ: | ||
| os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| Starting from `langchain-timbr` v2.0.0, all chains, agents, and nodes support optional environment-based configuration. You can set the following environment variables to provide default values and simplify setup for the provided tools: | ||
|
|
||
| ### Timbr Connection Parameters | ||
|
|
||
| - **TIMBR_URL**: Default Timbr server URL | ||
| - **TIMBR_TOKEN**: Default Timbr authentication token | ||
| - **TIMBR_ONTOLOGY**: Default ontology/knowledge graph name | ||
|
|
||
| When these environment variables are set, the corresponding parameters (`url`, `token`, `ontology`) become optional in all chain and agent constructors and will use the environment values as defaults. | ||
|
|
||
| ### LLM Configuration Parameters | ||
|
|
||
| - **LLM_TYPE**: The type of LLM provider (one of langchain_timbr LlmTypes enum: 'openai-chat', 'anthropic-chat', 'chat-google-generative-ai', 'azure-openai-chat', 'snowflake-cortex', 'chat-databricks') | ||
| - **LLM_API_KEY**: The API key for authenticating with the LLM provider | ||
| - **LLM_MODEL**: The model name or deployment to use | ||
| - **LLM_TEMPERATURE**: Temperature setting for the LLM | ||
| - **LLM_ADDITIONAL_PARAMS**: Additional parameters as dict or JSON string | ||
|
|
||
| When LLM environment variables are set, the `llm` parameter becomes optional and will use the `LlmWrapper` with environment configuration. | ||
|
|
||
| Example environment setup: | ||
|
|
||
| ```bash | ||
| # Timbr connection | ||
| export TIMBR_URL="https://your-timbr-app.com/" | ||
| export TIMBR_TOKEN="tk_XXXXXXXXXXXXXXXXXXXXXXXX" | ||
| export TIMBR_ONTOLOGY="timbr_knowledge_graph" | ||
|
|
||
| # LLM configuration | ||
| export LLM_TYPE="openai-chat" | ||
| export LLM_API_KEY="your-openai-api-key" | ||
| export LLM_MODEL="gpt-4o" | ||
| export LLM_TEMPERATURE="0.1" | ||
| export LLM_ADDITIONAL_PARAMS='{"max_tokens": 1000}' | ||
| ``` | ||
|
|
||
| ## Querying the semantic layer | ||
|
|
||
| We can now use Timbr's chains to query the semantic layer. Import and utilize your intended chain/node, or use TimbrLlmConnector to manually integrate with Timbr's semantic layer. | ||
|
|
||
| ```python | ||
| from langchain_timbr import ExecuteTimbrQueryChain | ||
| from langchain_openai import ChatOpenAI | ||
| ``` | ||
|
|
||
| ```python | ||
| # You can use the standard LangChain ChatOpenAI/ChatAnthropic models | ||
| # or any other LLM model based on langchain_core.language_models.chat.BaseChatModel | ||
| llm = ChatOpenAI(model="gpt-4o", temperature=0, openai_api_key="open-ai-api-key") | ||
|
|
||
| # Optional alternative: Use Timbr's LlmWrapper, which provides generic connections to different LLM providers | ||
| from langchain_timbr import LlmWrapper, LlmTypes | ||
| llm = LlmWrapper(llm_type=LlmTypes.OpenAI, api_key="open-ai-api-key", model="gpt-4o") | ||
wpbarco marked this conversation as resolved.
Show resolved
Hide resolved
wpbarco marked this conversation as resolved.
Show resolved
Hide resolved
wpbarco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### ExecuteTimbrQueryChain example | ||
|
|
||
| ```python | ||
| execute_timbr_query_chain = ExecuteTimbrQueryChain( | ||
| llm=llm, | ||
| url="https://your-timbr-app.com/", | ||
| token="tk_XXXXXXXXXXXXXXXXXXXXXXXX", | ||
| ontology="timbr_knowledge_graph", | ||
| schema="dtimbr", # optional | ||
| concept="Sales", # optional | ||
| concepts_list=["Sales","Orders"], # optional | ||
| views_list=["sales_view"], # optional | ||
| note="We only need sums", # optional | ||
| retries=3, # optional | ||
| should_validate_sql=True # optional | ||
| ) | ||
|
|
||
| result = execute_timbr_query_chain.invoke({"prompt": "What are the total sales for last month?"}) | ||
| rows = result["rows"] | ||
| sql = result["sql"] | ||
| concept = result["concept"] | ||
| schema = result["schema"] | ||
| error = result.get("error", None) | ||
|
|
||
| usage_metadata = result.get("execute_timbr_usage_metadata", {}) | ||
| determine_concept_usage = usage_metadata.get('determine_concept', {}) | ||
| generate_sql_usage = usage_metadata.get('generate_sql', {}) | ||
| # Each usage_metadata item contains: | ||
| # * 'approximate': Estimated token count calculated before invoking the LLM | ||
| # * 'input_tokens'/'output_tokens'/'total_tokens'/etc.: Actual token usage metrics returned by the LLM | ||
| ``` | ||
|
|
||
| ```output | ||
| {'rows': [{'total_sales': 150000}], 'sql': 'SELECT SUM(amount) as total_sales FROM sales WHERE date >= DATEADD(month, -1, GETDATE())', 'concept': 'Sales', 'schema': 'dtimbr'} | ||
| ``` | ||
|
|
||
| ### Using multiple chains with SequentialChain | ||
|
|
||
| You can combine multiple Timbr chains to create more complex workflows. | ||
|
|
||
| ```python | ||
| from langchain.chains import SequentialChain | ||
| from langchain_timbr import ExecuteTimbrQueryChain, GenerateAnswerChain | ||
|
|
||
| execute_timbr_query_chain = ExecuteTimbrQueryChain( | ||
| llm=llm, | ||
| url='https://your-timbr-app.com/', | ||
| token='tk_XXXXXXXXXXXXXXXXXXXXXXXX', | ||
| ontology='timbr_knowledge_graph', | ||
| ) | ||
|
|
||
| generate_answer_chain = GenerateAnswerChain( | ||
| llm=llm, | ||
| url='https://your-timbr-app.com/', | ||
| token='tk_XXXXXXXXXXXXXXXXXXXXXXXX', | ||
| ) | ||
|
|
||
| pipeline = SequentialChain( | ||
| chains=[execute_timbr_query_chain, generate_answer_chain], | ||
| input_variables=["prompt"], | ||
| output_variables=["answer", "sql"] | ||
| ) | ||
|
|
||
| result = pipeline.invoke({"prompt": "What are the total sales for last month?"}) | ||
| ``` | ||
|
|
||
| ```output | ||
| {'prompt': 'What are the total sales for last month?', 'answer': 'Based on the query results, the total sales for last month amount to $150,000.', 'sql': 'SELECT SUM(amount) as total_sales FROM sales WHERE date >= DATEADD(month, -1, GETDATE())'} | ||
| ``` | ||
|
|
||
| ## Using the TimbrLlmConnector | ||
|
|
||
| For manual integration with Timbr's semantic layer, you can use the `TimbrLlmConnector` which includes the following methods: | ||
|
|
||
| - `get_ontologies` - List Timbr's semantic knowledge graphs | ||
| - `get_concepts` - List selected knowledge graph ontology representation concepts | ||
| - `get_views` - List selected knowledge graph ontology representation views | ||
| - `determine_concept` - Identify relevant concepts from user prompts | ||
| - `generate_sql` - Generate SQL queries from natural language prompts | ||
| - `validate_sql` - Validate SQL queries against Timbr knowledge graph schemas | ||
| - `run_timbr_query` - Execute (semantic and regular) SQL queries against Timbr knowledge graph databases | ||
| - `run_llm_query` - Execute agent pipeline to determine concept, generate SQL, and run query from natural language prompt | ||
|
|
||
| ```python | ||
| from langchain_timbr import TimbrLlmConnector | ||
|
|
||
| connector = TimbrLlmConnector( | ||
| llm=llm, | ||
| url="https://your-timbr-app.com/", | ||
| token="tk_XXXXXXXXXXXXXXXXXXXXXXXX", | ||
| ontology="timbr_knowledge_graph" | ||
| ) | ||
|
|
||
| # Get available concepts | ||
| concepts = connector.get_concepts() | ||
| print("Available concepts:", concepts) | ||
|
|
||
| # Run a complete query pipeline | ||
| result = connector.run_llm_query("What are the top 5 customers by revenue?") | ||
| print("Query result:", result) | ||
| ``` | ||
|
|
||
| ## Additional Resources | ||
|
|
||
| - [PyPI](https://pypi.org/project/langchain-timbr) | ||
| - [GitHub](https://github.com/WPSemantix/langchain-timbr) | ||
| - [LangChain Timbr Docs](https://docs.timbr.ai/doc/docs/integration/langchain-sdk/) | ||
| - [LangGraph Timbr Docs](https://docs.timbr.ai/doc/docs/integration/langgraph-sdk) | ||
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,34 @@ | ||
| --- | ||
| title: Timbr | ||
| --- | ||
|
|
||
| >What is `Timbr`? | ||
|
|
||
| >- Timbr is a `semantic SQL knowledge graph platform` that specializes in connecting data through ontology-driven semantic layers. | ||
| >- Timbr allows you to represent and query data using business-friendly language, making it ideal for handling complex data relationships and business logic. | ||
| >- Timbr provides `natural language to SQL` capabilities, making it easy to interact with your data using plain English queries. | ||
| >- With Timbr, you can achieve high-performance `semantic data querying`, suitable for production-level analytics and business intelligence. | ||
|
|
||
| >Get started with Timbr by visiting [their website](https://docs.timbr.ai/doc/docs/getting-started/intro-timbr/). | ||
|
|
||
| ## Installation and Setup | ||
|
|
||
| - Install the Python SDK with `pip install langchain-timbr` | ||
|
|
||
| ### Optional: Install with selected LLM provider | ||
|
|
||
| Choose one of: openai, anthropic, google, azure_openai, snowflake, databricks, vertex_ai (or 'all') | ||
|
|
||
| ```bash | ||
| pip install 'langchain-timbr[<your selected providers, separated by comma without spaces>]' | ||
| ``` | ||
|
|
||
| ## Semantic SQL Queries | ||
|
|
||
| Timbr provides a wrapper around its semantic layer that generates SQL statements based on natural language input and retrieves relevant information from your knowledge graph. | ||
|
|
||
| ```python | ||
| from langchain_timbr import create_timbr_sql_agent, ExecuteTimbrQueryChain, GenerateTimbrSqlChain | ||
| ``` | ||
|
|
||
| See a [usage example](/oss/integrations/graphs/timbr) |
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.