mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 14:43:07 +00:00
Merge branch 'master' into wip-v1.0
This commit is contained in:
288
docs/docs/integrations/chat/aimlapi.ipynb
Normal file
288
docs/docs/integrations/chat/aimlapi.ipynb
Normal file
@@ -0,0 +1,288 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fbeb3f1eb129d115",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: AI/ML API\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6051ba9cfc65a60a",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"# ChatAimlapi\n",
|
||||
"\n",
|
||||
"This page will help you get started with AI/ML API [chat models](/docs/concepts/chat_models.mdx). For detailed documentation of all ChatAimlapi features and configurations, head to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n",
|
||||
"\n",
|
||||
"AI/ML API provides access to **300+ models** (Deepseek, Gemini, ChatGPT, etc.) via high-uptime and high-rate API."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "512f94fa4bea2628",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
|
||||
"| ChatAimlapi | langchain-aimlapi | ✅ | beta | ❌ |  |  |"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7163684608502d37",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"### Model features\n",
|
||||
"| Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |\n",
|
||||
"|:------------:|:-----------------:|:---------:|:-----------:|:-----------:|:-----------:|:---------------------:|:------------:|:-----------:|:--------:|\n",
|
||||
"| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bb9345d5b24a7741",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"To access AI/ML API models, sign up at [aimlapi.com](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration), generate an API key, and set the `AIMLAPI_API_KEY` environment variable:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "b26280519672f194",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:16:58.837623Z",
|
||||
"start_time": "2025-08-07T07:16:55.346214Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if \"AIMLAPI_API_KEY\" not in os.environ:\n",
|
||||
" os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fa131229e62dfd47",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"### Installation\n",
|
||||
"Install the `langchain-aimlapi` package:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "3777dc00d768299e",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:17:11.195741Z",
|
||||
"start_time": "2025-08-07T07:17:02.288142Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install -qU langchain-aimlapi"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d168108b0c4f9d7",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"Now we can instantiate the `ChatAimlapi` model and generate chat completions:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "f29131e65e47bd16",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:17:23.499746Z",
|
||||
"start_time": "2025-08-07T07:17:11.196747Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_aimlapi import ChatAimlapi\n",
|
||||
"\n",
|
||||
"llm = ChatAimlapi(\n",
|
||||
" model=\"meta-llama/Llama-3-70b-chat-hf\",\n",
|
||||
" temperature=0.7,\n",
|
||||
" max_tokens=512,\n",
|
||||
" timeout=30,\n",
|
||||
" max_retries=3,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "861b87289f8e146d",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Invocation\n",
|
||||
"You can invoke the model with a list of messages:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "430b1cff2e6d77b4",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:17:30.586261Z",
|
||||
"start_time": "2025-08-07T07:17:29.074409Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"J'adore la programmation.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"messages = [\n",
|
||||
" (\"system\", \"You are a helpful assistant that translates English to French.\"),\n",
|
||||
" (\"human\", \"I love programming.\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"ai_msg = llm.invoke(messages)\n",
|
||||
"print(ai_msg.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5463797524a19b2e",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Chaining\n",
|
||||
"We can chain the model with a prompt template as follows:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "bf6defc12a0c5d78",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:17:36.368436Z",
|
||||
"start_time": "2025-08-07T07:17:34.770581Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Ich liebe das Programmieren.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"\n",
|
||||
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||
" [\n",
|
||||
" (\n",
|
||||
" \"system\",\n",
|
||||
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
|
||||
" ),\n",
|
||||
" (\"human\", \"{input}\"),\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"chain = prompt | llm\n",
|
||||
"response = chain.invoke(\n",
|
||||
" {\n",
|
||||
" \"input_language\": \"English\",\n",
|
||||
" \"output_language\": \"German\",\n",
|
||||
" \"input\": \"I love programming.\",\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"print(response.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fcf0bf10a872355c",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all ChatAimlapi features and configurations, visit the [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -19,7 +19,7 @@
|
||||
"\n",
|
||||
"This notebook provides a quick overview for getting started with Anthropic [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatAnthropic features and configurations head to the [API reference](https://python.langchain.com/api_reference/anthropic/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html).\n",
|
||||
"\n",
|
||||
"Anthropic has several chat models. You can find information about their latest models and their costs, context windows, and supported input types in the [Anthropic docs](https://docs.anthropic.com/en/docs/models-overview).\n",
|
||||
"Anthropic has several chat models. You can find information about their latest models and their costs, context windows, and supported input types in the [Anthropic docs](https://docs.anthropic.com/en/docs/about-claude/models/overview).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
":::info AWS Bedrock and Google VertexAI\n",
|
||||
@@ -840,7 +840,7 @@
|
||||
"source": [
|
||||
"## Token-efficient tool use\n",
|
||||
"\n",
|
||||
"Anthropic supports a (beta) [token-efficient tool use](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/token-efficient-tool-use) feature. To use it, specify the relevant beta-headers when instantiating the model."
|
||||
"Anthropic supports a (beta) [token-efficient tool use](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/token-efficient-tool-use) feature. To use it, specify the relevant beta-headers when instantiating the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1198,7 +1198,7 @@
|
||||
"source": [
|
||||
"## Built-in tools\n",
|
||||
"\n",
|
||||
"Anthropic supports a variety of [built-in tools](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool), which can be bound to the model in the [usual way](/docs/how_to/tool_calling/). Claude will generate tool calls adhering to its internal schema for the tool:"
|
||||
"Anthropic supports a variety of [built-in tools](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/text-editor-tool), which can be bound to the model in the [usual way](/docs/how_to/tool_calling/). Claude will generate tool calls adhering to its internal schema for the tool:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1208,7 +1208,7 @@
|
||||
"source": [
|
||||
"### Web search\n",
|
||||
"\n",
|
||||
"Claude can use a [web search tool](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool) to run searches and ground its responses with citations."
|
||||
"Claude can use a [web search tool](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool) to run searches and ground its responses with citations."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1522,7 +1522,7 @@
|
||||
"source": [
|
||||
"### Text editor\n",
|
||||
"\n",
|
||||
"The text editor tool can be used to view and modify text files. See docs [here](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool) for details."
|
||||
"The text editor tool can be used to view and modify text files. See docs [here](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/text-editor-tool) for details."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"source": [
|
||||
"# ChatOCIGenAI\n",
|
||||
"\n",
|
||||
"This notebook provides a quick overview for getting started with OCIGenAI [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatOCIGenAI features and configurations head to the [API reference](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html).\n",
|
||||
"This notebook provides a quick overview for getting started with OCIGenAI [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatOCIGenAI features and configurations head to the [API reference](https://pypi.org/project/langchain-oci/).\n",
|
||||
"\n",
|
||||
"Oracle Cloud Infrastructure (OCI) Generative AI is a fully managed service that provides a set of state-of-the-art, customizable large language models (LLMs) that cover a wide range of use cases, and which is available through a single API.\n",
|
||||
"Using the OCI Generative AI service you can access ready-to-use pretrained models, or create and host your own fine-tuned custom models based on your own data on dedicated AI clusters. Detailed documentation of the service and API is available __[here](https://docs.oracle.com/en-us/iaas/Content/generative-ai/home.htm)__ and __[here](https://docs.oracle.com/en-us/iaas/api/#/en/generative-ai/20231130/)__.\n",
|
||||
@@ -26,9 +26,9 @@
|
||||
"## Overview\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/oci_generative_ai) |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: |\n",
|
||||
"| [ChatOCIGenAI](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html) | [langchain-community](https://python.langchain.com/api_reference/community/index.html) | ❌ | ❌ | ❌ |\n",
|
||||
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/oci_generative_ai) |\n",
|
||||
"| :--- |:---------------------------------------------------------------------------------| :---: | :---: | :---: |\n",
|
||||
"| [ChatOCIGenAI](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html) | [langchain-oci](https://github.com/oracle/langchain-oracle) | ❌ | ❌ | ❌ |\n",
|
||||
"\n",
|
||||
"### Model features\n",
|
||||
"| [Tool calling](/docs/how_to/tool_calling/) | [Structured output](/docs/how_to/structured_output/) | [JSON mode](/docs/how_to/structured_output/#advanced-specifying-the-method-for-structuring-outputs) | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
|
||||
@@ -37,7 +37,7 @@
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"To access OCIGenAI models you'll need to install the `oci` and `langchain-community` packages.\n",
|
||||
"To access OCIGenAI models you'll need to install the `oci` and `langchain-oci` packages.\n",
|
||||
"\n",
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
@@ -84,13 +84,15 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_oci.chat_models import ChatOCIGenAI\n",
|
||||
"from langchain_core.messages import AIMessage, HumanMessage, SystemMessage\n",
|
||||
"\n",
|
||||
"chat = ChatOCIGenAI(\n",
|
||||
" model_id=\"cohere.command-r-16k\",\n",
|
||||
" model_id=\"cohere.command-r-plus-08-2024\",\n",
|
||||
" service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n",
|
||||
" compartment_id=\"MY_OCID\",\n",
|
||||
" model_kwargs={\"temperature\": 0.7, \"max_tokens\": 500},\n",
|
||||
" compartment_id=\"compartment_id\",\n",
|
||||
" model_kwargs={\"temperature\": 0, \"max_tokens\": 500},\n",
|
||||
" auth_type=\"SECURITY_TOKEN\",\n",
|
||||
" auth_profile=\"auth_profile_name\",\n",
|
||||
" auth_file_location=\"auth_file_location\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -110,14 +112,7 @@
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"messages = [\n",
|
||||
" SystemMessage(content=\"your are an AI assistant.\"),\n",
|
||||
" AIMessage(content=\"Hi there human!\"),\n",
|
||||
" HumanMessage(content=\"tell me a joke.\"),\n",
|
||||
"]\n",
|
||||
"response = chat.invoke(messages)"
|
||||
]
|
||||
"source": "response = chat.invoke(\"Tell me one fact about Earth\")"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -146,13 +141,22 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain_oci.chat_models import ChatOCIGenAI\n",
|
||||
"\n",
|
||||
"prompt = ChatPromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
|
||||
"chain = prompt | chat\n",
|
||||
"\n",
|
||||
"response = chain.invoke({\"topic\": \"dogs\"})\n",
|
||||
"print(response.content)"
|
||||
"llm = ChatOCIGenAI(\n",
|
||||
" model_id=\"cohere.command-r-plus-08-2024\",\n",
|
||||
" service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n",
|
||||
" compartment_id=\"compartment_id\",\n",
|
||||
" model_kwargs={\"temperature\": 0, \"max_tokens\": 500},\n",
|
||||
" auth_type=\"SECURITY_TOKEN\",\n",
|
||||
" auth_profile=\"auth_profile_name\",\n",
|
||||
" auth_file_location=\"auth_file_location\",\n",
|
||||
")\n",
|
||||
"prompt = PromptTemplate(input_variables=[\"query\"], template=\"{query}\")\n",
|
||||
"llm_chain = prompt | llm\n",
|
||||
"response = llm_chain.invoke(\"what is the capital of france?\")\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -162,7 +166,7 @@
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all ChatOCIGenAI features and configurations head to the API reference: https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html"
|
||||
"For detailed documentation of all ChatOCIGenAI features and configurations head to the API reference: https://pypi.org/project/langchain-oci/"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
408
docs/docs/integrations/chat/qwen.ipynb
Normal file
408
docs/docs/integrations/chat/qwen.ipynb
Normal file
@@ -0,0 +1,408 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "afaf8039",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "raw"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: Qwen\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e49f1e0d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# ChatQwen\n",
|
||||
"\n",
|
||||
"This will help you get started with Qwen [chat models](../../concepts/chat_models.mdx). For detailed documentation of all ChatQwen features and configurations head to the [API reference](https://pypi.org/project/langchain-qwq/).\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"| Class | Package | Local | Serializable | Package downloads | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: | :---: |\n",
|
||||
"| [ChatQwen](https://pypi.org/project/langchain-qwq/) | [langchain-qwq](https://pypi.org/project/langchain-qwq/) | ❌ | beta |  |  |\n",
|
||||
"\n",
|
||||
"### Model features\n",
|
||||
"| [Tool calling](../../how_to/tool_calling.ipynb) | [Structured output](../../how_to/structured_output.ipynb) | JSON mode | [Image input](../../how_to/multimodal_inputs.ipynb) | Audio input | Video input | [Token-level streaming](../../how_to/chat_streaming.ipynb) | Native async | [Token usage](../../how_to/chat_token_usage_tracking.ipynb) | [Logprobs](../../how_to/logprobs.ipynb) |\n",
|
||||
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
|
||||
"| ✅ | ✅ | ✅ |✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | \n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"To access Qwen models you'll need to create an Alibaba Cloud account, get an API key, and install the `langchain-qwq` integration package.\n",
|
||||
"\n",
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"Head to [Alibaba's API Key page](https://account.alibabacloud.com/login/login.htm?oauth_callback=https%3A%2F%2Fbailian.console.alibabacloud.com%2F%3FapiKey%3D1&lang=en#/api-key) to sign up to Alibaba Cloud and generate an API key. Once you've done this set the `DASHSCOPE_API_KEY` environment variable:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"DASHSCOPE_API_KEY\"):\n",
|
||||
" os.environ[\"DASHSCOPE_API_KEY\"] = getpass.getpass(\"Enter your Dashscope API key: \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Installation\n",
|
||||
"\n",
|
||||
"The LangChain QwQ integration lives in the `langchain-qwq` package:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install -qU langchain-qwq"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"Now we can instantiate our model object and generate chat completions:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content='Hello! How can I assist you today? 😊', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-flash'}, id='run--62798a20-d425-48ab-91fc-8e62e37c6084-0', usage_metadata={'input_tokens': 9, 'output_tokens': 11, 'total_tokens': 20, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_qwq import ChatQwen\n",
|
||||
"\n",
|
||||
"llm = ChatQwen(model=\"qwen-flash\")\n",
|
||||
"response = llm.invoke(\"Hello\")\n",
|
||||
"\n",
|
||||
"response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2b4f3e15",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Invocation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "62e0dbc3",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content=\"J'adore la programmation.\", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-flash'}, id='run--33f905e0-880a-4a67-ab83-313fd7a06369-0', usage_metadata={'input_tokens': 32, 'output_tokens': 8, 'total_tokens': 40, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"messages = [\n",
|
||||
" (\n",
|
||||
" \"system\",\n",
|
||||
" \"You are a helpful assistant that translates English to French.\"\n",
|
||||
" \"Translate the user sentence.\",\n",
|
||||
" ),\n",
|
||||
" (\"human\", \"I love programming.\"),\n",
|
||||
"]\n",
|
||||
"ai_msg = llm.invoke(messages)\n",
|
||||
"ai_msg"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Chaining\n",
|
||||
"\n",
|
||||
"We can [chain](../../how_to/sequence.ipynb) our model with a prompt template like so:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content='Ich liebe Programmierung.', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-flash'}, id='run--9d8bab6d-d6fe-4b9f-95f2-c30c3ff0a50e-0', usage_metadata={'input_tokens': 28, 'output_tokens': 5, 'total_tokens': 33, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"\n",
|
||||
"prompt = ChatPromptTemplate(\n",
|
||||
" [\n",
|
||||
" (\n",
|
||||
" \"system\",\n",
|
||||
" \"You are a helpful assistant that translates\"\n",
|
||||
" \"{input_language} to {output_language}.\",\n",
|
||||
" ),\n",
|
||||
" (\"human\", \"{input}\"),\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"chain = prompt | llm\n",
|
||||
"chain.invoke(\n",
|
||||
" {\n",
|
||||
" \"input_language\": \"English\",\n",
|
||||
" \"output_language\": \"German\",\n",
|
||||
" \"input\": \"I love programming.\",\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8d1b3ef3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Tool Calling\n",
|
||||
"ChatQwen supports tool calling API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6db1a355",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Use with `bind_tools`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "15fb6a6d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_f0c2cc49307f480db78a45', 'function': {'arguments': '{\"first_int\": 5, \"second_int\": 42}', 'name': 'multiply'}, 'type': 'function'}]} response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-flash'} id='run--27c5aafb-9710-42f5-ab78-5a2ad1d9050e-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'call_f0c2cc49307f480db78a45', 'type': 'tool_call'}] usage_metadata={'input_tokens': 166, 'output_tokens': 27, 'total_tokens': 193, 'input_token_details': {}, 'output_token_details': {}}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.tools import tool\n",
|
||||
"\n",
|
||||
"from langchain_qwq import ChatQwen\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def multiply(first_int: int, second_int: int) -> int:\n",
|
||||
" \"\"\"Multiply two integers together.\"\"\"\n",
|
||||
" return first_int * second_int\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"llm = ChatQwen(model=\"qwen-flash\")\n",
|
||||
"\n",
|
||||
"llm_with_tools = llm.bind_tools([multiply])\n",
|
||||
"\n",
|
||||
"msg = llm_with_tools.invoke(\"What's 5 times forty two\")\n",
|
||||
"\n",
|
||||
"print(msg)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cc8ffd89-c474-45a7-a123-e0b1d362f54f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### vision Support"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3e8a7d46-d1f6-4ae8-835a-266ca47e4daf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Image"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "54f69db3-fa51-4b9a-885c-1353968066e3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"This image depicts a cozy, rustic Christmas scene set against a wooden backdrop. The arrangement features a variety of festive decorations that evoke a warm, holiday atmosphere:\n",
|
||||
"\n",
|
||||
"- **Centerpiece**: A decorative reindeer figurine with large antlers stands prominently in the background.\n",
|
||||
"- **Miniature Trees**: Two small, snow-dusted artificial Christmas trees flank the reindeer, adding to the wintry feel.\n",
|
||||
"- **Candles**: Three log-shaped candle holders made from birch bark are lit, casting a soft, warm glow. Two are in the foreground, and one is slightly behind them.\n",
|
||||
"- **\"Merry Christmas\" Sign**: A wooden cutout sign spelling \"MERRY CHRISTMAS\" is placed on the left, decorated with a tiny golden gift box and a small reindeer silhouette.\n",
|
||||
"- **Holiday Elements**: Pinecones, red berries, greenery, and fairy lights are scattered throughout, enhancing the natural, festive theme.\n",
|
||||
"- **Other Details**: A white sack with \"SANTA\" written on it is partially visible on the left, along with a large glass ornament and twinkling string lights.\n",
|
||||
"\n",
|
||||
"The overall aesthetic is warm, inviting, and traditional, emphasizing natural materials like wood, pine, and birch bark. It captures the essence of a rustic, homemade Christmas celebration.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"model = ChatQwen(model=\"qwen-vl-max-latest\")\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" HumanMessage(\n",
|
||||
" content=[\n",
|
||||
" {\n",
|
||||
" \"type\": \"image_url\",\n",
|
||||
" \"image_url\": {\"url\": \"https://example.com/image/image.png\"},\n",
|
||||
" },\n",
|
||||
" {\"type\": \"text\", \"text\": \"What do you see in this image?\"},\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = model.invoke(messages)\n",
|
||||
"print(response.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b1faea19-932f-4dc8-b0af-60e3507eee08",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Video"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "59355c38-d3e2-4051-811a-2b99286ea01b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"This video features a young woman with a warm and cheerful expression, standing outdoors in a well-lit environment. She has short, neatly styled brown hair with bangs and is wearing a soft pink knitted cardigan over a white top. A delicate necklace adorns her neck, adding a subtle touch of elegance to her outfit.\n",
|
||||
"\n",
|
||||
"Throughout the video, she maintains eye contact with the camera, smiling gently and occasionally opening her mouth as if speaking or laughing. Her facial expressions are natural and engaging, suggesting a friendly and approachable demeanor. The background is softly blurred, indicating a shallow depth of field, which keeps the focus on her. It appears to be an urban setting with modern buildings, possibly a residential or commercial area.\n",
|
||||
"\n",
|
||||
"The lighting is bright and natural, likely from sunlight, casting a soft glow on her face and highlighting her features. The overall tone of the video is pleasant and inviting, evoking a sense of warmth and positivity.\n",
|
||||
"\n",
|
||||
"In the top right corner of the frames, there is a watermark that reads \"通义·AI合成,\" which indicates that this video was generated using AI technology by Tongyi Lab, a company known for its advancements in artificial intelligence and digital content creation. This suggests that the video may be a demonstration of AI-generated human-like avatars or synthetic media.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"model = ChatQwen(model=\"qwen-vl-max-latest\")\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" HumanMessage(\n",
|
||||
" content=[\n",
|
||||
" {\n",
|
||||
" \"type\": \"video_url\",\n",
|
||||
" \"video_url\": {\"url\": \"https://example.com/video/1.mp4\"},\n",
|
||||
" },\n",
|
||||
" {\"type\": \"text\", \"text\": \"Can you tell me about this video?\"},\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = model.invoke(messages)\n",
|
||||
"print(response.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all ChatQwen features and configurations head to the [API reference](https://pypi.org/project/langchain-qwq/)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce1026e3",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -34,7 +34,7 @@
|
||||
"### Model features\n",
|
||||
"| [Tool calling](../../how_to/tool_calling.ipynb) | [Structured output](../../how_to/structured_output.ipynb) | JSON mode | [Image input](../../how_to/multimodal_inputs.ipynb) | Audio input | Video input | [Token-level streaming](../../how_to/chat_streaming.ipynb) | Native async | [Token usage](../../how_to/chat_token_usage_tracking.ipynb) | [Logprobs](../../how_to/logprobs.ipynb) |\n",
|
||||
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
|
||||
"| ✅ | ✅ | ✅ |❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n",
|
||||
"| ✅ | ✅ | ✅ |✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | \n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
@@ -47,7 +47,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1,
|
||||
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -91,7 +91,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -117,7 +117,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"id": "62e0dbc3",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@@ -126,10 +126,10 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content=\"J'aime la programmation.\", additional_kwargs={'reasoning_content': 'Okay, the user wants me to translate \"I love programming.\" into French. Let\\'s start by breaking down the sentence. The subject is \"I\", which in French is \"Je\". The verb is \"love\", which in this context is present tense, so \"aime\". The object is \"programming\". Now, \"programming\" in French can be \"la programmation\". \\n\\nWait, should it be \"programmation\" or \"programmation\"? Let me confirm the spelling. Yes, \"programmation\" is correct. Now, putting it all together: \"Je aime la programmation.\" Hmm, but in French, there\\'s a tendency to contract \"je\" and \"aime\". Wait, actually, \"je\" followed by a vowel sound usually takes \"j\\'\". So it should be \"J\\'aime la programmation.\" \\n\\nLet me double-check. \"J\\'aime\" is the correct contraction for \"I love\". The definite article \"la\" is needed because \"programmation\" is a feminine noun. Yes, \"programmation\" is a feminine noun, so \"la\" is correct. \\n\\nIs there any other way to say it? Maybe \"J\\'adore la programmation\" for \"I love\" in a stronger sense, but the user didn\\'t specify the intensity. Since the original is straightforward, \"J\\'aime la programmation.\" is the direct translation. \\n\\nI think that\\'s it. No mistakes there. So the final translation should be \"J\\'aime la programmation.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run-5045cd6a-edbd-4b2f-bf24-b7bdf3777fb9-0', usage_metadata={'input_tokens': 32, 'output_tokens': 326, 'total_tokens': 358, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
"AIMessage(content=\"J'aime la programmation.\", additional_kwargs={'reasoning_content': 'Okay, the user wants me to translate \"I love programming.\" into French. Let me start by recalling the basic translation. The verb \"love\" in French is \"aimer\", and \"programming\" is \"la programmation\". So the literal translation would be \"J\\'aime la programmation.\" But wait, I should check if there\\'s any context or nuances I need to consider. The user mentioned they\\'re a helpful assistant, so maybe they want a more natural or commonly used phrase. Sometimes in French, people might use \"adorer\" instead of \"aimer\" for stronger emphasis, but \"aimer\" is more standard here. Also, the structure \"J\\'aime\" is correct for \"I love\". No need for any articles if it\\'s a general statement, but \"la programmation\" is a feminine noun, so the article is necessary. Let me confirm the gender of \"programmation\"—yes, it\\'s feminine. So \"la\" is correct. I think that\\'s it. The translation should be \"J\\'aime la programmation.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run--396edf0f-ab92-4317-99be-cc9f5377c312-0', usage_metadata={'input_tokens': 32, 'output_tokens': 229, 'total_tokens': 261, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -159,17 +159,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content='Ich liebe das Programmieren.', additional_kwargs={'reasoning_content': 'Okay, the user wants me to translate \"I love programming.\" into German. Let me think. The verb \"love\" is \"lieben\" or \"mögen\" in German, but \"lieben\" is more like love, while \"mögen\" is prefer. Since it\\'s about programming, which is a strong affection, \"lieben\" is better. The subject is \"I\", which is \"ich\". Then \"programming\" is \"Programmierung\" or \"Coding\". But \"Programmierung\" is more formal. Alternatively, sometimes people say \"ich liebe es zu programmieren\" which is \"I love to program\". Hmm, maybe the direct translation would be \"Ich liebe die Programmierung.\" But maybe the more natural way is \"Ich liebe es zu programmieren.\" Let me check. Both are correct, but the second one might sound more natural in everyday speech. The user might prefer the concise version. Alternatively, maybe \"Ich liebe die Programmierung.\" is better. Wait, the original is \"programming\" as a noun. So using the noun form would be appropriate. So \"Ich liebe die Programmierung.\" But sometimes people also use \"Coding\" in German, like \"Ich liebe das Coding.\" But that\\'s more anglicism. Probably better to stick with \"Programmierung\". Alternatively, \"Programmieren\" as a noun. Oh right! \"Programmieren\" can be a noun when used in the accusative case. So \"Ich liebe das Programmieren.\" That\\'s correct and natural. Yes, that\\'s the best translation. So the answer is \"Ich liebe das Programmieren.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run-2c418451-51d8-4319-8269-2ce129363a1a-0', usage_metadata={'input_tokens': 28, 'output_tokens': 341, 'total_tokens': 369, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
"AIMessage(content='Ich liebe das Programmieren.', additional_kwargs={'reasoning_content': 'Okay, the user wants to translate \"I love programming.\" into German. Let\\'s start by breaking down the sentence. The subject is \"I,\" which translates to \"Ich\" in German. The verb \"love\" is \"liebe\" in present tense for the first person singular. Then \"programming\" is a noun. Now, in German, the word for programming, especially in the context of computer programming, is \"Programmierung.\" However, sometimes people might use \"Programmieren\" as well. Wait, but \"Programmierung\" is the noun form, so \"die Programmierung.\" The structure in German would be \"Ich liebe die Programmierung.\" Alternatively, could it be \"Programmieren\" as the verb in a nominalized form? Let me think. If you say \"Ich liebe das Programmieren,\" that\\'s also correct because \"das Programmieren\" is the gerundive form, which is commonly used for activities. So both are possible. Which one is more natural? Hmm. \"Das Programmieren\" might be more common in everyday language. Let me check some examples. For instance, \"I love cooking\" would be \"Ich liebe das Kochen.\" So following that pattern, \"Ich liebe das Programmieren\" would be the equivalent. Therefore, maybe \"Programmieren\" with the article \"das\" is better here. But the user might just want a direct translation without the article. Wait, the original sentence is \"I love programming,\" which is a noun, so in German, you need an article. So the correct translation would include \"das\" before the noun. So the correct sentence is \"Ich liebe das Programmieren.\" Alternatively, if they want to use the noun without an article, maybe in a more abstract sense, but I think \"das\" is necessary here. Let me confirm. Yes, in German, when using the noun form of a verb like this, you need the article. So the best translation is \"Ich liebe das Programmieren.\" I think that\\'s the most natural way to say it. Alternatively, \"Programmierung\" is more formal, but \"Programmieren\" is more commonly used in such contexts. So I\\'ll go with \"Ich liebe das Programmieren.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run--0ceaba8a-7842-48fb-8bec-eb96d2c83ed4-0', usage_metadata={'input_tokens': 28, 'output_tokens': 466, 'total_tokens': 494, 'input_token_details': {}, 'output_token_details': {}})"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -217,7 +217,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 5,
|
||||
"id": "15fb6a6d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -225,12 +225,13 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"content='' additional_kwargs={'reasoning_content': 'Okay, the user is asking \"What\\'s 5 times forty two\". Let me break this down. First, I need to identify the numbers involved. The first number is 5, which is straightforward. The second number is forty two, which is 42 in digits. The operation they want is multiplication.\\n\\nLooking at the tools provided, there\\'s a function called multiply that takes two integers. So I should use that. The parameters are first_int and second_int. \\n\\nI need to convert \"forty two\" to 42. Since the function requires integers, both numbers should be in integer form. So 5 and 42. \\n\\nNow, I\\'ll structure the tool call. The function name is multiply, and the arguments should be first_int: 5 and second_int: 42. I\\'ll make sure the JSON is correctly formatted without any syntax errors. Let me double-check the parameters to ensure they\\'re required and of the right type. Yep, both are required and integers. \\n\\nNo examples were provided, but the function\\'s purpose is clear. So the correct tool call should be to multiply those two numbers. I think that\\'s all. No other functions are needed here.'} response_metadata={'model_name': 'qwq-plus'} id='run-638895aa-fdde-4567-bcfa-7d8e5d4f24af-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'call_d088275851c140529ed2ad', 'type': 'tool_call'}] usage_metadata={'input_tokens': 176, 'output_tokens': 277, 'total_tokens': 453, 'input_token_details': {}, 'output_token_details': {}}\n"
|
||||
"content='' additional_kwargs={'reasoning_content': 'Okay, the user is asking \"What\\'s 5 times forty two\". Let me break this down. They want the product of 5 and 42. The function provided is called multiply, which takes two integers. First, I need to parse the numbers from the question. The first integer is 5, straightforward. The second is forty two, which is 42 in numeric form. So I should call the multiply function with first_int=5 and second_int=42. Let me double-check the parameters: both are required and of type integer. Yep, that\\'s correct. No examples given, but the function should handle these numbers. Alright, time to format the tool call.'} response_metadata={'model_name': 'qwq-plus'} id='run--3c5ff46c-3fc8-4caf-a665-2405aeef2948-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'call_33fb94c6662d44928e56ec', 'type': 'tool_call'}] usage_metadata={'input_tokens': 176, 'output_tokens': 173, 'total_tokens': 349, 'input_token_details': {}, 'output_token_details': {}}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.tools import tool\n",
|
||||
"\n",
|
||||
"from langchain_qwq import ChatQwQ\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -249,6 +250,170 @@
|
||||
"print(msg)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "88aa9980-1bd6-4cc9-aeac-4c9011e617fc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### vision Support"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "79e372e3-7050-4038-bf88-d1e8f5ddae09",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Image"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "c2372365-7208-42f9-a147-deffdc390313",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The image depicts a charming Christmas-themed arrangement set against a rustic wooden backdrop, creating a warm and festive atmosphere. Here's a detailed breakdown:\n",
|
||||
"\n",
|
||||
"### **Background & Setting**\n",
|
||||
"- **Wooden Wall**: A horizontally paneled wooden wall forms the backdrop, giving a cozy, cabin-like feel.\n",
|
||||
"- **Foreground Surface**: The decorations rest on a smooth wooden surface (likely a table or desk), enhancing the natural, earthy tone of the scene.\n",
|
||||
"\n",
|
||||
"### **Key Elements**\n",
|
||||
"1. **Snow-Covered Trees**:\n",
|
||||
" - Two miniature evergreen trees dusted with artificial snow flank the sides of the arrangement, evoking a wintry landscape.\n",
|
||||
"\n",
|
||||
"2. **String Lights**:\n",
|
||||
" - A strand of white bulb lights stretches across the back, weaving through the decor and adding a soft, glowing ambiance.\n",
|
||||
"\n",
|
||||
"3. **Ornamental Sphere**:\n",
|
||||
" - A reflective gold sphere with striped patterns sits near the center-left, catching and dispersing light.\n",
|
||||
"\n",
|
||||
"4. **\"Merry Christmas\" Sign**:\n",
|
||||
" - A wooden cutout spelling \"MERRY CHRISTMAS\" in capital letters serves as the focal point. The letters feature star-shaped cutouts, allowing light to shine through.\n",
|
||||
"\n",
|
||||
"5. **Reindeer Figurine**:\n",
|
||||
" - A brown reindeer with white facial markings and large antlers stands prominently on the right, facing forward and adding a playful touch.\n",
|
||||
"\n",
|
||||
"6. **Candle Holders**:\n",
|
||||
" - Three birch-bark candle holders are arranged in front of the reindeer. Two hold lit tealights, casting a warm glow, while the third remains unlit.\n",
|
||||
"\n",
|
||||
"7. **Natural Accents**:\n",
|
||||
" - **Pinecones**: Scattered throughout, adding texture and a woodland feel.\n",
|
||||
" - **Berry Branches**: Red-berried greenery (likely holly) weaves behind the sign, introducing vibrant color.\n",
|
||||
" - **Pine Branches**: Fresh-looking branches enhance the seasonal authenticity.\n",
|
||||
"\n",
|
||||
"8. **Gift Box**:\n",
|
||||
" - A small golden gift box with a bow sits near the left, symbolizing holiday gifting.\n",
|
||||
"\n",
|
||||
"9. **Textile Detail**:\n",
|
||||
" - A fabric piece with \"Christmas\" embroidered on it peeks from the left, partially obscured but contributing to the thematic unity.\n",
|
||||
"\n",
|
||||
"### **Color Palette & Mood**\n",
|
||||
"- **Warm Tones**: Browns (wood, reindeer), golds (ornament, gift box), and whites (snow, lights) dominate, creating a inviting glow.\n",
|
||||
"- **Cool Accents**: Greens (trees, branches) and reds (berries) provide contrast, balancing the warmth.\n",
|
||||
"- **Lighting**: The lit candles and string lights cast a soft, flickering illumination, enhancing the intimate, celebratory vibe.\n",
|
||||
"\n",
|
||||
"### **Composition**\n",
|
||||
"- **Balance**: The arrangement is symmetrical, with trees and candles on either side framing the central sign and reindeer.\n",
|
||||
"- **Depth**: Layered elements (trees, lights, branches) create visual interest, drawing the eye inward.\n",
|
||||
"\n",
|
||||
"This image beautifully captures the essence of a cozy, handmade Christmas display, blending traditional symbols with natural textures to evoke nostalgia and joy.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"model = ChatQwQ(model=\"qvq-max-latest\")\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" HumanMessage(\n",
|
||||
" content=[\n",
|
||||
" {\n",
|
||||
" \"type\": \"image_url\",\n",
|
||||
" \"image_url\": {\"url\": \"https://example.com/image/image.png\"},\n",
|
||||
" },\n",
|
||||
" {\"type\": \"text\", \"text\": \"What do you see in this image?\"},\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = model.invoke(messages)\n",
|
||||
"print(response.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9242acf7-9a66-40b1-98b5-b113d28fc6ec",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Video"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "f0a9e542-7a85-44d2-8576-14314a50d948",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The image provided is a still frame from a video featuring a young woman with short brown hair and bangs, smiling brightly at the camera. Here's a detailed breakdown:\n",
|
||||
"\n",
|
||||
"### **Description of the Image:**\n",
|
||||
"- **Subject:** A youthful female with a cheerful expression, showcasing a wide smile with visible teeth.\n",
|
||||
"- **Appearance:** \n",
|
||||
" - Short, neatly styled brown hair with blunt bangs.\n",
|
||||
" - Natural makeup emphasizing clear skin and subtle eye makeup.\n",
|
||||
" - Wearing a white round-neck shirt layered under a light pink knitted cardigan.\n",
|
||||
" - Accessories include a delicate necklace with a small pendant and small earrings.\n",
|
||||
"- **Background:** An outdoor setting with blurred architectural elements (e.g., buildings with columns), suggesting a campus, park, or residential area.\n",
|
||||
"- **Lighting:** Soft, natural daylight, enhancing the warm and inviting atmosphere.\n",
|
||||
"\n",
|
||||
"### **Key Details About the Video:**\n",
|
||||
"1. **AI-Generated Content:** The watermark (\"通义·AI合成\" / \"Tongyi AI Synthesis\") indicates this image was created using Alibaba's Tongyi AI model, known for generating hyper-realistic visuals.\n",
|
||||
"2. **Style & Purpose:** The high-quality, photorealistic style suggests the video may demonstrate AI imaging capabilities, potentially for advertising, entertainment, or educational purposes.\n",
|
||||
"3. **Context Clues:** The subject's casual yet polished look and the pleasant outdoor setting imply a positive, approachable theme (e.g., lifestyle, technology promotion, or social media content).\n",
|
||||
"\n",
|
||||
"### **What We Can Infer About the Video:**\n",
|
||||
"- Likely showcases dynamic AI-generated scenes featuring the same character in various poses or interactions.\n",
|
||||
"- May highlight realism in digital avatars or synthetic media.\n",
|
||||
"- Could be part of a demo reel, tutorial, or creative project emphasizing AI artistry.\n",
|
||||
"\n",
|
||||
"### **Limitations:**\n",
|
||||
"- As only a single frame is provided, specifics about the video's length, narrative, or additional scenes cannot be determined.\n",
|
||||
"\n",
|
||||
"If you have more frames or context, feel free to share! 😊\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"model = ChatQwQ(model=\"qvq-max-latest\")\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" HumanMessage(\n",
|
||||
" content=[\n",
|
||||
" {\n",
|
||||
" \"type\": \"video_url\",\n",
|
||||
" \"video_url\": {\"url\": \"https://example.com/video/1.mp4\"},\n",
|
||||
" },\n",
|
||||
" {\"type\": \"text\", \"text\": \"Can you tell me about this video?\"},\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = model.invoke(messages)\n",
|
||||
"print(response.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
|
||||
@@ -258,11 +423,19 @@
|
||||
"\n",
|
||||
"For detailed documentation of all ChatQwQ features and configurations head to the [API reference](https://pypi.org/project/langchain-qwq/)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "824f0c67-5f3b-4079-bc17-2cf92755bdd5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -276,7 +449,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.1"
|
||||
"version": "3.11.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -16,13 +16,7 @@
|
||||
"This notebook covers how to load documents from Oracle Autonomous Database.\n",
|
||||
"\n",
|
||||
"## Prerequisites\n",
|
||||
"1. Install python-oracledb:\n",
|
||||
"\n",
|
||||
" `pip install oracledb`\n",
|
||||
" \n",
|
||||
" See [Installing python-oracledb](https://python-oracledb.readthedocs.io/en/latest/user_guide/installation.html).\n",
|
||||
"\n",
|
||||
"2. A database that python-oracledb's default 'Thin' mode can connected to. This is true of Oracle Autonomous Database, see [python-oracledb Architecture](https://python-oracledb.readthedocs.io/en/latest/user_guide/introduction.html#architecture).\n"
|
||||
"1. A database that python-oracledb's default 'Thin' mode can connected to. This is true of Oracle Autonomous Database, see [python-oracledb Architecture](https://python-oracledb.readthedocs.io/en/latest/user_guide/introduction.html#architecture).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -38,17 +32,12 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"pip install oracledb"
|
||||
"You'll need to install `langchain-oracledb` with `python -m pip install -U langchain-oracledb` to use this integration.\n",
|
||||
"\n",
|
||||
"The `python-oracledb` driver is installed automatically as a dependency of langchain-oracledb."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -62,7 +51,21 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders import OracleAutonomousDatabaseLoader\n",
|
||||
"# python -m pip install -U langchain-oracledb"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_oracledb.document_loaders import OracleAutonomousDatabaseLoader\n",
|
||||
"from settings import s"
|
||||
]
|
||||
},
|
||||
@@ -99,7 +102,7 @@
|
||||
" config_dir=s.CONFIG_DIR,\n",
|
||||
" wallet_location=s.WALLET_LOCATION,\n",
|
||||
" wallet_password=s.PASSWORD,\n",
|
||||
" tns_name=s.TNS_NAME,\n",
|
||||
" dsn=s.DSN,\n",
|
||||
")\n",
|
||||
"doc_1 = doc_loader_1.load()\n",
|
||||
"\n",
|
||||
@@ -108,7 +111,7 @@
|
||||
" user=s.USERNAME,\n",
|
||||
" password=s.PASSWORD,\n",
|
||||
" schema=s.SCHEMA,\n",
|
||||
" connection_string=s.CONNECTION_STRING,\n",
|
||||
" dsn=s.DSN,\n",
|
||||
" wallet_location=s.WALLET_LOCATION,\n",
|
||||
" wallet_password=s.PASSWORD,\n",
|
||||
")\n",
|
||||
@@ -147,7 +150,7 @@
|
||||
" password=s.PASSWORD,\n",
|
||||
" schema=s.SCHEMA,\n",
|
||||
" config_dir=s.CONFIG_DIR,\n",
|
||||
" tns_name=s.TNS_NAME,\n",
|
||||
" dsn=s.DSN,\n",
|
||||
" parameters=[\"Direct Sales\"],\n",
|
||||
")\n",
|
||||
"doc_3 = doc_loader_3.load()\n",
|
||||
@@ -157,7 +160,7 @@
|
||||
" user=s.USERNAME,\n",
|
||||
" password=s.PASSWORD,\n",
|
||||
" schema=s.SCHEMA,\n",
|
||||
" connection_string=s.CONNECTION_STRING,\n",
|
||||
" dsn=s.DSN,\n",
|
||||
" parameters=[\"Direct Sales\"],\n",
|
||||
")\n",
|
||||
"doc_4 = doc_loader_4.load()"
|
||||
|
||||
@@ -42,7 +42,9 @@
|
||||
"source": [
|
||||
"### Prerequisites\n",
|
||||
"\n",
|
||||
"Please install Oracle Python Client driver to use Langchain with Oracle AI Vector Search. "
|
||||
"You'll need to install `langchain-oracledb` with `python -m pip install -U langchain-oracledb` to use this integration.\n",
|
||||
"\n",
|
||||
"The `python-oracledb` driver is installed automatically as a dependency of langchain-oracledb."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -51,7 +53,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# pip install oracledb"
|
||||
"# python -m pip install -U langchain-oracledb"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -154,7 +156,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders.oracleai import OracleDocLoader\n",
|
||||
"from langchain_oracledb.document_loaders.oracleai import OracleDocLoader\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
@@ -199,7 +201,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders.oracleai import OracleTextSplitter\n",
|
||||
"from langchain_oracledb.document_loaders.oracleai import OracleTextSplitter\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
|
||||
357
docs/docs/integrations/llms/aimlapi.ipynb
Normal file
357
docs/docs/integrations/llms/aimlapi.ipynb
Normal file
@@ -0,0 +1,357 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: AI/ML API\n",
|
||||
"---"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c74887ead73c5eb4"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# AimlapiLLM\n",
|
||||
"\n",
|
||||
"This page will help you get started with AI/ML API [text completion models](/docs/concepts/text_llms). For detailed documentation of all AimlapiLLM features and configurations, head to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n",
|
||||
"\n",
|
||||
"AI/ML API provides access to **300+ models** (Deepseek, Gemini, ChatGPT, etc.) via high-uptime and high-rate API."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c1895707cde83d90"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
|
||||
"| AimlapiLLM | langchain-aimlapi | ✅ | beta | ❌ |  |  |"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "72b0a510b6eac641"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Model features\n",
|
||||
"| Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |\n",
|
||||
"|:------------:|:-----------------:|:---------:|:-----------:|:-----------:|:-----------:|:---------------------:|:------------:|:-----------:|:--------:|\n",
|
||||
"| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "4b87089494d8877d"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"To access AI/ML API models, sign up at [aimlapi.com](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration), generate an API key, and set the `AIMLAPI_API_KEY` environment variable:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "2c45017efcc36569"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if \"AIMLAPI_API_KEY\" not in os.environ:\n",
|
||||
" os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:24:48.681319Z",
|
||||
"start_time": "2025-08-07T07:24:47.490206Z"
|
||||
}
|
||||
},
|
||||
"id": "86b05af725c45941",
|
||||
"execution_count": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Installation\n",
|
||||
"Install the `langchain-aimlapi` package:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "51171ba92cb2b382"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install -qU langchain-aimlapi"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:18:08.606708Z",
|
||||
"start_time": "2025-08-07T07:17:59.901457Z"
|
||||
}
|
||||
},
|
||||
"id": "2b15cbaf7d5e1560",
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"Now we can instantiate the `AimlapiLLM` model and generate text completions:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "e94379f9d37fe6b3"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_aimlapi import AimlapiLLM\n",
|
||||
"\n",
|
||||
"llm = AimlapiLLM(\n",
|
||||
" model=\"gpt-3.5-turbo-instruct\",\n",
|
||||
" temperature=0.5,\n",
|
||||
" max_tokens=256,\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:46:52.875867Z",
|
||||
"start_time": "2025-08-07T07:46:52.869961Z"
|
||||
}
|
||||
},
|
||||
"id": "8a3af681997723b0",
|
||||
"execution_count": 23
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Invocation\n",
|
||||
"You can invoke the model with a prompt:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c983ab1d95887e8f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted.\n",
|
||||
"\n",
|
||||
"The algorithm gets its name from the way smaller elements \"bubble\" to the top of the list. It is commonly used for educational purposes due to its simplicity, but it is not a very efficient sorting algorithm for large data sets.\n",
|
||||
"\n",
|
||||
"Here is an implementation of the bubble sort algorithm in Python:\n",
|
||||
"\n",
|
||||
"1. Start by defining a function that takes in a list as its argument.\n",
|
||||
"2. Set a variable \"swapped\" to True, indicating that a swap has occurred.\n",
|
||||
"3. Create a while loop that runs as long as the \"swapped\" variable is True.\n",
|
||||
"4. Inside the loop, set the \"swapped\" variable to False.\n",
|
||||
"5. Create a for loop that iterates through the list, starting from the first element and ending at the second to last element.\n",
|
||||
"6. Inside the for loop, compare the current element with the next element. If the current element is larger than the next element, swap them and set the \"swapped\" variable to True.\n",
|
||||
"7. After the for loop, if the \"swapped\" variable\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"response = llm.invoke(\"Explain the bubble sort algorithm in Python.\")\n",
|
||||
"print(response)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:46:57.209950Z",
|
||||
"start_time": "2025-08-07T07:46:53.935975Z"
|
||||
}
|
||||
},
|
||||
"id": "9a193081f431a42a",
|
||||
"execution_count": 24
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Streaming Invocation\n",
|
||||
"You can also stream responses token-by-token:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "1afedb28f556c7bd"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" \n",
|
||||
"\n",
|
||||
"1. Python\n",
|
||||
"Python has been consistently growing in popularity and has become one of the most widely used programming languages in recent years. It is used for a wide range of applications such as web development, data analysis, machine learning, and artificial intelligence. Its simple syntax and readability make it an attractive choice for beginners and experienced programmers alike. With the rise of data-driven technology and automation, Python is projected to be the most in-demand language in 2025.\n",
|
||||
"\n",
|
||||
"2. JavaScript\n",
|
||||
"JavaScript continues to dominate the web development scene and is expected to maintain its position as a top programming language in 2025. With the increasing use of front-end frameworks like React and Angular, JavaScript is crucial for building dynamic and interactive user interfaces. Additionally, the rise of serverless architecture and the popularity of Node.js make JavaScript an essential language for both front-end and back-end development.\n",
|
||||
"\n",
|
||||
"3. Go\n",
|
||||
"Go, also known as Golang, is a relatively new programming language developed by Google. It is designed for"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"llm = AimlapiLLM(\n",
|
||||
" model=\"gpt-3.5-turbo-instruct\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"for chunk in llm.stream(\"List top 5 programming languages in 2025 with reasons.\"):\n",
|
||||
" print(chunk, end=\"\", flush=True)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:49:25.223233Z",
|
||||
"start_time": "2025-08-07T07:49:22.101498Z"
|
||||
}
|
||||
},
|
||||
"id": "a132c9183f648fb4",
|
||||
"execution_count": 26
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all AimlapiLLM features and configurations, visit the [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "7b4ab33058dc0974"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Chaining\n",
|
||||
"\n",
|
||||
"You can also easily combine with a prompt template for easy structuring of user input. We can do this using [LCEL](/docs/concepts/lcel)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "900f36a35477c8ae"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"prompt = PromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
|
||||
"chain = prompt | llm"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:49:34.857042Z",
|
||||
"start_time": "2025-08-07T07:49:34.853032Z"
|
||||
}
|
||||
},
|
||||
"id": "d7f10052eb4ff249",
|
||||
"execution_count": 27
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "\"\\n\\nWhy do bears have fur coats?\\n\\nBecause they'd look silly in sweaters! \""
|
||||
},
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chain.invoke({\"topic\": \"bears\"})"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:49:48.565804Z",
|
||||
"start_time": "2025-08-07T07:49:35.558426Z"
|
||||
}
|
||||
},
|
||||
"id": "184c333c60f94b05",
|
||||
"execution_count": 28
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all `AI/ML API` llm features and configurations head to the API reference: [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "804f3a79a8046ec1"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -22,30 +22,28 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"Ensure that the oci sdk and the langchain-community package are installed"
|
||||
"Ensure that the oci sdk and the langchain-community package are installed\n",
|
||||
"\n",
|
||||
":::caution You are currently on a page documenting the use of Oracle's text generation models. Which are deprecated."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install -U langchain-oci"
|
||||
]
|
||||
"execution_count": null,
|
||||
"source": "!pip install -U langchain-oci"
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Usage"
|
||||
]
|
||||
"source": "## Usage"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"execution_count": null,
|
||||
"source": [
|
||||
"from langchain_oci.llms import OCIGenAI\n",
|
||||
"\n",
|
||||
|
||||
272
docs/docs/integrations/providers/aimlapi.ipynb
Normal file
272
docs/docs/integrations/providers/aimlapi.ipynb
Normal file
@@ -0,0 +1,272 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# AI/ML API LLM\n",
|
||||
"\n",
|
||||
"[AI/ML API](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration) provides an API to query **300+ leading AI models** (Deepseek, Gemini, ChatGPT, etc.) with enterprise-grade performance.\n",
|
||||
"\n",
|
||||
"This example demonstrates how to use LangChain to interact with AI/ML API models."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "bb9dcd1ba7b0f560"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Installation"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "e4c35f60c565d369"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Requirement already satisfied: langchain-aimlapi in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (0.1.0)\n",
|
||||
"Requirement already satisfied: langchain-core<0.4.0,>=0.3.15 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-aimlapi) (0.3.67)\n",
|
||||
"Requirement already satisfied: langsmith>=0.3.45 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.4.4)\n",
|
||||
"Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (9.1.2)\n",
|
||||
"Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.33)\n",
|
||||
"Requirement already satisfied: PyYAML>=5.3 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (6.0.2)\n",
|
||||
"Requirement already satisfied: packaging<25,>=23.2 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (24.2)\n",
|
||||
"Requirement already satisfied: typing-extensions>=4.7 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (4.14.0)\n",
|
||||
"Requirement already satisfied: pydantic>=2.7.4 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.11.7)\n",
|
||||
"Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.0.0)\n",
|
||||
"Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.28.1)\n",
|
||||
"Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.10.18)\n",
|
||||
"Requirement already satisfied: requests<3,>=2 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.32.4)\n",
|
||||
"Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.0.0)\n",
|
||||
"Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in c:\\users\\tuman\\appdata\\roaming\\python\\python312\\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.23.0)\n",
|
||||
"Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.7.0)\n",
|
||||
"Requirement already satisfied: pydantic-core==2.33.2 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.33.2)\n",
|
||||
"Requirement already satisfied: typing-inspection>=0.4.0 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.4.1)\n",
|
||||
"Requirement already satisfied: anyio in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (4.9.0)\n",
|
||||
"Requirement already satisfied: certifi in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2025.6.15)\n",
|
||||
"Requirement already satisfied: httpcore==1.* in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.0.9)\n",
|
||||
"Requirement already satisfied: idna in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.10)\n",
|
||||
"Requirement already satisfied: h11>=0.16 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.16.0)\n",
|
||||
"Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.4.2)\n",
|
||||
"Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.5.0)\n",
|
||||
"Requirement already satisfied: sniffio>=1.1 in c:\\users\\tuman\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.3.1)\n",
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"[notice] A new release of pip is available: 25.0.1 -> 25.2\n",
|
||||
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install --upgrade langchain-aimlapi"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-06T15:22:02.570792Z",
|
||||
"start_time": "2025-08-06T15:21:32.377131Z"
|
||||
}
|
||||
},
|
||||
"id": "77d4a44909effc3c",
|
||||
"execution_count": 4
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Environment\n",
|
||||
"\n",
|
||||
"To use AI/ML API, you'll need an API key which you can generate at:\n",
|
||||
"[https://aimlapi.com/app/](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration)\n",
|
||||
"\n",
|
||||
"You can pass it via `aimlapi_api_key` parameter or set as environment variable `AIMLAPI_API_KEY`."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c41eaf364c0b414f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import getpass\n",
|
||||
"\n",
|
||||
"if \"AIMLAPI_API_KEY\" not in os.environ:\n",
|
||||
" os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:15:37.147559Z",
|
||||
"start_time": "2025-08-07T07:15:30.919160Z"
|
||||
}
|
||||
},
|
||||
"id": "421cd40d4e54de62",
|
||||
"execution_count": 3
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Example: Chat Model"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "d9cbe98904f4c5e4"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The city that never sleeps! New York City is a treasure trove of excitement, entertainment, and adventure. Here are some fun things to do in NYC:\n",
|
||||
"\n",
|
||||
"**Iconic Attractions:**\n",
|
||||
"\n",
|
||||
"1. **Statue of Liberty and Ellis Island**: Take a ferry to Liberty Island to see the iconic statue up close and visit the Ellis Island Immigration Museum.\n",
|
||||
"2. **Central Park**: A tranquil oasis in the middle of Manhattan, perfect for a stroll, picnic, or bike ride.\n",
|
||||
"3. **Empire State Building**: For a panoramic view of the city, head to the observation deck of this iconic skyscraper.\n",
|
||||
"4. **The Metropolitan Museum of Art**: One of the world's largest and most famous museums, with a collection that spans over 5,000 years of human history.\n",
|
||||
"\n",
|
||||
"**Neighborhood Explorations:**\n",
|
||||
"\n",
|
||||
"1. **SoHo**: Known for its trendy boutiques, art galleries, and cast-iron buildings.\n",
|
||||
"2. **Greenwich Village**: A charming neighborhood with a rich history, known for its bohemian vibe, jazz clubs, and historic brownstones.\n",
|
||||
"3. **Chinatown and Little Italy**: Experience the vibrant cultures of these two iconic neighborhoods, with delicious food, street festivals, and unique shops.\n",
|
||||
"4. **Williamsburg, Brooklyn**: A hip neighborhood with a thriving arts scene, trendy bars, and some of the best restaurants in the city.\n",
|
||||
"\n",
|
||||
"**Food and Drink:**\n",
|
||||
"\n",
|
||||
"1. **Try a classic NYC slice of pizza**: Visit Lombardi's, Joe's Pizza, or Patsy's Pizzeria for a taste of the city's famous pizza.\n",
|
||||
"2. **Bagels with lox and cream cheese**: A classic NYC breakfast at a Jewish deli like Russ & Daughters Cafe or Ess-a-Bagel.\n",
|
||||
"3. **Food markets**: Visit Smorgasburg in Brooklyn or Chelsea Market for a variety of artisanal foods and drinks.\n",
|
||||
"4. **Rooftop bars**: Enjoy a drink with a view at 230 Fifth, the Top of the Strand, or the Roof at The Viceroy Central Park.\n",
|
||||
"**Performing Arts:**\n",
|
||||
"\n",
|
||||
"1. **Broadway shows**: Catch a musical or play on the Great White Way, like Hamilton, The Lion King, or Wicked.\n",
|
||||
"2. **Jazz clubs**: Visit Blue Note Jazz Club, the Village Vanguard, or the Jazz Standard for live music performances.\n",
|
||||
"3. **Lincoln Center**: Home to the New York City Ballet, the Metropolitan Opera, and the Juilliard School.\n",
|
||||
"4. **"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_aimlapi import ChatAimlapi\n",
|
||||
"\n",
|
||||
"chat = ChatAimlapi(\n",
|
||||
" model=\"meta-llama/Llama-3-70b-chat-hf\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Stream response\n",
|
||||
"for chunk in chat.stream(\"Tell me fun things to do in NYC\"):\n",
|
||||
" print(chunk.content, end=\"\", flush=True)\n",
|
||||
"\n",
|
||||
"# Or use invoke()\n",
|
||||
"# response = chat.invoke(\"Tell me fun things to do in NYC\")\n",
|
||||
"# print(response)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:15:59.612289Z",
|
||||
"start_time": "2025-08-07T07:15:47.864231Z"
|
||||
}
|
||||
},
|
||||
"id": "3f73a8e113a58e9b",
|
||||
"execution_count": 4
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Example: Text Completion Model"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "7aca59af5cadce80"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" # Funkcja ponownie zwraca nową listę, bez zmienienia listy przekazanej jako argument w funkcji\n",
|
||||
" my_list = [16, 12, 16, 3, 2, 6]\n",
|
||||
" new_list = my_list[:]\n",
|
||||
" for x in range(len(new_list)):\n",
|
||||
" for y in range(len(new_list) - 1):\n",
|
||||
" if new_list[y] > new_list[y + 1]:\n",
|
||||
" new_list[y], new_list[y + 1] = new_list[y + 1], new_list[y]\n",
|
||||
" return new_list, my_list\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def bubble_sort_lib3(list): # Sortowanie z wykorzystaniem zewnętrznej biblioteki poza pętlą\n",
|
||||
" from itertools import permutations\n",
|
||||
" y = len(list)\n",
|
||||
" perms = []\n",
|
||||
" for a in range(0, y + 1):\n",
|
||||
" for subset in permutations(list, a):\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_aimlapi import AimlapiLLM\n",
|
||||
"\n",
|
||||
"llm = AimlapiLLM(\n",
|
||||
" model=\"gpt-3.5-turbo-instruct\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(llm.invoke(\"def bubble_sort(): \"))"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:16:22.595703Z",
|
||||
"start_time": "2025-08-07T07:16:19.410881Z"
|
||||
}
|
||||
},
|
||||
"id": "2af3be417769efc3",
|
||||
"execution_count": 6
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
Install the python SDK:
|
||||
|
||||
```bash
|
||||
pip install firecrawl-py==0.0.20
|
||||
pip install firecrawl-py
|
||||
```
|
||||
|
||||
## Document loader
|
||||
|
||||
129
docs/docs/integrations/providers/google-bigtable.mdx
Normal file
129
docs/docs/integrations/providers/google-bigtable.mdx
Normal file
@@ -0,0 +1,129 @@
|
||||
# Bigtable
|
||||
|
||||
Bigtable is a scalable, fully managed key-value and wide-column store ideal for fast access to structured, semi-structured, or unstructured data. This page provides an overview of Bigtable's LangChain integrations.
|
||||
|
||||
**Client Library Documentation:** [cloud.google.com/python/docs/reference/langchain-google-bigtable/latest](https://cloud.google.com/python/docs/reference/langchain-google-bigtable/latest)
|
||||
|
||||
**Product Documentation:** [cloud.google.com/bigtable](https://cloud.google.com/bigtable)
|
||||
|
||||
## Quick Start
|
||||
|
||||
To use this library, you first need to:
|
||||
|
||||
1. Select or create a Cloud Platform project.
|
||||
2. Enable billing for your project.
|
||||
3. Enable the Google Cloud Bigtable API.
|
||||
4. Set up Authentication.
|
||||
|
||||
## Installation
|
||||
|
||||
The main package for this integration is `langchain-google-bigtable`.
|
||||
|
||||
```bash
|
||||
pip install -U langchain-google-bigtable
|
||||
```
|
||||
|
||||
## Integrations
|
||||
|
||||
The `langchain-google-bigtable` package provides the following integrations:
|
||||
|
||||
### Vector Store
|
||||
|
||||
With `BigtableVectorStore`, you can store documents and their vector embeddings to find the most similar or relevant information in your database.
|
||||
|
||||
* **Full `VectorStore` Implementation:** Supports all methods from the LangChain `VectorStore` abstract class.
|
||||
* **Async/Sync Support:** All methods are available in both asynchronous and synchronous versions.
|
||||
* **Metadata Filtering:** Powerful filtering on metadata fields, including logical AND/OR combinations.
|
||||
* **Multiple Distance Strategies:** Supports both Cosine and Euclidean distance for similarity search.
|
||||
* **Customizable Storage:** Full control over how content, embeddings, and metadata are stored in Bigtable columns.
|
||||
|
||||
```python
|
||||
from langchain_google_bigtable import BigtableVectorStore
|
||||
|
||||
# Your embedding service and other configurations
|
||||
# embedding_service = ...
|
||||
|
||||
engine = await BigtableEngine.async_initialize(project_id="your-project-id")
|
||||
vector_store = await BigtableVectorStore.create(
|
||||
engine=engine,
|
||||
instance_id="your-instance-id",
|
||||
table_id="your-table-id",
|
||||
embedding_service=embedding_service,
|
||||
collection="your_collection_name",
|
||||
)
|
||||
await vector_store.aadd_documents([your_documents])
|
||||
results = await vector_store.asimilarity_search("your query")
|
||||
```
|
||||
|
||||
|
||||
Learn more in the [Vector Store how-to guide](https://colab.research.google.com/github/googleapis/langchain-google-bigtable-python/blob/main/docs/vector_store.ipynb).
|
||||
|
||||
### Key-value Store
|
||||
|
||||
Use `BigtableByteStore` as a persistent, scalable key-value store for caching, session management, or other storage needs. It supports both synchronous and asynchronous operations.
|
||||
|
||||
```python
|
||||
from langchain_google_bigtable import BigtableByteStore
|
||||
|
||||
# Initialize the store
|
||||
store = await BigtableByteStore.create(
|
||||
project_id="your-project-id",
|
||||
instance_id="your-instance-id",
|
||||
table_id="your-table-id",
|
||||
)
|
||||
|
||||
# Set and get values
|
||||
await store.amset([("key1", b"value1")])
|
||||
retrieved = await store.amget(["key1"])
|
||||
```
|
||||
|
||||
Learn more in the [Key-value Store how-to guide](https://cloud.google.com/python/docs/reference/langchain-google-bigtable/latest/key-value-store).
|
||||
|
||||
### Document Loader
|
||||
|
||||
Use the `BigtableLoader` to load data from a Bigtable table and represent it as LangChain `Document` objects.
|
||||
|
||||
```python
|
||||
from langchain_google_bigtable import BigtableLoader
|
||||
|
||||
loader = BigtableLoader(
|
||||
project_id="your-project-id",
|
||||
instance_id="your-instance-id",
|
||||
table_id="your-table-name"
|
||||
)
|
||||
docs = loader.load()
|
||||
```
|
||||
|
||||
Learn more in the [Document Loader how-to guide](https://cloud.google.com/python/docs/reference/langchain-google-bigtable/latest/document-loader).
|
||||
|
||||
### Chat Message History
|
||||
|
||||
Use `BigtableChatMessageHistory` to store conversation histories, enabling stateful chains and agents.
|
||||
|
||||
```python
|
||||
from langchain_google_bigtable import BigtableChatMessageHistory
|
||||
|
||||
history = BigtableChatMessageHistory(
|
||||
project_id="your-project-id",
|
||||
instance_id="your-instance-id",
|
||||
table_id="your-message-store",
|
||||
session_id="user-session-123"
|
||||
)
|
||||
|
||||
history.add_user_message("Hello!")
|
||||
history.add_ai_message("Hi there!")
|
||||
```
|
||||
|
||||
Learn more in the [Chat Message History how-to guide](https://cloud.google.com/python/docs/reference/langchain-google-bigtable/latest/chat-message-history).
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions to this library are welcome. Please see the CONTRIBUTING guide in the [package repo](https://github.com/googleapis/langchain-google-bigtable-python/) for more details
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the Apache 2.0 License - see the LICENSE file in the [package repo](https://github.com/googleapis/langchain-google-bigtable-python/blob/main/LICENSE) for details.
|
||||
|
||||
## Disclaimer
|
||||
|
||||
This is not an officially supported Google product.
|
||||
@@ -1,4 +1,4 @@
|
||||
# Oracle Cloud Infrastructure (OCI)
|
||||
# Oracle Cloud Infrastructure (OCI)
|
||||
|
||||
The `LangChain` integrations related to [Oracle Cloud Infrastructure](https://www.oracle.com/artificial-intelligence/).
|
||||
|
||||
@@ -11,16 +11,14 @@ The `LangChain` integrations related to [Oracle Cloud Infrastructure](https://ww
|
||||
To use, you should have the latest `oci` python SDK and the langchain_community package installed.
|
||||
|
||||
```bash
|
||||
pip install -U langchain_oci
|
||||
python -m pip install -U langchain-oci
|
||||
```
|
||||
|
||||
See [chat](/docs/integrations/llms/oci_generative_ai), [complete](/docs/integrations/chat/oci_generative_ai), and [embedding](/docs/integrations/text_embedding/oci_generative_ai) usage examples.
|
||||
See [chat](/docs/integrations/chat/oci_generative_ai), [complete](/docs/integrations/chat/oci_generative_ai), and [embedding](/docs/integrations/text_embedding/oci_generative_ai) usage examples.
|
||||
|
||||
```python
|
||||
from langchain_oci.chat_models import ChatOCIGenAI
|
||||
|
||||
from langchain_oci.llms import OCIGenAI
|
||||
|
||||
from langchain_oci.embeddings import OCIGenAIEmbeddings
|
||||
```
|
||||
|
||||
|
||||
140
docs/docs/integrations/providers/superlinked.mdx
Normal file
140
docs/docs/integrations/providers/superlinked.mdx
Normal file
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: Superlinked
|
||||
description: LangChain integration package for the Superlinked retrieval stack
|
||||
---
|
||||
|
||||
import Link from '@docusaurus/Link';
|
||||
|
||||
### Overview
|
||||
|
||||
Superlinked enables context‑aware retrieval using multiple space types (text similarity, categorical, numerical, recency, and more). The `langchain-superlinked` package provides a LangChain‑native `SuperlinkedRetriever` that plugs directly into your RAG chains.
|
||||
|
||||
### Links
|
||||
|
||||
- <Link to="https://github.com/superlinked/langchain-superlinked">Integration repository</Link>
|
||||
- <Link to="https://links.superlinked.com/langchain_repo_sl">Superlinked core repository</Link>
|
||||
- <Link to="https://links.superlinked.com/langchain_article">Article: Build RAG using LangChain & Superlinked</Link>
|
||||
|
||||
### Install
|
||||
|
||||
```bash
|
||||
pip install -U langchain-superlinked superlinked
|
||||
```
|
||||
|
||||
### Quickstart
|
||||
|
||||
```python
|
||||
import superlinked.framework as sl
|
||||
from langchain_superlinked import SuperlinkedRetriever
|
||||
|
||||
# 1) Define schema
|
||||
class DocumentSchema(sl.Schema):
|
||||
id: sl.IdField
|
||||
content: sl.String
|
||||
|
||||
doc_schema = DocumentSchema()
|
||||
|
||||
# 2) Define space and index
|
||||
text_space = sl.TextSimilaritySpace(
|
||||
text=doc_schema.content, model="sentence-transformers/all-MiniLM-L6-v2"
|
||||
)
|
||||
doc_index = sl.Index([text_space])
|
||||
|
||||
# 3) Define query
|
||||
query = (
|
||||
sl.Query(doc_index)
|
||||
.find(doc_schema)
|
||||
.similar(text_space.text, sl.Param("query_text"))
|
||||
.select([doc_schema.content])
|
||||
.limit(sl.Param("limit"))
|
||||
)
|
||||
|
||||
# 4) Minimal app setup
|
||||
source = sl.InMemorySource(schema=doc_schema)
|
||||
executor = sl.InMemoryExecutor(sources=[source], indices=[doc_index])
|
||||
app = executor.run()
|
||||
source.put([
|
||||
{"id": "1", "content": "Machine learning algorithms process data efficiently."},
|
||||
{"id": "2", "content": "Natural language processing understands human language."},
|
||||
])
|
||||
|
||||
# 5) LangChain retriever
|
||||
retriever = SuperlinkedRetriever(
|
||||
sl_client=app, sl_query=query, page_content_field="content"
|
||||
)
|
||||
|
||||
# Search
|
||||
docs = retriever.invoke("artificial intelligence", limit=2)
|
||||
for d in docs:
|
||||
print(d.page_content)
|
||||
```
|
||||
|
||||
### What the retriever expects (App and Query)
|
||||
|
||||
The retriever takes two core inputs:
|
||||
|
||||
- `sl_client`: a Superlinked App created by running an executor (e.g., `InMemoryExecutor(...).run()`)
|
||||
- `sl_query`: a `QueryDescriptor` returned by chaining `sl.Query(...).find(...).similar(...).select(...).limit(...)`
|
||||
|
||||
Minimal setup:
|
||||
|
||||
```python
|
||||
import superlinked.framework as sl
|
||||
from langchain_superlinked import SuperlinkedRetriever
|
||||
|
||||
class Doc(sl.Schema):
|
||||
id: sl.IdField
|
||||
content: sl.String
|
||||
|
||||
doc = Doc()
|
||||
space = sl.TextSimilaritySpace(text=doc.content, model="sentence-transformers/all-MiniLM-L6-v2")
|
||||
index = sl.Index([space])
|
||||
|
||||
query = (
|
||||
sl.Query(index)
|
||||
.find(doc)
|
||||
.similar(space.text, sl.Param("query_text"))
|
||||
.select([doc.content])
|
||||
.limit(sl.Param("limit"))
|
||||
)
|
||||
|
||||
source = sl.InMemorySource(schema=doc)
|
||||
app = sl.InMemoryExecutor(sources=[source], indices=[index]).run()
|
||||
|
||||
retriever = SuperlinkedRetriever(sl_client=app, sl_query=query, page_content_field="content")
|
||||
```
|
||||
|
||||
Note: For a persistent vector DB, pass `vector_database=...` to the executor (e.g., Qdrant) before `.run()`.
|
||||
|
||||
### Use within a chain
|
||||
|
||||
```python
|
||||
from langchain_core.runnables import RunnablePassthrough
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
def format_docs(docs):
|
||||
return "\n\n".join(doc.page_content for doc in docs)
|
||||
|
||||
prompt = ChatPromptTemplate.from_template(
|
||||
"""
|
||||
Answer based on context:\n\nContext: {context}\nQuestion: {question}
|
||||
"""
|
||||
)
|
||||
|
||||
chain = ({"context": retriever | format_docs, "question": RunnablePassthrough()}
|
||||
| prompt
|
||||
| ChatOpenAI())
|
||||
|
||||
answer = chain.invoke("How does machine learning work?")
|
||||
```
|
||||
|
||||
### Resources
|
||||
|
||||
- <Link to="https://pypi.org/project/langchain-superlinked/">PyPI: langchain-superlinked</Link>
|
||||
- <Link to="https://pypi.org/project/superlinked/">PyPI: superlinked</Link>
|
||||
- <Link to="https://github.com/superlinked/langchain-superlinked">Source repository</Link>
|
||||
- <Link to="https://links.superlinked.com/langchain_repo_sl">Superlinked core repository</Link>
|
||||
- <Link to="https://links.superlinked.com/langchain_article">Build RAG using LangChain & Superlinked (article)</Link>
|
||||
|
||||
|
||||
170
docs/docs/integrations/providers/timbr.mdx
Normal file
170
docs/docs/integrations/providers/timbr.mdx
Normal file
@@ -0,0 +1,170 @@
|
||||
# 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. This connector 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
|
||||
|
||||
## Quickstart
|
||||
|
||||
### 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 (or 'all')
|
||||
|
||||
```bash
|
||||
pip install 'langchain-timbr[<your selected providers, separated by comma without spaces>]'
|
||||
```
|
||||
|
||||
## 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}'
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Import and utilize your intended chain/node, or use TimbrLlmConnector to manually integrate with Timbr's semantic layer. For a complete agent working example, see the [Timbr tool page](/docs/integrations/tools/timbr).
|
||||
|
||||
### ExecuteTimbrQueryChain example
|
||||
|
||||
```python
|
||||
from langchain_timbr import ExecuteTimbrQueryChain
|
||||
|
||||
# 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")
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
### Multiple chains using SequentialChain example
|
||||
|
||||
```python
|
||||
from langchain.chains import SequentialChain
|
||||
from langchain_timbr import ExecuteTimbrQueryChain, GenerateAnswerChain
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
# 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")
|
||||
|
||||
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?"})
|
||||
```
|
||||
|
||||
## 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)
|
||||
68
docs/docs/integrations/providers/zenrows.ipynb
Normal file
68
docs/docs/integrations/providers/zenrows.ipynb
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "_MFfVhVCa15x"
|
||||
},
|
||||
"source": [
|
||||
"# ZenRows\n",
|
||||
"\n",
|
||||
"[ZenRows](https://www.zenrows.com/) is an enterprise-grade web scraping tool that provides advanced web data extraction capabilities at scale. ZenRows specializes in scraping modern websites, bypassing anti-bot systems, extracting structured data from any website, rendering JavaScript-heavy content, accessing geo-restricted websites, and more.\n",
|
||||
"\n",
|
||||
"[langchain-zenrows](https://pypi.org/project/langchain-zenrows/) provides tools that allow LLMs to access web data using ZenRows' powerful scraping infrastructure.\n",
|
||||
"\n",
|
||||
"## Installation and Setup\n",
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"pip install langchain-zenrows\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"You'll need to set up your ZenRows API key:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"import os\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"your-api-key\"\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Or you can pass it directly when initializing tools:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper(zenrows_api_key=\"your-api-key\")\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"## Tools\n",
|
||||
"\n",
|
||||
"### ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"The ZenRows integration provides comprehensive web scraping features:\n",
|
||||
"\n",
|
||||
"- **JavaScript Rendering**: Scrape modern SPAs and dynamic content\n",
|
||||
"- **Anti-Bot Bypass**: Overcome sophisticated bot detection systems \n",
|
||||
"- **Geo-Targeting**: Access region-specific content with 190+ countries\n",
|
||||
"- **Multiple Output Formats**: HTML, Markdown, Plaintext, PDF, Screenshots\n",
|
||||
"- **CSS Extraction**: Target specific data with CSS selectors\n",
|
||||
"- **Structured Data Extraction**: Automatically extract emails, phone numbers, links, and more\n",
|
||||
"- **Session Management**: Maintain consistent sessions across requests\n",
|
||||
"- **Premium Proxies**: Residential IPs for maximum success rates\n",
|
||||
"\n",
|
||||
"See more in the [ZenRows tool documentation](/docs/integrations/tools/zenrows_universal_scraper)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
603
docs/docs/integrations/providers/zeusdb.mdx
Normal file
603
docs/docs/integrations/providers/zeusdb.mdx
Normal file
@@ -0,0 +1,603 @@
|
||||
<p align="center" width="100%">
|
||||
<h1 align="center">LangChain ZeusDB Integration</h1>
|
||||
</p>
|
||||
|
||||
A high-performance LangChain integration for ZeusDB, bringing enterprise-grade vector search capabilities to your LangChain applications.
|
||||
|
||||
## Features
|
||||
|
||||
🚀 **High Performance**
|
||||
- Rust-powered vector database backend
|
||||
- Advanced HNSW indexing for sub-millisecond search
|
||||
- Product Quantization for 4x-256x memory compression
|
||||
- Concurrent search with automatic parallelization
|
||||
|
||||
🎯 **LangChain Native**
|
||||
- Full VectorStore API compliance
|
||||
- Async/await support for all operations
|
||||
- Seamless integration with LangChain retrievers
|
||||
- Maximal Marginal Relevance (MMR) search
|
||||
|
||||
🏢 **Enterprise Ready**
|
||||
- Structured logging with performance monitoring
|
||||
- Index persistence with complete state preservation
|
||||
- Advanced metadata filtering
|
||||
- Graceful error handling and fallback mechanisms
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
pip install -qU langchain-zeusdb
|
||||
```
|
||||
|
||||
### Getting Started
|
||||
|
||||
This example uses *OpenAIEmbeddings*, which requires an OpenAI API key - [Get your OpenAI API key here](https://platform.openai.com/api-keys)
|
||||
|
||||
If you prefer, you can also use this package with any other embedding provider (Hugging Face, Cohere, custom functions, etc.).
|
||||
```bash
|
||||
pip install langchain-openai
|
||||
```
|
||||
|
||||
```python
|
||||
import os
|
||||
import getpass
|
||||
|
||||
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')
|
||||
```
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```python
|
||||
from langchain_zeusdb import ZeusDBVectorStore
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
from zeusdb import VectorDatabase
|
||||
|
||||
# Initialize embeddings
|
||||
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
||||
|
||||
# Create ZeusDB index
|
||||
vdb = VectorDatabase()
|
||||
index = vdb.create(
|
||||
index_type="hnsw",
|
||||
dim=1536,
|
||||
space="cosine"
|
||||
)
|
||||
|
||||
# Create vector store
|
||||
vector_store = ZeusDBVectorStore(
|
||||
zeusdb_index=index,
|
||||
embedding=embeddings
|
||||
)
|
||||
|
||||
# Add documents
|
||||
from langchain_core.documents import Document
|
||||
|
||||
docs = [
|
||||
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
|
||||
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
|
||||
]
|
||||
|
||||
vector_store.add_documents(docs)
|
||||
|
||||
# Search
|
||||
results = vector_store.similarity_search("fast database", k=2)
|
||||
print(f"Found the following {len(results)} results:")
|
||||
print(results)
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
Found the following 2 results:
|
||||
[Document(id='ea2b4f13-b0b7-4cef-bb91-0fc4f4c41295', metadata={'source': 'docs'}, page_content='ZeusDB is fast'), Document(id='33dc1e87-a18a-4827-a0df-6ee47eabc7b2', metadata={'source': 'docs'}, page_content='LangChain is powerful')]
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
### Factory Methods
|
||||
|
||||
For convenience, you can create and populate a vector store in a single step:
|
||||
|
||||
**Example 1: - Create from texts (creates index and adds texts in one step)**
|
||||
```python
|
||||
vector_store_texts = ZeusDBVectorStore.from_texts(
|
||||
texts=["Hello world", "Goodbye world"],
|
||||
embedding=embeddings,
|
||||
metadatas=[{"source": "text1"}, {"source": "text2"}]
|
||||
)
|
||||
|
||||
print("texts store count:", vector_store_texts.get_vector_count()) # -> 2
|
||||
print("texts store peek:", vector_store_texts.zeusdb_index.list(2)) # [('id1', {...}), ('id2', {...})]
|
||||
|
||||
# Search the texts-based store
|
||||
results = vector_store_texts.similarity_search("Hello", k=1)
|
||||
print(f"Found in texts store: {results[0].page_content}") # -> "Hello world"
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
texts store count: 2
|
||||
texts store peek: [('e9c39b44-b610-4e00-91f3-bf652e9989ac', {'source': 'text1', 'text': 'Hello world'}), ('d33f210c-ed53-4006-a64a-a9eee397fec9', {'source': 'text2', 'text': 'Goodbye world'})]
|
||||
Found in texts store: Hello world
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
**Example 2: - Create from documents (creates index and adds documents in one step)**
|
||||
```python
|
||||
new_docs = [
|
||||
Document(page_content="Python is great", metadata={"source": "python"}),
|
||||
Document(page_content="JavaScript is flexible", metadata={"source": "js"}),
|
||||
]
|
||||
|
||||
vector_store_docs = ZeusDBVectorStore.from_documents(
|
||||
documents=new_docs,
|
||||
embedding=embeddings
|
||||
)
|
||||
|
||||
print("docs store count:", vector_store_docs.get_vector_count()) # -> 2
|
||||
print("docs store peek:", vector_store_docs.zeusdb_index.list(2)) # [('id3', {...}), ('id4', {...})]
|
||||
|
||||
# Search the documents-based store
|
||||
results = vector_store_docs.similarity_search("Python", k=1)
|
||||
print(f"Found in docs store: {results[0].page_content}") # -> "Python is great"
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
docs store count: 2
|
||||
docs store peek: [('aab2d1c1-7e02-4817-8dd8-6fb03570bb6f', {'text': 'Python is great', 'source': 'python'}), ('9a8a82cb-0e70-456c-9db2-556e464de14e', {'text': 'JavaScript is flexible', 'source': 'js'})]
|
||||
Found in docs store: Python is great
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
## Advanced Features
|
||||
|
||||
ZeusDB's enterprise-grade capabilities are fully integrated into the LangChain ecosystem, providing quantization, persistence, advanced search features and many other enterprise capabilities.
|
||||
|
||||
### Memory-Efficient Setup with Quantization
|
||||
|
||||
For large datasets, use Product Quantization to reduce memory usage:
|
||||
|
||||
```python
|
||||
# Create quantized index for memory efficiency
|
||||
quantization_config = {
|
||||
'type': 'pq',
|
||||
'subvectors': 8,
|
||||
'bits': 8,
|
||||
'training_size': 10000
|
||||
}
|
||||
|
||||
vdb = VectorDatabase()
|
||||
index = vdb.create(
|
||||
index_type="hnsw",
|
||||
dim=1536,
|
||||
space="cosine",
|
||||
quantization_config=quantization_config
|
||||
)
|
||||
|
||||
vector_store = ZeusDBVectorStore(
|
||||
zeusdb_index=index,
|
||||
embedding=embeddings
|
||||
)
|
||||
```
|
||||
|
||||
Please refer to our [documentation](https://docs.zeusdb.com/en/latest/vector_database/product_quantization.html) for helpful configuration guidelines and recommendations for setting up quantization.
|
||||
|
||||
<br />
|
||||
|
||||
### Persistence
|
||||
|
||||
ZeusDB persistence lets you save a fully populated index to disk and load it later with complete state restoration. This includes vectors, metadata, HNSW graph, and (if enabled) Product Quantization models.
|
||||
|
||||
What gets saved:
|
||||
- Vectors & IDs
|
||||
- Metadata
|
||||
- HNSW graph structure
|
||||
- Quantization config, centroids, and training state (if PQ is enabled)
|
||||
|
||||
|
||||
**How to Save your vector store**
|
||||
```python
|
||||
# Save index
|
||||
vector_store.save_index("my_index.zdb")
|
||||
```
|
||||
|
||||
**How to Load your vector store**
|
||||
```python
|
||||
# Load index
|
||||
loaded_store = ZeusDBVectorStore.load_index(
|
||||
path="my_index.zdb",
|
||||
embedding=embeddings
|
||||
)
|
||||
|
||||
# Verify after load
|
||||
print("vector count:", loaded_store.get_vector_count())
|
||||
print("index info:", loaded_store.info())
|
||||
print("store peek:", loaded_store.zeusdb_index.list(2))
|
||||
```
|
||||
|
||||
**Notes**
|
||||
- The path is a directory, not a single file. Ensure the target is writable.
|
||||
- Saved indexes are cross-platform and include format/version info for compatibility checks.
|
||||
- If you used PQ, both the compression model and state are preserved—no need to retrain after loading.
|
||||
- You can continue to use all vector store APIs (similarity_search, retrievers, etc.) on the loaded_store.
|
||||
|
||||
For further details (including file structure, and further comprehensive examples), see the [documentation](https://docs.zeusdb.com/en/latest/vector_database/persistence.html).
|
||||
|
||||
<br />
|
||||
|
||||
### Advanced Search Options
|
||||
|
||||
Use these to control scoring, diversity, metadata filtering, and retriever integration for your searches.
|
||||
|
||||
#### Similarity search with scores
|
||||
|
||||
Returns `(Document, raw_distance)` pairs from ZeusDB — **lower distance = more similar**.
|
||||
If you prefer normalized relevance in `[0, 1]`, use `similarity_search_with_relevance_scores`.
|
||||
|
||||
```python
|
||||
# Similarity search with scores
|
||||
results_with_scores = vector_store.similarity_search_with_score(
|
||||
query="machine learning",
|
||||
k=5
|
||||
)
|
||||
|
||||
print(results_with_scores)
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
[
|
||||
(Document(id='ac0eaf5b-9f02-4ce2-8957-c369a7262c61', metadata={'source': 'docs'}, page_content='LangChain is powerful'), 0.8218843340873718),
|
||||
(Document(id='faae3adf-7cf3-463c-b282-3790b096fa23', metadata={'source': 'docs'}, page_content='ZeusDB is fast'), 0.9140053391456604)
|
||||
]
|
||||
```
|
||||
|
||||
#### MMR search for diversity
|
||||
|
||||
MMR (Maximal Marginal Relevance) balances two forces: relevance to the query and diversity among selected results, reducing near-duplicate answers. Control the trade-off with lambda_mult (1.0 = all relevance, 0.0 = all diversity).
|
||||
|
||||
```python
|
||||
# MMR search for diversity
|
||||
mmr_results = vector_store.max_marginal_relevance_search(
|
||||
query="AI applications",
|
||||
k=5,
|
||||
fetch_k=20,
|
||||
lambda_mult=0.7 # Balance relevance vs diversity
|
||||
)
|
||||
|
||||
print(mmr_results)
|
||||
```
|
||||
|
||||
#### Search with metadata filtering
|
||||
|
||||
Filter results using document metadata you stored when adding docs
|
||||
|
||||
```python
|
||||
# Search with metadata filtering
|
||||
results = vector_store.similarity_search(
|
||||
query="database performance",
|
||||
k=3,
|
||||
filter={"source": "documentation"}
|
||||
)
|
||||
```
|
||||
|
||||
For supported metadata query types and operators, please refer to the [documentation](https://docs.zeusdb.com/en/latest/vector_database/metadata_filtering.html).
|
||||
|
||||
#### As a Retriever
|
||||
|
||||
Turning the vector store into a retriever gives you a standard LangChain interface that chains (e.g., RetrievalQA) can call to fetch context. Under the hood it uses your chosen search type (similarity or mmr) and search_kwargs.
|
||||
|
||||
```python
|
||||
# Convert to retriever for use in chains
|
||||
retriever = vector_store.as_retriever(
|
||||
search_type="mmr",
|
||||
search_kwargs={"k": 3, "lambda_mult": 0.8}
|
||||
)
|
||||
|
||||
# Use with LangChain Expression Language (LCEL) - requires only langchain-core
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_core.runnables import RunnablePassthrough
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
def format_docs(docs):
|
||||
return "\n\n".join([d.page_content for d in docs])
|
||||
|
||||
template = """Answer the question based only on the following context:
|
||||
{context}
|
||||
|
||||
Question: {question}
|
||||
"""
|
||||
|
||||
prompt = ChatPromptTemplate.from_template(template)
|
||||
llm = ChatOpenAI()
|
||||
|
||||
# Create a chain using LCEL
|
||||
chain = (
|
||||
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
||||
| prompt
|
||||
| llm
|
||||
| StrOutputParser()
|
||||
)
|
||||
|
||||
# Use the chain
|
||||
answer = chain.invoke("What is ZeusDB?")
|
||||
print(answer)
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
ZeusDB is a fast database management system.
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
## Async Support
|
||||
|
||||
ZeusDB supports asynchronous operations for non-blocking, concurrent vector operations.
|
||||
|
||||
**When to use async:** web servers (FastAPI/Starlette), agents/pipelines doing parallel searches, or notebooks where you want non-blocking/concurrent retrieval. If you're writing simple scripts, the sync methods are fine.
|
||||
|
||||
Those are **asynchronous operations** - the async/await versions of the regular synchronous methods. Here's what each one does:
|
||||
|
||||
1. `await vector_store.aadd_documents(documents)` - Asynchronously adds documents to the vector store (async version of `add_documents()`)
|
||||
2. `await vector_store.asimilarity_search("query", k=5)` - Asynchronously performs similarity search (async version of `similarity_search()`)
|
||||
3. `await vector_store.adelete(ids=["doc1", "doc2"])` - Asynchronously deletes documents by their IDs (async version of `delete()`)
|
||||
|
||||
The async versions are useful when:
|
||||
- You're building async applications (using `asyncio`, FastAPI, etc.)
|
||||
- You want non-blocking operations that can run concurrently
|
||||
- You're handling multiple requests simultaneously
|
||||
- You want better performance in I/O-bound applications
|
||||
|
||||
For example, instead of blocking while adding documents:
|
||||
|
||||
```python
|
||||
# Synchronous (blocking)
|
||||
vector_store.add_documents(docs) # Blocks until complete
|
||||
|
||||
# Asynchronous (non-blocking)
|
||||
await vector_store.aadd_documents(docs) # Can do other work while this runs
|
||||
```
|
||||
|
||||
All operations support async/await:
|
||||
|
||||
**Script version (`python my_script.py`):**
|
||||
```python
|
||||
import asyncio
|
||||
from langchain_zeusdb import ZeusDBVectorStore
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
from langchain_core.documents import Document
|
||||
from zeusdb import VectorDatabase
|
||||
|
||||
# Setup
|
||||
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
||||
vdb = VectorDatabase()
|
||||
index = vdb.create(index_type="hnsw", dim=1536, space="cosine")
|
||||
vector_store = ZeusDBVectorStore(zeusdb_index=index, embedding=embeddings)
|
||||
|
||||
docs = [
|
||||
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
|
||||
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
|
||||
]
|
||||
|
||||
async def main():
|
||||
# Add documents asynchronously
|
||||
ids = await vector_store.aadd_documents(docs)
|
||||
print("Added IDs:", ids)
|
||||
|
||||
# Run multiple searches concurrently
|
||||
results_fast, results_powerful = await asyncio.gather(
|
||||
vector_store.asimilarity_search("fast", k=2),
|
||||
vector_store.asimilarity_search("powerful", k=2),
|
||||
)
|
||||
print("Fast results:", [d.page_content for d in results_fast])
|
||||
print("Powerful results:", [d.page_content for d in results_powerful])
|
||||
|
||||
# Delete documents asynchronously
|
||||
deleted = await vector_store.adelete(ids=ids[:1])
|
||||
print("Deleted first doc:", deleted)
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
**Colab/Notebook/Jupyter version (top-level `await`):**
|
||||
```python
|
||||
from langchain_zeusdb import ZeusDBVectorStore
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
from langchain_core.documents import Document
|
||||
from zeusdb import VectorDatabase
|
||||
import asyncio
|
||||
|
||||
# Setup
|
||||
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
||||
vdb = VectorDatabase()
|
||||
index = vdb.create(index_type="hnsw", dim=1536, space="cosine")
|
||||
vector_store = ZeusDBVectorStore(zeusdb_index=index, embedding=embeddings)
|
||||
|
||||
docs = [
|
||||
Document(page_content="ZeusDB is fast", metadata={"source": "docs"}),
|
||||
Document(page_content="LangChain is powerful", metadata={"source": "docs"}),
|
||||
]
|
||||
|
||||
# Add documents asynchronously
|
||||
ids = await vector_store.aadd_documents(docs)
|
||||
print("Added IDs:", ids)
|
||||
|
||||
# Run multiple searches concurrently
|
||||
results_fast, results_powerful = await asyncio.gather(
|
||||
vector_store.asimilarity_search("fast", k=2),
|
||||
vector_store.asimilarity_search("powerful", k=2),
|
||||
)
|
||||
print("Fast results:", [d.page_content for d in results_fast])
|
||||
print("Powerful results:", [d.page_content for d in results_powerful])
|
||||
|
||||
# Delete documents asynchronously
|
||||
deleted = await vector_store.adelete(ids=ids[:1])
|
||||
print("Deleted first doc:", deleted)
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
Added IDs: ['9c440918-715f-49ba-9b97-0d991d29e997', 'ad59c645-d3ba-4a4a-a016-49ed39514123']
|
||||
Fast results: ['ZeusDB is fast', 'LangChain is powerful']
|
||||
Powerful results: ['LangChain is powerful', 'ZeusDB is fast']
|
||||
Deleted first doc: True
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
## Monitoring and Observability
|
||||
|
||||
### Performance Monitoring
|
||||
|
||||
```python
|
||||
# Get index statistics
|
||||
stats = vector_store.get_zeusdb_stats()
|
||||
print(f"Index size: {stats.get('total_vectors', '0')} vectors")
|
||||
print(f"Dimension: {stats.get('dimension')} | Space: {stats.get('space')} | Index type: {stats.get('index_type')}")
|
||||
|
||||
# Benchmark search performance
|
||||
performance = vector_store.benchmark_search_performance(
|
||||
query_count=100,
|
||||
max_threads=4
|
||||
)
|
||||
print(f"Search QPS: {performance.get('parallel_qps', 0):.0f}")
|
||||
|
||||
# Check quantization status
|
||||
if vector_store.is_quantized():
|
||||
progress = vector_store.get_training_progress()
|
||||
print(f"Quantization training: {progress:.1f}% complete")
|
||||
else:
|
||||
print("Index is not quantized")
|
||||
```
|
||||
|
||||
**Expected results:**
|
||||
```
|
||||
Index size: 2 vectors
|
||||
Dimension: 1536 | Space: cosine | Index type: HNSW
|
||||
Search QPS: 53807
|
||||
Index is not quantized
|
||||
```
|
||||
|
||||
### Enterprise Logging
|
||||
|
||||
ZeusDB includes enterprise-grade structured logging that works automatically with smart environment detection:
|
||||
|
||||
```python
|
||||
import logging
|
||||
|
||||
# ZeusDB automatically detects your environment and applies appropriate logging:
|
||||
# - Development: Human-readable logs, WARNING level
|
||||
# - Production: JSON structured logs, ERROR level
|
||||
# - Testing: Minimal output, CRITICAL level
|
||||
# - Jupyter: Clean readable logs, INFO level
|
||||
|
||||
# Operations are automatically logged with performance metrics
|
||||
vector_store.add_documents(docs)
|
||||
# Logs: {"operation":"vector_addition","total_inserted":2,"duration_ms":45}
|
||||
|
||||
# Control logging with environment variables if needed
|
||||
# ZEUSDB_LOG_LEVEL=debug ZEUSDB_LOG_FORMAT=json python your_app.py
|
||||
```
|
||||
|
||||
To learn more about the full features of ZeusDB's enterprise logging capabilities please read the following [documentation](https://docs.zeusdb.com/en/latest/vector_database/logging.html).
|
||||
|
||||
<br />
|
||||
|
||||
## Configuration Options
|
||||
|
||||
### Index Parameters
|
||||
|
||||
```python
|
||||
vdb = VectorDatabase()
|
||||
index = vdb.create(
|
||||
index_type="hnsw", # Index algorithm
|
||||
dim=1536, # Vector dimension
|
||||
space="cosine", # Distance metric: cosine, l2, l1
|
||||
m=16, # HNSW connectivity
|
||||
ef_construction=200, # Build-time search width
|
||||
expected_size=100000, # Expected number of vectors
|
||||
quantization_config=None # Optional quantization
|
||||
)
|
||||
```
|
||||
|
||||
### Search Parameters
|
||||
|
||||
```python
|
||||
results = vector_store.similarity_search(
|
||||
query="search query",
|
||||
k=5, # Number of results
|
||||
ef_search=None, # Runtime search width (auto if None)
|
||||
filter={"key": "value"} # Metadata filter
|
||||
)
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
The integration includes comprehensive error handling:
|
||||
|
||||
```python
|
||||
try:
|
||||
results = vector_store.similarity_search("query")
|
||||
print(results)
|
||||
except Exception as e:
|
||||
# Graceful degradation with logging
|
||||
print(f"Search failed: {e}")
|
||||
# Fallback logic here
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
- **Python**: 3.10 or higher
|
||||
- **ZeusDB**: 0.0.8 or higher
|
||||
- **LangChain Core**: 0.3.74 or higher
|
||||
|
||||
## Installation from Source
|
||||
|
||||
```bash
|
||||
git clone https://github.com/zeusdb/langchain-zeusdb.git
|
||||
cd langchain-zeusdb/libs/zeusdb
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
- **RAG Applications**: High-performance retrieval for question answering
|
||||
- **Semantic Search**: Fast similarity search across large document collections
|
||||
- **Recommendation Systems**: Vector-based content and collaborative filtering
|
||||
- **Embeddings Analytics**: Analysis of high-dimensional embedding spaces
|
||||
- **Real-time Applications**: Low-latency vector search for production systems
|
||||
|
||||
## Compatibility
|
||||
|
||||
### LangChain Versions
|
||||
- **LangChain Core**: 0.3.74+
|
||||
|
||||
### Distance Metrics
|
||||
- **Cosine**: Default, normalized similarity
|
||||
- **Euclidean (L2)**: Geometric distance
|
||||
- **Manhattan (L1)**: City-block distance
|
||||
|
||||
### Embedding Models
|
||||
Compatible with any embedding provider:
|
||||
- OpenAI (`text-embedding-3-small`, `text-embedding-3-large`)
|
||||
- Hugging Face Transformers
|
||||
- Cohere Embeddings
|
||||
- Custom embedding functions
|
||||
|
||||
## Support
|
||||
|
||||
- **Documentation**: [docs.zeusdb.com](https://docs.zeusdb.com)
|
||||
- **Issues**: [GitHub Issues](https://github.com/zeusdb/langchain-zeusdb/issues)
|
||||
- **Email**: contact@zeusdb.com
|
||||
|
||||
---
|
||||
|
||||
*Making vector search fast, scalable, and developer-friendly.*
|
||||
1292
docs/docs/integrations/retrievers/superlinked.ipynb
Normal file
1292
docs/docs/integrations/retrievers/superlinked.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
204
docs/docs/integrations/retrievers/superlinked_examples.ipynb
Normal file
204
docs/docs/integrations/retrievers/superlinked_examples.ipynb
Normal file
@@ -0,0 +1,204 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# SuperlinkedRetriever Examples\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to build a Superlinked App and Query Descriptor and use them with the LangChain `SuperlinkedRetriever`.\n",
|
||||
"\n",
|
||||
"Install the integration from PyPI:\n",
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"pip install -U langchain-superlinked superlinked\n",
|
||||
"```\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"Install the integration and its peer dependency:\n",
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"pip install -U langchain-superlinked superlinked\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"See below for creating a Superlinked App (`sl_client`) and a `QueryDescriptor` (`sl_query`), then wiring them into `SuperlinkedRetriever`.\n",
|
||||
"\n",
|
||||
"## Usage\n",
|
||||
"\n",
|
||||
"Call `retriever.invoke(query_text, **params)` to retrieve `Document` objects. Examples below show single-space and multi-space setups.\n",
|
||||
"\n",
|
||||
"## Use within a chain\n",
|
||||
"\n",
|
||||
"The retriever can be used in LangChain chains by piping it into your prompt and model. See the main Superlinked retriever page for a full RAG example.\n",
|
||||
"\n",
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"Refer to the API docs:\n",
|
||||
"\n",
|
||||
"- https://python.langchain.com/api_reference/superlinked/retrievers/langchain_superlinked.retrievers.SuperlinkedRetriever.html\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import superlinked.framework as sl\n",
|
||||
"from langchain_superlinked import SuperlinkedRetriever\n",
|
||||
"from datetime import timedelta\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Define schema\n",
|
||||
"class DocumentSchema(sl.Schema):\n",
|
||||
" id: sl.IdField\n",
|
||||
" content: sl.String\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"doc_schema = DocumentSchema()\n",
|
||||
"\n",
|
||||
"# Space + index\n",
|
||||
"text_space = sl.TextSimilaritySpace(\n",
|
||||
" text=doc_schema.content, model=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||
")\n",
|
||||
"doc_index = sl.Index([text_space])\n",
|
||||
"\n",
|
||||
"# Query descriptor\n",
|
||||
"query = (\n",
|
||||
" sl.Query(doc_index)\n",
|
||||
" .find(doc_schema)\n",
|
||||
" .similar(text_space.text, sl.Param(\"query_text\"))\n",
|
||||
" .select([doc_schema.content])\n",
|
||||
" .limit(sl.Param(\"limit\"))\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Minimal app\n",
|
||||
"source = sl.InMemorySource(schema=doc_schema)\n",
|
||||
"executor = sl.InMemoryExecutor(sources=[source], indices=[doc_index])\n",
|
||||
"app = executor.run()\n",
|
||||
"\n",
|
||||
"# Data\n",
|
||||
"source.put(\n",
|
||||
" [\n",
|
||||
" {\"id\": \"1\", \"content\": \"Machine learning algorithms process data efficiently.\"},\n",
|
||||
" {\n",
|
||||
" \"id\": \"2\",\n",
|
||||
" \"content\": \"Natural language processing understands human language.\",\n",
|
||||
" },\n",
|
||||
" {\"id\": \"3\", \"content\": \"Deep learning models require significant compute.\"},\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Retriever\n",
|
||||
"retriever = SuperlinkedRetriever(\n",
|
||||
" sl_client=app, sl_query=query, page_content_field=\"content\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"retriever.invoke(\"artificial intelligence\", limit=2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Multi-space example (blog posts)\n",
|
||||
"class BlogPostSchema(sl.Schema):\n",
|
||||
" id: sl.IdField\n",
|
||||
" title: sl.String\n",
|
||||
" content: sl.String\n",
|
||||
" category: sl.String\n",
|
||||
" published_date: sl.Timestamp\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"blog = BlogPostSchema()\n",
|
||||
"\n",
|
||||
"content_space = sl.TextSimilaritySpace(\n",
|
||||
" text=blog.content, model=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||
")\n",
|
||||
"title_space = sl.TextSimilaritySpace(\n",
|
||||
" text=blog.title, model=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||
")\n",
|
||||
"cat_space = sl.CategoricalSimilaritySpace(\n",
|
||||
" category_input=blog.category, categories=[\"technology\", \"science\", \"business\"]\n",
|
||||
")\n",
|
||||
"recency_space = sl.RecencySpace(\n",
|
||||
" timestamp=blog.published_date,\n",
|
||||
" period_time_list=[\n",
|
||||
" sl.PeriodTime(timedelta(days=30)),\n",
|
||||
" sl.PeriodTime(timedelta(days=90)),\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"blog_index = sl.Index([content_space, title_space, cat_space, recency_space])\n",
|
||||
"\n",
|
||||
"blog_query = (\n",
|
||||
" sl.Query(\n",
|
||||
" blog_index,\n",
|
||||
" weights={\n",
|
||||
" content_space: sl.Param(\"content_weight\"),\n",
|
||||
" title_space: sl.Param(\"title_weight\"),\n",
|
||||
" cat_space: sl.Param(\"category_weight\"),\n",
|
||||
" recency_space: sl.Param(\"recency_weight\"),\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" .find(blog)\n",
|
||||
" .similar(content_space.text, sl.Param(\"query_text\"))\n",
|
||||
" .select([blog.title, blog.content, blog.category, blog.published_date])\n",
|
||||
" .limit(sl.Param(\"limit\"))\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"source = sl.InMemorySource(schema=blog)\n",
|
||||
"app = sl.InMemoryExecutor(sources=[source], indices=[blog_index]).run()\n",
|
||||
"\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"source.put(\n",
|
||||
" [\n",
|
||||
" {\n",
|
||||
" \"id\": \"p1\",\n",
|
||||
" \"title\": \"Intro to ML\",\n",
|
||||
" \"content\": \"Machine learning 101\",\n",
|
||||
" \"category\": \"technology\",\n",
|
||||
" \"published_date\": int((datetime.now() - timedelta(days=5)).timestamp()),\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"id\": \"p2\",\n",
|
||||
" \"title\": \"AI in Healthcare\",\n",
|
||||
" \"content\": \"Transforming diagnosis\",\n",
|
||||
" \"category\": \"science\",\n",
|
||||
" \"published_date\": int((datetime.now() - timedelta(days=15)).timestamp()),\n",
|
||||
" },\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"blog_retriever = SuperlinkedRetriever(\n",
|
||||
" sl_client=app,\n",
|
||||
" sl_query=blog_query,\n",
|
||||
" page_content_field=\"content\",\n",
|
||||
" metadata_fields=[\"title\", \"category\", \"published_date\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"blog_retriever.invoke(\n",
|
||||
" \"machine learning\", content_weight=1.0, recency_weight=0.5, limit=2\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
483
docs/docs/integrations/stores/bigtable.ipynb
Normal file
483
docs/docs/integrations/stores/bigtable.ipynb
Normal file
@@ -0,0 +1,483 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: Google Bigtable\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# BigtableByteStore\n",
|
||||
"\n",
|
||||
"This guide covers how to use Google Cloud Bigtable as a key-value store.\n",
|
||||
"\n",
|
||||
"[Bigtable](https://cloud.google.com/bigtable) is a key-value and wide-column store, ideal for fast access to structured, semi-structured, or unstructured data. \n",
|
||||
"\n",
|
||||
"[](https://colab.research.google.com/github/googleapis/langchain-google-bigtable-python/blob/main/docs/key_value_store.ipynb)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"The `BigtableByteStore` uses Google Cloud Bigtable as a backend for a key-value store. It supports synchronous and asynchronous operations for setting, getting, and deleting key-value pairs.\n",
|
||||
"\n",
|
||||
"### Integration details\n",
|
||||
"| Class | Package | Local | JS support | Package downloads | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: | :---: |\n",
|
||||
"| [BigtableByteStore](https://github.com/googleapis/langchain-google-bigtable-python/blob/main/src/langchain_google_bigtable/key_value_store.py) | [langchain-google-bigtable](https://pypi.org/project/langchain-google-bigtable/) | ❌ | ❌ |  |  |"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"### Prerequisites\n",
|
||||
"\n",
|
||||
"To get started, you will need a Google Cloud project with an active Bigtable instance and table. \n",
|
||||
"* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n",
|
||||
"* [Enable the Bigtable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable.googleapis.com)\n",
|
||||
"* [Create a Bigtable instance and table](https://cloud.google.com/bigtable/docs/creating-instance)\n",
|
||||
"\n",
|
||||
"### Installation\n",
|
||||
"\n",
|
||||
"The integration is in the `langchain-google-bigtable` package. The command below also installs `langchain-google-vertexai` for the embedding cache example."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install -qU langchain-google-bigtable langchain-google-vertexai"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ☁ Set Your Google Cloud Project\n",
|
||||
"Set your Google Cloud project to use its resources within this notebook.\n",
|
||||
"\n",
|
||||
"If you don't know your project ID, you can run `gcloud config list` or see the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @markdown Please fill in your project, instance, and table details.\n",
|
||||
"PROJECT_ID = \"your-gcp-project-id\" # @param {type:\"string\"}\n",
|
||||
"INSTANCE_ID = \"your-instance-id\" # @param {type:\"string\"}\n",
|
||||
"TABLE_ID = \"your-table-id\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"!gcloud config set project {PROJECT_ID}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 🔐 Authentication\n",
|
||||
"Authenticate to Google Cloud to access your project resources.\n",
|
||||
"- For **Colab**, use the cell below.\n",
|
||||
"- For **Vertex AI Workbench**, see the [setup instructions](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.colab import auth\n",
|
||||
"\n",
|
||||
"auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"To use `BigtableByteStore`, we first ensure a table exists and then initialize a `BigtableEngine` to manage connections."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable import (\n",
|
||||
" BigtableByteStore,\n",
|
||||
" BigtableEngine,\n",
|
||||
" init_key_value_store_table,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Ensure the table and column family exist.\n",
|
||||
"init_key_value_store_table(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" instance_id=INSTANCE_ID,\n",
|
||||
" table_id=TABLE_ID,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### BigtableEngine\n",
|
||||
"A `BigtableEngine` object handles the execution context for the store, especially for async operations. It's recommended to initialize a single engine and reuse it across multiple stores for better performance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Initialize the engine to manage async operations.\n",
|
||||
"engine = await BigtableEngine.async_initialize(\n",
|
||||
" project_id=PROJECT_ID, instance_id=INSTANCE_ID\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### BigtableByteStore\n",
|
||||
"\n",
|
||||
"This is the main class for interacting with the key-value store. It provides the methods for setting, getting, and deleting data."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Initialize the store.\n",
|
||||
"store = await BigtableByteStore.create(engine=engine, table_id=TABLE_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Usage\n",
|
||||
"\n",
|
||||
"The store supports both sync (`mset`, `mget`) and async (`amset`, `amget`) methods. This guide uses the async versions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Set\n",
|
||||
"Use `amset` to save key-value pairs to the store."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"kv_pairs = [\n",
|
||||
" (\"key1\", b\"value1\"),\n",
|
||||
" (\"key2\", b\"value2\"),\n",
|
||||
" (\"key3\", b\"value3\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"await store.amset(kv_pairs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Get\n",
|
||||
"Use `amget` to retrieve values. If a key is not found, `None` is returned for that key."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"retrieved_vals = await store.amget([\"key1\", \"key2\", \"nonexistent_key\"])\n",
|
||||
"print(retrieved_vals)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Delete\n",
|
||||
"Use `amdelete` to remove keys from the store."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"await store.amdelete([\"key3\"])\n",
|
||||
"\n",
|
||||
"# Verifying the key was deleted\n",
|
||||
"await store.amget([\"key1\", \"key3\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Iterate over keys\n",
|
||||
"Use `ayield_keys` to iterate over all keys or keys with a specific prefix."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"all_keys = [key async for key in store.ayield_keys()]\n",
|
||||
"print(f\"All keys: {all_keys}\")\n",
|
||||
"\n",
|
||||
"prefixed_keys = [key async for key in store.ayield_keys(prefix=\"key1\")]\n",
|
||||
"print(f\"Prefixed keys: {prefixed_keys}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Advanced Usage: Embedding Caching\n",
|
||||
"\n",
|
||||
"A common use case for a key-value store is to cache expensive operations like computing text embeddings, which saves time and cost."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.embeddings import CacheBackedEmbeddings\n",
|
||||
"from langchain_google_vertexai.embeddings import VertexAIEmbeddings\n",
|
||||
"\n",
|
||||
"underlying_embeddings = VertexAIEmbeddings(\n",
|
||||
" project=PROJECT_ID, model_name=\"textembedding-gecko@003\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Use a namespace to avoid key collisions with other data.\n",
|
||||
"cached_embedder = CacheBackedEmbeddings.from_bytes_store(\n",
|
||||
" underlying_embeddings, store, namespace=\"text-embeddings\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"First call (computes and caches embedding):\")\n",
|
||||
"%time embedding_result_1 = await cached_embedder.aembed_query(\"Hello, world!\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"\\nSecond call (retrieves from cache):\")\n",
|
||||
"%time embedding_result_2 = await cached_embedder.aembed_query(\"Hello, world!\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### As a Simple Document Retriever\n",
|
||||
"\n",
|
||||
"This section shows how to create a simple retriever using the Bigtable store. It acts as a document persistence layer, fetching documents that match a query prefix."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.retrievers import BaseRetriever\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"from langchain_core.callbacks import CallbackManagerForRetrieverRun\n",
|
||||
"from typing import List, Optional, Any, Union\n",
|
||||
"import json\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class SimpleKVStoreRetriever(BaseRetriever):\n",
|
||||
" \"\"\"A simple retriever that retrieves documents based on a prefix match in the key-value store.\"\"\"\n",
|
||||
"\n",
|
||||
" store: BigtableByteStore\n",
|
||||
" documents: List[Union[Document, str]]\n",
|
||||
" k: int\n",
|
||||
"\n",
|
||||
" def set_up_store(self):\n",
|
||||
" kv_pairs_to_set = []\n",
|
||||
" for i, doc in enumerate(self.documents):\n",
|
||||
" if isinstance(doc, str):\n",
|
||||
" doc = Document(page_content=doc)\n",
|
||||
" if not doc.id:\n",
|
||||
" doc.id = str(i)\n",
|
||||
" value = (\n",
|
||||
" \"Page Content\\n\"\n",
|
||||
" + doc.page_content\n",
|
||||
" + \"\\nMetadata\"\n",
|
||||
" + json.dumps(doc.metadata)\n",
|
||||
" )\n",
|
||||
" kv_pairs_to_set.append((doc.id, value.encode(\"utf-8\")))\n",
|
||||
" self.store.mset(kv_pairs_to_set)\n",
|
||||
"\n",
|
||||
" async def _aget_relevant_documents(\n",
|
||||
" self,\n",
|
||||
" query: str,\n",
|
||||
" *,\n",
|
||||
" run_manager: Optional[CallbackManagerForRetrieverRun] = None,\n",
|
||||
" ) -> List[Document]:\n",
|
||||
" keys = [key async for key in self.store.ayield_keys(prefix=query)][: self.k]\n",
|
||||
" documents_retrieved = []\n",
|
||||
" async for document in await self.store.amget(keys):\n",
|
||||
" if document:\n",
|
||||
" document_str = document.decode(\"utf-8\")\n",
|
||||
" page_content = document_str.split(\"Content\\n\")[1].split(\"\\nMetadata\")[0]\n",
|
||||
" metadata = json.loads(document_str.split(\"\\nMetadata\")[1])\n",
|
||||
" documents_retrieved.append(\n",
|
||||
" Document(page_content=page_content, metadata=metadata)\n",
|
||||
" )\n",
|
||||
" return documents_retrieved\n",
|
||||
"\n",
|
||||
" def _get_relevant_documents(\n",
|
||||
" self,\n",
|
||||
" query: str,\n",
|
||||
" *,\n",
|
||||
" run_manager: Optional[CallbackManagerForRetrieverRun] = None,\n",
|
||||
" ) -> list[Document]:\n",
|
||||
" keys = [key for key in self.store.yield_keys(prefix=query)][: self.k]\n",
|
||||
" documents_retrieved = []\n",
|
||||
" for document in self.store.mget(keys):\n",
|
||||
" if document:\n",
|
||||
" document_str = document.decode(\"utf-8\")\n",
|
||||
" page_content = document_str.split(\"Content\\n\")[1].split(\"\\nMetadata\")[0]\n",
|
||||
" metadata = json.loads(document_str.split(\"\\nMetadata\")[1])\n",
|
||||
" documents_retrieved.append(\n",
|
||||
" Document(page_content=page_content, metadata=metadata)\n",
|
||||
" )\n",
|
||||
" return documents_retrieved"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"documents = [\n",
|
||||
" Document(\n",
|
||||
" page_content=\"Goldfish are popular pets for beginners, requiring relatively simple care.\",\n",
|
||||
" metadata={\"type\": \"fish\", \"trait\": \"low maintenance\"},\n",
|
||||
" id=\"fish#Goldfish\",\n",
|
||||
" ),\n",
|
||||
" Document(\n",
|
||||
" page_content=\"Cats are independent pets that often enjoy their own space.\",\n",
|
||||
" metadata={\"type\": \"cat\", \"trait\": \"independence\"},\n",
|
||||
" id=\"mammals#Cats\",\n",
|
||||
" ),\n",
|
||||
" Document(\n",
|
||||
" page_content=\"Rabbits are social animals that need plenty of space to hop around.\",\n",
|
||||
" metadata={\"type\": \"rabbit\", \"trait\": \"social\"},\n",
|
||||
" id=\"mammals#Rabbits\",\n",
|
||||
" ),\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"retriever_store = BigtableByteStore.create_sync(\n",
|
||||
" engine=engine, instance_id=INSTANCE_ID, table_id=TABLE_ID\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"KVDocumentRetriever = SimpleKVStoreRetriever(\n",
|
||||
" store=retriever_store, documents=documents, k=2\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"KVDocumentRetriever.set_up_store()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"KVDocumentRetriever.invoke(\"fish\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"KVDocumentRetriever.invoke(\"mammals\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For full details on the `BigtableByteStore` class, see the source code on [GitHub](https://github.com/googleapis/langchain-google-bigtable-python/blob/main/src/langchain_google_bigtable/key_value_store.py)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
319
docs/docs/integrations/text_embedding/aimlapi.ipynb
Normal file
319
docs/docs/integrations/text_embedding/aimlapi.ipynb
Normal file
@@ -0,0 +1,319 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: AI/ML API Embeddings\n",
|
||||
"---"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "24ae9a5bcf0c8c19"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# AimlapiEmbeddings\n",
|
||||
"\n",
|
||||
"This will help you get started with AI/ML API embedding models using LangChain. For detailed documentation on `AimlapiEmbeddings` features and configuration options, please refer to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"import { ItemTable } from \"@theme/FeatureTables\";\n",
|
||||
"\n",
|
||||
"<ItemTable category=\"text_embedding\" item=\"AI/ML API\" />\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"To access AI/ML API embedding models you'll need to create an account, get an API key, and install the `langchain-aimlapi` integration package.\n",
|
||||
"\n",
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"Head to [https://aimlapi.com/app/](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration) to sign up and generate an API key. Once you've done this, set the `AIMLAPI_API_KEY` environment variable:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "4af58f76e6ce897a"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"AIMLAPI_API_KEY\"):\n",
|
||||
" os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:50:37.393789Z",
|
||||
"start_time": "2025-08-07T07:50:27.679399Z"
|
||||
}
|
||||
},
|
||||
"id": "3297a770bc0b2b88",
|
||||
"execution_count": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "da319ae795659a93"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
|
||||
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:50:40.840377Z",
|
||||
"start_time": "2025-08-07T07:50:40.837144Z"
|
||||
}
|
||||
},
|
||||
"id": "6869f433a2f9dc3e",
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Installation\n",
|
||||
"\n",
|
||||
"The LangChain AI/ML API integration lives in the `langchain-aimlapi` package:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "3f6de2cfc36a4dba"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install -qU langchain-aimlapi"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:50:50.693835Z",
|
||||
"start_time": "2025-08-07T07:50:41.453138Z"
|
||||
}
|
||||
},
|
||||
"id": "23c22092f806aa31",
|
||||
"execution_count": 3
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"Now we can instantiate our embeddings model and perform embedding operations:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "db718f4b551164f3"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_aimlapi import AimlapiEmbeddings\n",
|
||||
"\n",
|
||||
"embeddings = AimlapiEmbeddings(\n",
|
||||
" model=\"text-embedding-ada-002\",\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:51:03.046723Z",
|
||||
"start_time": "2025-08-07T07:50:50.694842Z"
|
||||
}
|
||||
},
|
||||
"id": "88b86f20598af88e",
|
||||
"execution_count": 4
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Indexing and Retrieval\n",
|
||||
"\n",
|
||||
"Embedding models are often used in retrieval-augmented generation (RAG) flows. Below is how to index and retrieve data using the `embeddings` object we initialized above with `InMemoryVectorStore`."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "847447f4ff1fe82a"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "'LangChain is the framework for building context-aware reasoning applications'"
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.vectorstores import InMemoryVectorStore\n",
|
||||
"\n",
|
||||
"text = \"LangChain is the framework for building context-aware reasoning applications\"\n",
|
||||
"\n",
|
||||
"vectorstore = InMemoryVectorStore.from_texts(\n",
|
||||
" [text],\n",
|
||||
" embedding=embeddings,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"retriever = vectorstore.as_retriever()\n",
|
||||
"\n",
|
||||
"retrieved_documents = retriever.invoke(\"What is LangChain?\")\n",
|
||||
"retrieved_documents[0].page_content"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:51:05.421030Z",
|
||||
"start_time": "2025-08-07T07:51:03.047729Z"
|
||||
}
|
||||
},
|
||||
"id": "595ccebd97dabeef",
|
||||
"execution_count": 5
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Direct Usage\n",
|
||||
"\n",
|
||||
"You can directly call `embed_query` and `embed_documents` for custom embedding scenarios.\n",
|
||||
"\n",
|
||||
"### Embed single text:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "aa922f78938d1ae1"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[-0.0011368310078978539, 0.00714730704203248, -0.014703838154673576, -0.034064359962940216, 0.011239\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"single_vector = embeddings.embed_query(text)\n",
|
||||
"print(str(single_vector)[:100])"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:51:06.285037Z",
|
||||
"start_time": "2025-08-07T07:51:05.422035Z"
|
||||
}
|
||||
},
|
||||
"id": "c06952ac53aab22",
|
||||
"execution_count": 6
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Embed multiple texts:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "52c9b7de79992a7b"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[-0.0011398226488381624, 0.007080476265400648, -0.014682820066809654, -0.03407655283808708, 0.011276\n",
|
||||
"[-0.005510928109288216, 0.016650190576910973, -0.011078780516982079, -0.03116573952138424, -0.003735\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"text2 = (\n",
|
||||
" \"LangGraph is a library for building stateful, multi-actor applications with LLMs\"\n",
|
||||
")\n",
|
||||
"two_vectors = embeddings.embed_documents([text, text2])\n",
|
||||
"for vector in two_vectors:\n",
|
||||
" print(str(vector)[:100])"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-07T07:51:07.954778Z",
|
||||
"start_time": "2025-08-07T07:51:06.285544Z"
|
||||
}
|
||||
},
|
||||
"id": "f1dcf3c389e11cc1",
|
||||
"execution_count": 7
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## API Reference\n",
|
||||
"\n",
|
||||
"For detailed documentation on `AimlapiEmbeddings` features and configuration options, please refer to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "a45ff6faef63cab2"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -26,13 +26,11 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install -U langchain_oci"
|
||||
]
|
||||
"execution_count": null,
|
||||
"source": "!pip install -U langchain-oci"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -75,9 +73,9 @@
|
||||
"\n",
|
||||
"# use default authN method API-key\n",
|
||||
"embeddings = OCIGenAIEmbeddings(\n",
|
||||
" model_id=\"MY_EMBEDDING_MODEL\",\n",
|
||||
" model_id=\"cohere.embed-v4.0\",\n",
|
||||
" service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n",
|
||||
" compartment_id=\"MY_OCID\",\n",
|
||||
" compartment_id=\"compartment_id\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -42,7 +42,9 @@
|
||||
"source": [
|
||||
"### Prerequisites\n",
|
||||
"\n",
|
||||
"Ensure you have the Oracle Python Client driver installed to facilitate the integration of Langchain with Oracle AI Vector Search."
|
||||
"You'll need to install `langchain-oracledb` with `python -m pip install -U langchain-oracledb` to use this integration.\n",
|
||||
"\n",
|
||||
"The `python-oracledb` driver is installed automatically as a dependency of langchain-oracledb."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -51,7 +53,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# pip install oracledb"
|
||||
"# python -m pip install -U langchain-oracledb"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -113,7 +115,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.embeddings.oracleai import OracleEmbeddings\n",
|
||||
"from langchain_oracledb.embeddings.oracleai import OracleEmbeddings\n",
|
||||
"\n",
|
||||
"# Update the directory and file names for your ONNX model\n",
|
||||
"# make sure that you have onnx file in the system\n",
|
||||
@@ -223,7 +225,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.embeddings.oracleai import OracleEmbeddings\n",
|
||||
"from langchain_oracledb.embeddings.oracleai import OracleEmbeddings\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
@@ -237,10 +239,10 @@
|
||||
"\n",
|
||||
"# using huggingface\n",
|
||||
"embedder_params = {\n",
|
||||
" \"provider\": \"huggingface\", \n",
|
||||
" \"credential_name\": \"HF_CRED\", \n",
|
||||
" \"url\": \"https://api-inference.huggingface.co/pipeline/feature-extraction/\", \n",
|
||||
" \"model\": \"sentence-transformers/all-MiniLM-L6-v2\", \n",
|
||||
" \"provider\": \"huggingface\",\n",
|
||||
" \"credential_name\": \"HF_CRED\",\n",
|
||||
" \"url\": \"https://api-inference.huggingface.co/pipeline/feature-extraction/\",\n",
|
||||
" \"model\": \"sentence-transformers/all-MiniLM-L6-v2\",\n",
|
||||
" \"wait_for_model\": \"true\"\n",
|
||||
"}\n",
|
||||
"\"\"\"\n",
|
||||
|
||||
@@ -42,7 +42,9 @@
|
||||
"source": [
|
||||
"### Prerequisites\n",
|
||||
"\n",
|
||||
"Please install Oracle Python Client driver to use Langchain with Oracle AI Vector Search. "
|
||||
"You'll need to install `langchain-oracledb` with `python -m pip install -U langchain-oracledb` to use this integration.\n",
|
||||
"\n",
|
||||
"The `python-oracledb` driver is installed automatically as a dependency of langchain-oracledb."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -51,7 +53,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# pip install oracledb"
|
||||
"# python -m pip install -U langchain-oracledb"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -123,7 +125,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.utilities.oracleai import OracleSummary\n",
|
||||
"from langchain_oracledb.utilities.oracleai import OracleSummary\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
|
||||
350
docs/docs/integrations/tools/timbr.ipynb
Normal file
350
docs/docs/integrations/tools/timbr.ipynb
Normal file
@@ -0,0 +1,350 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "2ce4bdbc",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "raw"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: timbr\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6f91f20",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Timbr\n",
|
||||
"\n",
|
||||
"[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.\n",
|
||||
"\n",
|
||||
"This notebook provides a quick overview for getting started with Timbr tools and agents. For more information about Timbr visit [Timbr.ai](https://timbr.ai/) or the [Timbr Documentation](https://docs.timbr.ai/doc/docs/integration/langchain-sdk/)\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"Timbr package for LangChain is [langchain-timbr](https://pypi.org/project/langchain-timbr), which provides seamless integration with Timbr's semantic layer for natural language to SQL conversion.\n",
|
||||
"\n",
|
||||
"### Tool features\n",
|
||||
"\n",
|
||||
"| Tool Name | Description |\n",
|
||||
"| :--- | :--- |\n",
|
||||
"| `IdentifyTimbrConceptChain` | Identify relevant concepts from user prompts |\n",
|
||||
"| `GenerateTimbrSqlChain` | Generate SQL queries from natural language prompts |\n",
|
||||
"| `ValidateTimbrSqlChain` | Validate SQL queries against Timbr knowledge graph schemas |\n",
|
||||
"| `ExecuteTimbrQueryChain` | Execute SQL queries against Timbr knowledge graph databases |\n",
|
||||
"| `GenerateAnswerChain` | Generate human-readable answers from query results |\n",
|
||||
"| `TimbrSqlAgent` | End-to-end SQL agent for natural language queries |\n",
|
||||
"\n",
|
||||
"### TimbrSqlAgent Parameters\n",
|
||||
"\n",
|
||||
"The `TimbrSqlAgent` is a pre-built agent that combines all the above tools for end-to-end natural language to SQL processing.\n",
|
||||
"\n",
|
||||
"For the complete list of parameters and detailed documentation, see: [TimbrSqlAgent Documentation](https://docs.timbr.ai/doc/docs/integration/langchain-sdk/#timbr-sql-agent)\n",
|
||||
"\n",
|
||||
"| Parameter | Type | Required | Description |\n",
|
||||
"| :--- | :--- | :--- | :--- |\n",
|
||||
"| `llm` | BaseChatModel | Yes | Language model instance (ChatOpenAI, ChatAnthropic, etc.) |\n",
|
||||
"| `url` | str | Yes | Timbr application URL |\n",
|
||||
"| `token` | str | Yes | Timbr API token |\n",
|
||||
"| `ontology` | str | Yes | Knowledge graph ontology name |\n",
|
||||
"| `schema` | str | No | Database schema name |\n",
|
||||
"| `concept` | str | No | Specific concept to focus on |\n",
|
||||
"| `concepts_list` | List[str] | No | List of relevant concepts |\n",
|
||||
"| `views_list` | List[str] | No | List of available views |\n",
|
||||
"| `note` | str | No | Additional context or instructions |\n",
|
||||
"| `retries` | int | No | Number of retry attempts (default: 3) |\n",
|
||||
"| `should_validate_sql` | bool | No | Whether to validate generated SQL (default: True) |\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"The integration lives in the `langchain-timbr` package.\n",
|
||||
"\n",
|
||||
"In this example, we'll use OpenAI for the LLM provider."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f85b4089",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --quiet -U langchain-timbr[openai]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b15e9266",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"You'll need Timbr credentials to use the tools. Get your API token from your Timbr application's API settings."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e0b178a2-8816-40ca-b57c-ccdd86dde9c9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# Set up Timbr credentials\n",
|
||||
"if not os.environ.get(\"TIMBR_URL\"):\n",
|
||||
" os.environ[\"TIMBR_URL\"] = input(\"Timbr URL:\\n\")\n",
|
||||
"\n",
|
||||
"if not os.environ.get(\"TIMBR_TOKEN\"):\n",
|
||||
" os.environ[\"TIMBR_TOKEN\"] = getpass.getpass(\"Timbr API Token:\\n\")\n",
|
||||
"\n",
|
||||
"if not os.environ.get(\"TIMBR_ONTOLOGY\"):\n",
|
||||
" os.environ[\"TIMBR_ONTOLOGY\"] = input(\"Timbr Ontology:\\n\")\n",
|
||||
"\n",
|
||||
"if not os.environ.get(\"OPENAI_API_KEY\"):\n",
|
||||
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1c97218f-f366-479d-8bf7-fe9f2f6df73f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"Instantiate Timbr tools and agents. First, let's set up the LLM and basic Timbr chains:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8b3ddfe9-ca79-494c-a7ab-1f56d9407a64",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_timbr import (\n",
|
||||
" ExecuteTimbrQueryChain,\n",
|
||||
" GenerateAnswerChain,\n",
|
||||
" TimbrSqlAgent,\n",
|
||||
" LlmWrapper,\n",
|
||||
" LlmTypes,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set up the LLM\n",
|
||||
"# from langchain_openai import ChatOpenAI\n",
|
||||
"# llm = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Alternative: Use Timbr's LlmWrapper for an easy LLM setup\n",
|
||||
"llm = LlmWrapper(\n",
|
||||
" llm_type=LlmTypes.OpenAI, api_key=os.environ[\"OPENAI_API_KEY\"], model=\"gpt-4o\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Instantiate Timbr chains\n",
|
||||
"execute_timbr_query_chain = ExecuteTimbrQueryChain(\n",
|
||||
" llm=llm,\n",
|
||||
" url=os.environ[\"TIMBR_URL\"],\n",
|
||||
" token=os.environ[\"TIMBR_TOKEN\"],\n",
|
||||
" ontology=os.environ[\"TIMBR_ONTOLOGY\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"generate_answer_chain = GenerateAnswerChain(\n",
|
||||
" llm=llm, url=os.environ[\"TIMBR_URL\"], token=os.environ[\"TIMBR_TOKEN\"]\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "74147a1a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Invocation\n",
|
||||
"\n",
|
||||
"### Execute SQL queries from natural language\n",
|
||||
"\n",
|
||||
"You can use the individual chains to perform specific operations:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "65310a8b-eb0c-4d9e-a618-4f4abe2414fc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Execute a natural language query\n",
|
||||
"result = execute_timbr_query_chain.invoke(\n",
|
||||
" {\"prompt\": \"What are the total sales for last month?\"}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"SQL Query:\", result[\"sql\"])\n",
|
||||
"print(\"Results:\", result[\"rows\"])\n",
|
||||
"print(\"Concept:\", result[\"concept\"])\n",
|
||||
"\n",
|
||||
"# Generate a human-readable answer from the results\n",
|
||||
"answer_result = generate_answer_chain.invoke(\n",
|
||||
" {\"prompt\": \"What are the total sales for last month?\", \"rows\": result[\"rows\"]}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Human-readable answer:\", answer_result[\"answer\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d6e73897",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Use within an agent\n",
|
||||
"\n",
|
||||
"### Using TimbrSqlAgent\n",
|
||||
"\n",
|
||||
"The `TimbrSqlAgent` provides an end-to-end solution that combines concept identification, SQL generation, validation, execution, and answer generation:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f90e33a7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.agents import AgentExecutor\n",
|
||||
"\n",
|
||||
"# Create a TimbrSqlAgent with all parameters\n",
|
||||
"timbr_agent = TimbrSqlAgent(\n",
|
||||
" llm=llm,\n",
|
||||
" url=os.environ[\"TIMBR_URL\"],\n",
|
||||
" token=os.environ[\"TIMBR_TOKEN\"],\n",
|
||||
" ontology=os.environ[\"TIMBR_ONTOLOGY\"],\n",
|
||||
" concepts_list=[\"Sales\", \"Orders\"], # optional\n",
|
||||
" views_list=[\"sales_view\"], # optional\n",
|
||||
" note=\"Focus on monthly aggregations\", # optional\n",
|
||||
" retries=3, # optional\n",
|
||||
" should_validate_sql=True, # optional\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Use the agent for end-to-end natural language to answer processing\n",
|
||||
"agent_result = AgentExecutor.from_agent_and_tools(\n",
|
||||
" agent=timbr_agent,\n",
|
||||
" tools=[], # No tools needed as we're directly using the chain\n",
|
||||
" verbose=True,\n",
|
||||
").invoke(\"Show me the top 5 customers by total sales amount this year\")\n",
|
||||
"\n",
|
||||
"print(\"Final Answer:\", agent_result[\"answer\"])\n",
|
||||
"print(\"Generated SQL:\", agent_result[\"sql\"])\n",
|
||||
"print(\"Usage Metadata:\", agent_result.get(\"usage_metadata\", {}))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "659f9fbd-6fcf-445f-aa8c-72d8e60154bd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sequential Chains\n",
|
||||
"\n",
|
||||
"You can combine multiple Timbr chains using LangChain's SequentialChain for custom workflows:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "af3123ad-7a02-40e5-b58e-7d56e23e5830",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import SequentialChain\n",
|
||||
"\n",
|
||||
"# Create a sequential pipeline\n",
|
||||
"pipeline = SequentialChain(\n",
|
||||
" chains=[execute_timbr_query_chain, generate_answer_chain],\n",
|
||||
" input_variables=[\"prompt\"],\n",
|
||||
" output_variables=[\"answer\", \"sql\", \"rows\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Execute the pipeline\n",
|
||||
"pipeline_result = pipeline.invoke(\n",
|
||||
" {\"prompt\": \"What are the average order values by customer segment?\"}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Pipeline Result:\", pipeline_result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "fdbf35b5-3aaf-4947-9ec6-48c21533fb95",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Example: Accessing usage metadata from Timbr operations\n",
|
||||
"result_with_metadata = execute_timbr_query_chain.invoke(\n",
|
||||
" {\"prompt\": \"How many orders were placed last quarter?\"}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Extract usage metadata\n",
|
||||
"usage_metadata = result_with_metadata.get(\"execute_timbr_usage_metadata\", {})\n",
|
||||
"determine_concept_usage = usage_metadata.get(\"determine_concept\", {})\n",
|
||||
"generate_sql_usage = usage_metadata.get(\"generate_sql\", {})\n",
|
||||
"\n",
|
||||
"print(determine_concept_usage)\n",
|
||||
"\n",
|
||||
"print(\n",
|
||||
" \"Concept determination token estimate:\",\n",
|
||||
" determine_concept_usage.get(\"approximate\", \"N/A\"),\n",
|
||||
")\n",
|
||||
"print(\n",
|
||||
" \"Concept determination tokens:\",\n",
|
||||
" determine_concept_usage.get(\"token_usage\", {}).get(\"total_tokens\", \"N/A\"),\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"SQL generation token estimate:\", generate_sql_usage.get(\"approximate\", \"N/A\"))\n",
|
||||
"print(\n",
|
||||
" \"SQL generation tokens:\",\n",
|
||||
" generate_sql_usage.get(\"token_usage\", {}).get(\"total_tokens\", \"N/A\"),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4ac8146c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"- [PyPI](https://pypi.org/project/langchain-timbr)\n",
|
||||
"- [GitHub](https://github.com/WPSemantix/langchain-timbr)\n",
|
||||
"- [LangChain Timbr Documentation](https://docs.timbr.ai/doc/docs/integration/langchain-sdk/)\n",
|
||||
"- [LangGraph Timbr Documentation](https://docs.timbr.ai/doc/docs/integration/langgraph-sdk)\n",
|
||||
"- [Timbr Official Website](https://timbr.ai/)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
281
docs/docs/integrations/tools/zenrows_universal_scraper.ipynb
Normal file
281
docs/docs/integrations/tools/zenrows_universal_scraper.ipynb
Normal file
@@ -0,0 +1,281 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "FVo_qZB6crBs"
|
||||
},
|
||||
"source": [
|
||||
"# ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"[ZenRows](https://www.zenrows.com/) is an enterprise-grade web scraping tool that provides advanced web data extraction capabilities at scale. For more information about ZenRows and its Universal Scraper API, visit the [official documentation](https://docs.zenrows.com/universal-scraper-api/).\n",
|
||||
"\n",
|
||||
"This document provides a quick overview for getting started with ZenRowsUniversalScraper tool. For detailed documentation of all ZenRowsUniversalScraper features and configurations head to the [API reference](https://github.com/ZenRows-Hub/langchain-zenrows?tab=readme-ov-file#api-reference).\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"### Integration details\n",
|
||||
"\n",
|
||||
"| Class | Package | JS support | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: |\n",
|
||||
"| [ZenRowsUniversalScraper](https://pypi.org/project/langchain-zenrows/) | [langchain-zenrows](https://pypi.org/project/langchain-zenrows/) | ❌ |  |\n",
|
||||
"\n",
|
||||
"### Tool features\n",
|
||||
"\n",
|
||||
"| Feature | Support |\n",
|
||||
"| :--- | :---: |\n",
|
||||
"| **JavaScript Rendering** | ✅ |\n",
|
||||
"| **Anti-Bot Bypass** | ✅ |\n",
|
||||
"| **Geo-Targeting** | ✅ |\n",
|
||||
"| **Multiple Output Formats** | ✅ |\n",
|
||||
"| **CSS Extraction** | ✅ |\n",
|
||||
"| **Screenshot Capture** | ✅ |\n",
|
||||
"| **Session Management** | ✅ |\n",
|
||||
"| **Premium Proxies** | ✅ |\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"Install the required dependencies."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"id": "henNSgOlcww5"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pip install langchain-zenrows"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "IS2yw_UaczgP"
|
||||
},
|
||||
"source": [
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"You'll need a ZenRows API key to use this tool. You can sign up for free at [ZenRows](https://app.zenrows.com/register?prod=universal_scraper)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"id": "Z097qruic2iH"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# Set your ZenRows API key\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"<YOUR_ZENROWS_API_KEY>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "hB7fHgmQc5eh"
|
||||
},
|
||||
"source": [
|
||||
"## Instantiation\n",
|
||||
"\n",
|
||||
"Here's how to instantiate an instance of the ZenRowsUniversalScraper tool."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"id": "ezdGcI3Hc8H3"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"# Set your ZenRows API key\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"<YOUR_ZENROWS_API_KEY>\"\n",
|
||||
"\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cUal-Ioic_0k"
|
||||
},
|
||||
"source": [
|
||||
"You can also pass the ZenRows API key when initializing the ZenRowsUniversalScraper tool."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"id": "sPd95HKzdCGr"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper(zenrows_api_key=\"your-api-key\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c8rEvAY4dFX2"
|
||||
},
|
||||
"source": [
|
||||
"## Invocation\n",
|
||||
"\n",
|
||||
"### Basic Usage\n",
|
||||
"\n",
|
||||
"The tool accepts a URL and various optional parameters to customize the scraping behavior:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "GKTDKhXEdGku"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"# Set your ZenRows API key\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"<YOUR_ZENROWS_API_KEY>\"\n",
|
||||
"\n",
|
||||
"# Initialize the tool\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper()\n",
|
||||
"\n",
|
||||
"# Scrape a simple webpage\n",
|
||||
"result = zenrows_scraper_tool.invoke({\"url\": \"https://httpbin.io/html\"})\n",
|
||||
"print(result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "7Kd1loN5dJbt"
|
||||
},
|
||||
"source": [
|
||||
"### Advanced Usage with Parameters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "NfJOQdBhdLrp"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"\n",
|
||||
"# Set your ZenRows API key\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"<YOUR_ZENROWS_API_KEY>\"\n",
|
||||
"\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper()\n",
|
||||
"\n",
|
||||
"# Scrape with JavaScript rendering and premium proxies\n",
|
||||
"result = zenrows_scraper_tool.invoke(\n",
|
||||
" {\n",
|
||||
" \"url\": \"https://www.scrapingcourse.com/ecommerce/\",\n",
|
||||
" \"js_render\": True,\n",
|
||||
" \"premium_proxy\": True,\n",
|
||||
" \"proxy_country\": \"us\",\n",
|
||||
" \"response_type\": \"markdown\",\n",
|
||||
" \"wait\": 2000,\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "8eivshtqdNe0"
|
||||
},
|
||||
"source": [
|
||||
"### Use within an agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "JmbPF7xadPgK"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain_openai import ChatOpenAI # or your preferred LLM\n",
|
||||
"from langchain_zenrows import ZenRowsUniversalScraper\n",
|
||||
"from langgraph.prebuilt import create_react_agent\n",
|
||||
"\n",
|
||||
"# Set your ZenRows and OpenAI API keys\n",
|
||||
"os.environ[\"ZENROWS_API_KEY\"] = \"<YOUR_ZENROWS_API_KEY>\"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPEN_AI_API_KEY>\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initialize components\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
|
||||
"zenrows_scraper_tool = ZenRowsUniversalScraper()\n",
|
||||
"\n",
|
||||
"# Create agent\n",
|
||||
"agent = create_react_agent(llm, [zenrows_scraper_tool])\n",
|
||||
"\n",
|
||||
"# Use the agent\n",
|
||||
"result = agent.invoke(\n",
|
||||
" {\n",
|
||||
" \"messages\": \"Scrape https://news.ycombinator.com/ and list the top 3 stories with title, points, comments, username, and time.\"\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Agent Response:\")\n",
|
||||
"for message in result[\"messages\"]:\n",
|
||||
" print(f\"{message.content}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "k9lqlhoAdRSb"
|
||||
},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For detailed documentation of all ZenRowsUniversalScraper features and configurations head to the [**ZenRowsUniversalScraper API reference**](https://github.com/ZenRows-Hub/langchain-zenrows).\n",
|
||||
"\n",
|
||||
"For comprehensive information about the underlying API parameters and capabilities, see the [ZenRows Universal API documentation](https://docs.zenrows.com/universal-scraper-api/api-reference)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
839
docs/docs/integrations/vectorstores/bigtable.ipynb
Normal file
839
docs/docs/integrations/vectorstores/bigtable.ipynb
Normal file
@@ -0,0 +1,839 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "7fb27b941602401d91542211134fc71a",
|
||||
"metadata": {
|
||||
"id": "7fb27b941602401d91542211134fc71a"
|
||||
},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: Google Bigtable\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "acae54e37e7d407bbb7b55eff062a284",
|
||||
"metadata": {
|
||||
"id": "acae54e37e7d407bbb7b55eff062a284"
|
||||
},
|
||||
"source": [
|
||||
"# BigtableVectorStore\n",
|
||||
"\n",
|
||||
"This guide covers the `BigtableVectorStore` integration for using Google Cloud Bigtable as a vector store.\n",
|
||||
"\n",
|
||||
"[Bigtable](https://cloud.google.com/bigtable) is a key-value and wide-column store, ideal for fast access to structured, semi-structured, or unstructured data.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a63283cbaf04dbcab1f6479b197f3a8",
|
||||
"metadata": {
|
||||
"id": "9a63283cbaf04dbcab1f6479b197f3a8"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"The `BigtableVectorStore` uses Google Cloud Bigtable to store documents and their vector embeddings for similarity search and retrieval. It supports powerful metadata filtering to refine search results.\n",
|
||||
"\n",
|
||||
"### Integration details\n",
|
||||
"| Class | Package | Local | JS support | Package downloads | Package latest |\n",
|
||||
"| :--- | :--- | :---: | :---: | :---: | :---: |\n",
|
||||
"| [BigtableVectorStore](https://github.com/googleapis/langchain-google-bigtable-python/blob/main/src/langchain_google_bigtable/vector_store.py) | [langchain-google-bigtable](https://pypi.org/project/langchain-google-bigtable/) | ❌ | ❌ |  |  |"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8dd0d8092fe74a7c96281538738b07e2",
|
||||
"metadata": {
|
||||
"id": "8dd0d8092fe74a7c96281538738b07e2"
|
||||
},
|
||||
"source": [
|
||||
"## Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "72eea5119410473aa328ad9291626812",
|
||||
"metadata": {
|
||||
"id": "72eea5119410473aa328ad9291626812"
|
||||
},
|
||||
"source": [
|
||||
"### Prerequisites\n",
|
||||
"\n",
|
||||
"To get started, you will need a Google Cloud project with an active Bigtable instance.\n",
|
||||
"* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n",
|
||||
"* [Enable the Bigtable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable.googleapis.com)\n",
|
||||
"* [Create a Bigtable instance](https://cloud.google.com/bigtable/docs/creating-instance)\n",
|
||||
"\n",
|
||||
"### Installation\n",
|
||||
"\n",
|
||||
"The integration is in the `langchain-google-bigtable` package. The command below also installs `langchain-google-vertexai` to use for an embedding service."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8edb47106e1a46a883d545849b8ab81b",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "8edb47106e1a46a883d545849b8ab81b",
|
||||
"outputId": "b6c95f84-f271-4bd0-f024-81ea38ce7f80"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install -qU langchain-google-bigtable langchain-google-vertexai"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "WEparXIIO41L",
|
||||
"metadata": {
|
||||
"id": "WEparXIIO41L"
|
||||
},
|
||||
"source": [
|
||||
"**Colab only**: Uncomment the following cell to restart the kernel or use the button to restart the kernel. For Vertex AI Workbench you can restart the terminal using the button on top."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "OB8Mg8HxO9HV",
|
||||
"metadata": {
|
||||
"id": "OB8Mg8HxO9HV"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# import IPython\n",
|
||||
"\n",
|
||||
"# app = IPython.Application.instance()\n",
|
||||
"# app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "10185d26023b46108eb7d9f57d49d2b3",
|
||||
"metadata": {
|
||||
"id": "10185d26023b46108eb7d9f57d49d2b3"
|
||||
},
|
||||
"source": [
|
||||
"### Set Your Google Cloud Project\n",
|
||||
"Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook.\n",
|
||||
"\n",
|
||||
"If you don't know your project ID, try the following:\n",
|
||||
"\n",
|
||||
"* Run `gcloud config list`.\n",
|
||||
"* Run `gcloud projects list`.\n",
|
||||
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8763a12b2bbd4a93a75aff182afb95dc",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "8763a12b2bbd4a93a75aff182afb95dc",
|
||||
"outputId": "865ca13d-47e1-4458-dfe3-96b0e7a57810"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @markdown Please fill in your project, instance, and a new table name.\n",
|
||||
"PROJECT_ID = \"google.com:cloud-bigtable-dev\" # @param {type:\"string\"}\n",
|
||||
"INSTANCE_ID = \"anweshadas-test\" # @param {type:\"string\"}\n",
|
||||
"TABLE_ID = \"your-vector-store-table-3\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"!gcloud config set project {PROJECT_ID}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "xx0JMrbNOfnV",
|
||||
"metadata": {
|
||||
"id": "xx0JMrbNOfnV"
|
||||
},
|
||||
"source": [
|
||||
"### 🔐 Authentication\n",
|
||||
"\n",
|
||||
"Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.\n",
|
||||
"\n",
|
||||
"- If you are using Colab to run this notebook, use the cell below and continue.\n",
|
||||
"- If you are using Vertex AI Workbench, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "T1pPsDCzOURd",
|
||||
"metadata": {
|
||||
"id": "T1pPsDCzOURd"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.colab import auth\n",
|
||||
"\n",
|
||||
"auth.authenticate_user(project_id=PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7623eae2785240b9bd12b16a66d81610",
|
||||
"metadata": {
|
||||
"id": "7623eae2785240b9bd12b16a66d81610"
|
||||
},
|
||||
"source": [
|
||||
"## Initialization\n",
|
||||
"\n",
|
||||
"Initializing the `BigtableVectorStore` involves three steps: setting up the embedding service, ensuring the Bigtable table is created, and configuring the store's parameters."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7cdc8c89c7104fffa095e18ddfef8986",
|
||||
"metadata": {
|
||||
"id": "7cdc8c89c7104fffa095e18ddfef8986"
|
||||
},
|
||||
"source": [
|
||||
"### 1. Set up Embedding Service\n",
|
||||
"First, we need a model to create the vector embeddings for our documents. We'll use a Vertex AI model for this example."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b118ea5561624da68c537baed56e602f",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "b118ea5561624da68c537baed56e602f",
|
||||
"outputId": "99b55b9a-61c7-4dbe-bf1f-dd84ddc434da"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_vertexai import VertexAIEmbeddings\n",
|
||||
"\n",
|
||||
"embeddings = VertexAIEmbeddings(project=PROJECT_ID, model_name=\"gemini-embedding-001\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "938c804e27f84196a10c8828c723f798",
|
||||
"metadata": {
|
||||
"id": "938c804e27f84196a10c8828c723f798"
|
||||
},
|
||||
"source": [
|
||||
"### 2. Initialize a Table\n",
|
||||
"Before creating a `BigtableVectorStore`, a table with the correct column families must exist. The `init_vector_store_table` helper function is the recommended way to create and configure a table. If the table already exists, it will do nothing."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "504fb2a444614c0babb325280ed9130a",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "504fb2a444614c0babb325280ed9130a",
|
||||
"outputId": "2e6453bc-5eed-4a3e-a8d4-59e945485be6"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable.vector_store import init_vector_store_table\n",
|
||||
"\n",
|
||||
"DATA_COLUMN_FAMILY = \"doc_data\"\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" init_vector_store_table(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" instance_id=INSTANCE_ID,\n",
|
||||
" table_id=TABLE_ID,\n",
|
||||
" content_column_family=DATA_COLUMN_FAMILY,\n",
|
||||
" embedding_column_family=DATA_COLUMN_FAMILY,\n",
|
||||
" )\n",
|
||||
" print(f\"Table '{TABLE_ID}' is ready.\")\n",
|
||||
"except ValueError as e:\n",
|
||||
" print(e)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "59bbdb311c014d738909a11f9e486628",
|
||||
"metadata": {
|
||||
"id": "59bbdb311c014d738909a11f9e486628"
|
||||
},
|
||||
"source": [
|
||||
"### 3. Configure the Vector Store\n",
|
||||
"Now we define the parameters that control how the vector store connects to Bigtable and how it handles data."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b43b363d81ae4b689946ece5c682cd59",
|
||||
"metadata": {
|
||||
"id": "b43b363d81ae4b689946ece5c682cd59"
|
||||
},
|
||||
"source": [
|
||||
"#### The BigtableEngine\n",
|
||||
"A `BigtableEngine` object manages clients and async operations. It is highly recommended to initialize a single engine and reuse it across multiple stores for better performance and resource management."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8a65eabff63a45729fe45fb5ade58bdc",
|
||||
"metadata": {
|
||||
"id": "8a65eabff63a45729fe45fb5ade58bdc"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable import BigtableEngine\n",
|
||||
"\n",
|
||||
"engine = await BigtableEngine.async_initialize(project_id=PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c3933fab20d04ec698c2621248eb3be0",
|
||||
"metadata": {
|
||||
"id": "c3933fab20d04ec698c2621248eb3be0"
|
||||
},
|
||||
"source": [
|
||||
"#### Collections\n",
|
||||
"A `collection` provides a logical namespace for your documents within a single Bigtable table. It is used as a prefix for the row keys, allowing multiple vector stores to coexist in the same table without interfering with each other."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4dd4641cc4064e0191573fe9c69df29b",
|
||||
"metadata": {
|
||||
"id": "4dd4641cc4064e0191573fe9c69df29b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"collection_name = \"my_docs\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8309879909854d7188b41380fd92a7c3",
|
||||
"metadata": {
|
||||
"id": "8309879909854d7188b41380fd92a7c3"
|
||||
},
|
||||
"source": [
|
||||
"#### Metadata Configuration\n",
|
||||
"When creating a `BigtableVectorStore`, you have two optional parameters for handling metadata:\n",
|
||||
"\n",
|
||||
"* `metadata_mappings`: This is a list of `VectorMetadataMapping` objects. You **must** define a mapping for any metadata key you wish to use for filtering in your search queries. Each mapping specifies the data type (`encoding`) for the metadata field, which is crucial for correct filtering.\n",
|
||||
"* `metadata_as_json_column`: This is an optional `ColumnConfig` that tells the store to save the *entire* metadata dictionary as a single JSON string in a specific column. This is useful for efficiently retrieving all of a document's metadata at once, including fields not defined in `metadata_mappings`. **Note:** Fields stored only in this JSON column cannot be used for filtering."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3ed186c9a28b402fb0bc4494df01f08d",
|
||||
"metadata": {
|
||||
"id": "3ed186c9a28b402fb0bc4494df01f08d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable import ColumnConfig, VectorMetadataMapping, Encoding\n",
|
||||
"\n",
|
||||
"# Define mappings for metadata fields you want to filter on.\n",
|
||||
"metadata_mappings = [\n",
|
||||
" VectorMetadataMapping(metadata_key=\"author\", encoding=Encoding.UTF8),\n",
|
||||
" VectorMetadataMapping(metadata_key=\"year\", encoding=Encoding.INT_BIG_ENDIAN),\n",
|
||||
" VectorMetadataMapping(metadata_key=\"category\", encoding=Encoding.UTF8),\n",
|
||||
" VectorMetadataMapping(metadata_key=\"rating\", encoding=Encoding.FLOAT),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"# Define the optional column for storing all metadata as a single JSON string.\n",
|
||||
"metadata_as_json_column = ColumnConfig(\n",
|
||||
" column_family=DATA_COLUMN_FAMILY, column_qualifier=\"metadata_json\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cb1e1581032b452c9409d6c6813c49d1",
|
||||
"metadata": {
|
||||
"id": "cb1e1581032b452c9409d6c6813c49d1"
|
||||
},
|
||||
"source": [
|
||||
"### 4. Create the BigtableVectorStore Instance"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "iKM4BktZR56p",
|
||||
"metadata": {
|
||||
"id": "iKM4BktZR56p"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Configure the columns for your store.\n",
|
||||
"content_column = ColumnConfig(\n",
|
||||
" column_family=DATA_COLUMN_FAMILY, column_qualifier=\"content\"\n",
|
||||
")\n",
|
||||
"embedding_column = ColumnConfig(\n",
|
||||
" column_family=DATA_COLUMN_FAMILY, column_qualifier=\"embedding\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "379cbbc1e968416e875cc15c1202d7eb",
|
||||
"metadata": {
|
||||
"id": "379cbbc1e968416e875cc15c1202d7eb"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable import BigtableVectorStore\n",
|
||||
"\n",
|
||||
"vector_store = await BigtableVectorStore.create(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" instance_id=INSTANCE_ID,\n",
|
||||
" table_id=TABLE_ID,\n",
|
||||
" engine=engine,\n",
|
||||
" embedding_service=embeddings,\n",
|
||||
" collection=collection_name,\n",
|
||||
" metadata_mappings=metadata_mappings,\n",
|
||||
" metadata_as_json_column=metadata_as_json_column,\n",
|
||||
" content_column=content_column,\n",
|
||||
" embedding_column=embedding_column,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "277c27b1587741f2af2001be3712ef0d",
|
||||
"metadata": {
|
||||
"id": "277c27b1587741f2af2001be3712ef0d"
|
||||
},
|
||||
"source": [
|
||||
"## Manage vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "db7b79bc585a40fcaf58bf750017e135",
|
||||
"metadata": {
|
||||
"id": "db7b79bc585a40fcaf58bf750017e135"
|
||||
},
|
||||
"source": [
|
||||
"### Add Documents\n",
|
||||
"You can add documents with pre-defined IDs. If a `Document` is added without an `id` attribute, the vector store will automatically generate a **`uuid4` string** for it."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "916684f9a58a4a2aa5f864670399430d",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "916684f9a58a4a2aa5f864670399430d",
|
||||
"outputId": "eb343088-624a-41a1-94cd-53e0c3cfa207"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"docs_to_add = [\n",
|
||||
" Document(\n",
|
||||
" page_content=\"A young farm boy, Luke Skywalker, is thrust into a galactic conflict.\",\n",
|
||||
" id=\"doc_1\",\n",
|
||||
" metadata={\n",
|
||||
" \"author\": \"George Lucas\",\n",
|
||||
" \"year\": 1977,\n",
|
||||
" \"category\": \"sci-fi\",\n",
|
||||
" \"rating\": 4.8,\n",
|
||||
" },\n",
|
||||
" ),\n",
|
||||
" Document(\n",
|
||||
" page_content=\"A hobbit named Frodo Baggins must destroy a powerful ring.\",\n",
|
||||
" id=\"doc_2\",\n",
|
||||
" metadata={\n",
|
||||
" \"author\": \"J.R.R. Tolkien\",\n",
|
||||
" \"year\": 1954,\n",
|
||||
" \"category\": \"fantasy\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" },\n",
|
||||
" ),\n",
|
||||
" # Document without a pre-defined ID, one will be generated.\n",
|
||||
" Document(\n",
|
||||
" page_content=\"A group of children confront an evil entity emerging from the sewers.\",\n",
|
||||
" metadata={\"author\": \"Stephen King\", \"year\": 1986, \"category\": \"horror\"},\n",
|
||||
" ),\n",
|
||||
" Document(\n",
|
||||
" page_content=\"In a distant future, the noble House Atreides rules the desert planet Arrakis.\",\n",
|
||||
" id=\"doc_3\",\n",
|
||||
" metadata={\n",
|
||||
" \"author\": \"Frank Herbert\",\n",
|
||||
" \"year\": 1965,\n",
|
||||
" \"category\": \"sci-fi\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" },\n",
|
||||
" ),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"added_ids = await vector_store.aadd_documents(docs_to_add)\n",
|
||||
"print(f\"Added documents with IDs: {added_ids}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1671c31a24314836a5b85d7ef7fbf015",
|
||||
"metadata": {
|
||||
"id": "1671c31a24314836a5b85d7ef7fbf015"
|
||||
},
|
||||
"source": [
|
||||
"### Update Documents\n",
|
||||
"`BigtableVectorStore` handles updates by overwriting. To update a document, simply add it again with the same ID but with new content or metadata."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "33b0902fd34d4ace834912fa1002cf8e",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "33b0902fd34d4ace834912fa1002cf8e",
|
||||
"outputId": "d80f2b01-44df-45d7-9ff5-f77527f04733"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"doc_to_update = [\n",
|
||||
" Document(\n",
|
||||
" page_content=\"An old hobbit, Frodo Baggins, must take a powerful ring to be destroyed.\", # Updated content\n",
|
||||
" id=\"doc_2\", # Same ID\n",
|
||||
" metadata={\n",
|
||||
" \"author\": \"J.R.R. Tolkien\",\n",
|
||||
" \"year\": 1954,\n",
|
||||
" \"category\": \"epic-fantasy\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" }, # Updated metadata\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"await vector_store.aadd_documents(doc_to_update)\n",
|
||||
"print(\"Document 'doc_2' has been updated.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f6fa52606d8c4a75a9b52967216f8f3f",
|
||||
"metadata": {
|
||||
"id": "f6fa52606d8c4a75a9b52967216f8f3f"
|
||||
},
|
||||
"source": [
|
||||
"### Delete Documents"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f5a1fa73e5044315a093ec459c9be902",
|
||||
"metadata": {
|
||||
"id": "f5a1fa73e5044315a093ec459c9be902"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"is_deleted = await vector_store.adelete(ids=[\"doc_2\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cdf66aed5cc84ca1b48e60bad68798a8",
|
||||
"metadata": {
|
||||
"id": "cdf66aed5cc84ca1b48e60bad68798a8"
|
||||
},
|
||||
"source": [
|
||||
"## Query vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "28d3efd5258a48a79c179ea5c6759f01",
|
||||
"metadata": {
|
||||
"id": "28d3efd5258a48a79c179ea5c6759f01"
|
||||
},
|
||||
"source": [
|
||||
"### Search"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3f9bc0b9dd2c44919cc8dcca39b469f8",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "3f9bc0b9dd2c44919cc8dcca39b469f8",
|
||||
"outputId": "dbd5426c-139a-451b-d456-c241cf794aec"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"results = await vector_store.asimilarity_search(\"a story about a powerful ring\", k=1)\n",
|
||||
"print(results[0].page_content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0e382214b5f147d187d36a2058b9c724",
|
||||
"metadata": {
|
||||
"id": "0e382214b5f147d187d36a2058b9c724"
|
||||
},
|
||||
"source": [
|
||||
"### Search with Filters\n",
|
||||
"\n",
|
||||
"Apply filters before the vector search runs."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e7f8g9h0-query-header-restored",
|
||||
"metadata": {
|
||||
"id": "e7f8g9h0-query-header-restored"
|
||||
},
|
||||
"source": [
|
||||
"#### The kNN Search Algorithm and Filtering\n",
|
||||
"\n",
|
||||
"By default, `BigtableVectorStore` uses a **k-Nearest Neighbors (kNN)** search algorithm to find the `k` vectors in the database that are most similar to your query vector. The vector store offers filtering to reduce the search space *before* the kNN search is performed, which can make queries faster and more relevant.\n",
|
||||
"\n",
|
||||
"#### Configuring Queries with `QueryParameters`\n",
|
||||
"\n",
|
||||
"All search settings are controlled via the `QueryParameters` object. This object allows you to specify not only filters but also other important search aspects:\n",
|
||||
"* `algorithm`: The search algorithm to use. Defaults to `\"kNN\"`.\n",
|
||||
"* `distance_strategy`: The metric used for comparison, such as `COSINE` (default) or `EUCLIDEAN`.\n",
|
||||
"* `vector_data_type`: The data type of the stored vectors, like `FLOAT32` or `DOUBLE64`. This should match the precision of your embeddings.\n",
|
||||
"* `filters`: A dictionary defining the filtering logic to apply.\n",
|
||||
"\n",
|
||||
"#### Understanding Encodings\n",
|
||||
"\n",
|
||||
"To filter on metadata fields, you must define them in `metadata_mappings` with the correct `encoding` so Bigtable can properly interpret the data. Supported encodings include:\n",
|
||||
"* **String**: `UTF8`, `UTF16`, `ASCII` for text-based metadata.\n",
|
||||
"* **Numeric**: `INT_BIG_ENDIAN` or `INT_LITTLE_ENDIAN` for integers, and `FLOAT` or `DOUBLE` for decimal numbers.\n",
|
||||
"* **Boolean**: `BOOL` for true/false values."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5b09d5ef5b5e4bb6ab9b829b10b6a29f",
|
||||
"metadata": {
|
||||
"id": "5b09d5ef5b5e4bb6ab9b829b10b6a29f"
|
||||
},
|
||||
"source": [
|
||||
"#### Filtering Support Table\n",
|
||||
"\n",
|
||||
"| Filter Category | Key / Operator | Meaning |\n",
|
||||
"|---|---|---|\n",
|
||||
"| **Row Key** | `RowKeyFilter` | Narrows search to document IDs with a specific prefix. |\n",
|
||||
"| **Metadata Key** | `ColumnQualifiers` | Checks for the presence of one or more exact metadata keys. |\n",
|
||||
"| | `ColumnQualifierPrefix` | Checks if a metadata key starts with a given prefix. |\n",
|
||||
"| | `ColumnQualifierRegex` | Checks if a metadata key matches a regular expression. |\n",
|
||||
"| **Metadata Value** | `ColumnValueFilter` | Container for all value-based conditions. |\n",
|
||||
"| | `==` | Equality |\n",
|
||||
"| | `!=` | Inequality |\n",
|
||||
"| | `>` | Greater than |\n",
|
||||
"| | `<` | Less than |\n",
|
||||
"| | `>=` | Greater than or equal |\n",
|
||||
"| | `<=` | Less than or equal |\n",
|
||||
"| | `in` | Value is in a list. |\n",
|
||||
"| | `nin` | Value is not in a list. |\n",
|
||||
"| | `contains` | Checks for substring presence. |\n",
|
||||
"| | `like` | Performs a regex match on a string. |\n",
|
||||
"| **Logical**| `ColumnValueChainFilter` | Logical AND for combining value conditions. |\n",
|
||||
"| | `ColumnValueUnionFilter` | Logical OR for combining value conditions. |"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a50416e276a0479cbe66534ed1713a40",
|
||||
"metadata": {
|
||||
"id": "a50416e276a0479cbe66534ed1713a40"
|
||||
},
|
||||
"source": [
|
||||
"#### Complex Filter Example\n",
|
||||
"\n",
|
||||
"This example uses multiple nested logical filters. It searches for documents that are either (`category` is 'sci-fi' AND `year` between 1970-2000) OR (`author` is 'J.R.R. Tolkien') OR (`rating` > 4.5)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "46a27a456b804aa2a380d5edf15a5daf",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "46a27a456b804aa2a380d5edf15a5daf",
|
||||
"outputId": "7679570a-80f6-4342-8380-daecb62d7cf8"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_google_bigtable.vector_store import QueryParameters\n",
|
||||
"\n",
|
||||
"complex_filter = {\n",
|
||||
" \"ColumnValueFilter\": {\n",
|
||||
" \"ColumnValueUnionFilter\": { # OR\n",
|
||||
" \"ColumnValueChainFilter\": { # First AND condition\n",
|
||||
" \"category\": {\"==\": \"sci-fi\"},\n",
|
||||
" \"year\": {\">\": 1970, \"<\": 2000},\n",
|
||||
" },\n",
|
||||
" \"author\": {\"==\": \"J.R.R. Tolkien\"},\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"query_params_complex = QueryParameters(filters=complex_filter)\n",
|
||||
"\n",
|
||||
"complex_results = await vector_store.asimilarity_search(\n",
|
||||
" \"a story about a hero's journey\", k=5, query_parameters=query_params_complex\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(f\"Found {len(complex_results)} documents matching the complex filter:\")\n",
|
||||
"for doc in complex_results:\n",
|
||||
" print(f\"- ID: {doc.id}, Metadata: {doc.metadata}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1944c39560714e6e80c856f20744a8e5",
|
||||
"metadata": {
|
||||
"id": "1944c39560714e6e80c856f20744a8e5"
|
||||
},
|
||||
"source": [
|
||||
"### Search with score\n",
|
||||
"You can also retrieve the distance score along with the documents."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d6ca27006b894b04b6fc8b79396e2797",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "d6ca27006b894b04b6fc8b79396e2797",
|
||||
"outputId": "32360bd3-7ccb-4ed6-b68a-52788c902049"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"results_with_scores = await vector_store.asimilarity_search_with_score(\n",
|
||||
" query=\"an evil entity\", k=1\n",
|
||||
")\n",
|
||||
"for doc, score in results_with_scores:\n",
|
||||
" print(f\"* [SCORE={score:.4f}] {doc.page_content} [{doc.metadata}]\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f61877af4e7f4313ad8234302950b331",
|
||||
"metadata": {
|
||||
"id": "f61877af4e7f4313ad8234302950b331"
|
||||
},
|
||||
"source": [
|
||||
"### Use as Retriever\n",
|
||||
"The vector store can be easily used as a retriever in RAG applications. You can specify the search type (e.g., `similarity` or `mmr`) and pass search-time arguments like `k` and `query_parameters`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "84d5ab97d17b4c38ab41a2b065bbd0c0",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "84d5ab97d17b4c38ab41a2b065bbd0c0",
|
||||
"outputId": "b33dc07f-08d4-4108-c50d-96dd4e8d719b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define a filter to use with the retriever\n",
|
||||
"retriever_filter = {\"ColumnValueFilter\": {\"category\": {\"==\": \"horror\"}}}\n",
|
||||
"retriever_query_params = QueryParameters(filters=retriever_filter)\n",
|
||||
"\n",
|
||||
"retriever = vector_store.as_retriever(\n",
|
||||
" search_type=\"mmr\", # Specify MMR for retrieval\n",
|
||||
" search_kwargs={\n",
|
||||
" \"k\": 1,\n",
|
||||
" \"lambda_mult\": 0.8,\n",
|
||||
" \"query_parameters\": retriever_query_params, # Pass filter parameters\n",
|
||||
" },\n",
|
||||
")\n",
|
||||
"retrieved_docs = await retriever.ainvoke(\"a story about a hobbit\")\n",
|
||||
"print(retrieved_docs[0].page_content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "35ffc1ce1c7b4df9ace1bc936b8b1dc2",
|
||||
"metadata": {
|
||||
"id": "35ffc1ce1c7b4df9ace1bc936b8b1dc2"
|
||||
},
|
||||
"source": [
|
||||
"## Usage for retrieval-augmented generation\n",
|
||||
"\n",
|
||||
"For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n",
|
||||
"\n",
|
||||
"- [Tutorials](https://python.langchain.com/docs/tutorials/rag/)\n",
|
||||
"- [How-to: Question and answer with RAG](https://python.langchain.com/docs/how_to/#qa-with-rag)\n",
|
||||
"- [Retrieval conceptual docs](https://python.langchain.com/docs/concepts/retrieval/)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "76127f4a2f6a44fba749ea7800e59d51",
|
||||
"metadata": {
|
||||
"id": "76127f4a2f6a44fba749ea7800e59d51"
|
||||
},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
"\n",
|
||||
"For full details on the `BigtableVectorStore` class, see the source code on [GitHub](https://github.com/googleapis/langchain-google-bigtable-python/blob/main/src/langchain_google_bigtable/vector_store.py)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": [],
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -43,9 +43,9 @@
|
||||
"source": [
|
||||
"### Prerequisites for using Langchain with Oracle AI Vector Search\n",
|
||||
"\n",
|
||||
"You'll need to install `langchain-community` with `pip install -qU langchain-community` to use this integration\n",
|
||||
"You'll need to install `langchain-oracledb` with `python -m pip install -U langchain-oracledb` to use this integration.\n",
|
||||
"\n",
|
||||
"Please install Oracle Python Client driver to use Langchain with Oracle AI Vector Search. "
|
||||
"The `python-oracledb` driver is installed automatically as a dependency of langchain-oracledb."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -55,7 +55,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# pip install oracledb"
|
||||
"# python -m pip install -U langchain-oracledb"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -103,8 +103,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.vectorstores import oraclevs\n",
|
||||
"from langchain_community.vectorstores.oraclevs import OracleVS\n",
|
||||
"from langchain_oracledb.vectorstores import oraclevs\n",
|
||||
"from langchain_oracledb.vectorstores.oraclevs import OracleVS\n",
|
||||
"from langchain_community.vectorstores.utils import DistanceStrategy\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"from langchain_huggingface import HuggingFaceEmbeddings"
|
||||
@@ -400,7 +400,111 @@
|
||||
"id": "7223d048-5c0b-4e91-a91b-a7daa9f86758",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Demonstrate advanced searches on all six vector stores, with and without attribute filtering – with filtering, we only select the document id 101 and nothing else"
|
||||
"### Demonstrate advanced searches on all six vector stores, with and without attribute filtering – with filtering, we only select the document id 101 and nothing else.\n",
|
||||
"\n",
|
||||
"Oracle Database 23ai supports pre-filtering, in-filtering, and post-filtering to enhance AI Vector Search capabilities. These filtering mechanisms allow users to apply constraints before, during, and after performing vector similarity searches, improving search performance and accuracy.\n",
|
||||
"\n",
|
||||
"Key Points about Filtering in Oracle 23ai:\n",
|
||||
"1. Pre-filtering\n",
|
||||
" Applies traditional SQL filters to reduce the dataset before performing the vector similarity search.\n",
|
||||
" Helps improve efficiency by limiting the amount of data processed by AI algorithms.\n",
|
||||
"2. In-filtering\n",
|
||||
" Utilizes AI Vector Search to perform similarity searches directly on vector embeddings, using optimized indexes and algorithms.\n",
|
||||
" Efficiently filters results based on vector similarity without requiring full dataset scans.\n",
|
||||
"3. Post-filtering\n",
|
||||
" Applies additional SQL filtering to refine the results after the vector similarity search.\n",
|
||||
" Allows further refinement based on business logic or additional metadata conditions.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"**Why is this Important?**\n",
|
||||
"- Performance Optimization: Pre-filtering significantly reduces query execution time, making searches on massive datasets more efficient.\n",
|
||||
"- Accuracy Enhancement: In-filtering ensures that vector searches are semantically meaningful, improving the quality of search results.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "71406bf9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Filter Details\n",
|
||||
"\n",
|
||||
"`OracleVS` supports a set of filters that can be applied to `metadata` fields using `filter` parameter. These filters allow you to select and refine data based on various criteria. \n",
|
||||
"\n",
|
||||
"**Available Filter Operators:**\n",
|
||||
"\n",
|
||||
"| Operator | Description |\n",
|
||||
"|--------------------------|--------------------------------------------------------------------------------------------------|\n",
|
||||
"| \\$exists | Field exists. |\n",
|
||||
"| \\$eq | Field value equals the operand value (`=`). |\n",
|
||||
"| \\$ne | Field exists and value does not equal the operand value (`!=`). |\n",
|
||||
"| \\$gt | Field value is greater than the operand value (`>`). |\n",
|
||||
"| \\$lt | Field value is less than the operand value (`<`). |\n",
|
||||
"| \\$gte | Field value is greater than or equal to the operand value (`>=`). |\n",
|
||||
"| \\$lte | Field value is less than or equal to the operand value (`<=`). |\n",
|
||||
"| \\$between | Field value is between (or equal to) two values in the operand array. |\n",
|
||||
"| \\$startsWith | Field value starts with the operand value. |\n",
|
||||
"| \\$hasSubstring | Field value contains the operand as a substring. |\n",
|
||||
"| \\$instr | Field value contains the operand as a substring. |\n",
|
||||
"| \\$regex | Field value matches the given regular expression pattern. |\n",
|
||||
"| \\$like | Field value matches the operand pattern (using SQL-like syntax). |\n",
|
||||
"| \\$in | Field value equals at least one value in the operand array. |\n",
|
||||
"| \\$nin | Field exists, but its value is not equal to any in the operand array, or the field does not exist.|\n",
|
||||
"| \\$all | Field value is an array containing all items from the operand array, or a scalar matching a single operand. |\n",
|
||||
"\n",
|
||||
"- You can combine these filters using logical operators:\n",
|
||||
"\n",
|
||||
"| Logical Operator | Description |\n",
|
||||
"|------------------|----------------------|\n",
|
||||
"| \\$and | Logical AND |\n",
|
||||
"| \\$or | Logical OR |\n",
|
||||
"| \\$nor | Logical NOR |\n",
|
||||
"\n",
|
||||
"**Example Filter:**\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"age\": 65,\n",
|
||||
" \"name\": {\"$regex\": \"*rk\"},\n",
|
||||
" \"$or\": [\n",
|
||||
" {\n",
|
||||
" \"$and\": [\n",
|
||||
" {\"name\": \"Jason\"},\n",
|
||||
" {\"drinks\": {\"$in\": [\"tea\", \"soda\"]}}\n",
|
||||
" ]\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"$nor\": [\n",
|
||||
" {\"age\": {\"$lt\": 65}},\n",
|
||||
" {\"name\": \"Jason\"}\n",
|
||||
" ]\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"**Additional Usage Tips:**\n",
|
||||
"- You can omit `$and` when all filters in an object must be satisfied. These two are equivalent:\n",
|
||||
"```json\n",
|
||||
"{ \"$and\": [\n",
|
||||
" { \"name\": { \"$startsWith\": \"Fred\" } },\n",
|
||||
" { \"salary\": { \"$gt\": 10000, \"$lte\": 20000 } }\n",
|
||||
"]}\n",
|
||||
"```\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"name\": { \"$startsWith\": \"Fred\" },\n",
|
||||
" \"salary\": { \"$gt\": 10000, \"$lte\": 20000 }\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"- The `$not` clause can negate a comparison operator:\n",
|
||||
"```json\n",
|
||||
"{ \"address.zip\": { \"$not\": { \"$eq\": \"90001\" } } }\n",
|
||||
"```\n",
|
||||
"- Using `field: scalar` is equivalent to `field: { \"$eq\": scalar }`:\n",
|
||||
"```json\n",
|
||||
"{ \"animal\": \"cat\" }\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"For more filter examples, refer to the [test specification](https://github.com/oracle/langchain-oracle/blob/main/libs/oracledb/tests/integration_tests/vectorstores/test_oraclevs.py)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -415,7 +519,23 @@
|
||||
" query = \"How are LOBS stored in Oracle Database\"\n",
|
||||
" # Constructing a filter for direct comparison against document metadata\n",
|
||||
" # This filter aims to include documents whose metadata 'id' is exactly '2'\n",
|
||||
" filter_criteria = {\"id\": [\"101\"]} # Direct comparison filter\n",
|
||||
" db_filter = {\n",
|
||||
" \"$and\": [\n",
|
||||
" {\"id\": \"101\"}, # FilterCondition\n",
|
||||
" {\n",
|
||||
" \"$or\": [ # FilterGroup\n",
|
||||
" {\"status\": \"approved\"},\n",
|
||||
" {\"link\": \"Document Example Test 2\"},\n",
|
||||
" {\n",
|
||||
" \"$and\": [ # Nested FilterGroup\n",
|
||||
" {\"status\": \"approved\"},\n",
|
||||
" {\"link\": \"Document Example Test 2\"},\n",
|
||||
" ]\n",
|
||||
" },\n",
|
||||
" ]\n",
|
||||
" },\n",
|
||||
" ]\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" for i, vs in enumerate(vector_stores, start=1):\n",
|
||||
" print(f\"\\n--- Vector Store {i} Advanced Searches ---\")\n",
|
||||
@@ -425,7 +545,7 @@
|
||||
"\n",
|
||||
" # Similarity search with a filter\n",
|
||||
" print(\"\\nSimilarity search results with filter:\")\n",
|
||||
" print(vs.similarity_search(query, 2, filter=filter_criteria))\n",
|
||||
" print(vs.similarity_search(query, 2, filter=db_filter))\n",
|
||||
"\n",
|
||||
" # Similarity search with relevance score\n",
|
||||
" print(\"\\nSimilarity search with relevance score:\")\n",
|
||||
@@ -433,7 +553,7 @@
|
||||
"\n",
|
||||
" # Similarity search with relevance score with filter\n",
|
||||
" print(\"\\nSimilarity search with relevance score with filter:\")\n",
|
||||
" print(vs.similarity_search_with_score(query, 2, filter=filter_criteria))\n",
|
||||
" print(vs.similarity_search_with_score(query, 2, filter=db_filter))\n",
|
||||
"\n",
|
||||
" # Max marginal relevance search\n",
|
||||
" print(\"\\nMax marginal relevance search results:\")\n",
|
||||
@@ -443,7 +563,7 @@
|
||||
" print(\"\\nMax marginal relevance search results with filter:\")\n",
|
||||
" print(\n",
|
||||
" vs.max_marginal_relevance_search(\n",
|
||||
" query, 2, fetch_k=20, lambda_mult=0.5, filter=filter_criteria\n",
|
||||
" query, 2, fetch_k=20, lambda_mult=0.5, filter=db_filter\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
@@ -477,7 +597,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.9"
|
||||
"version": "3.13.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
664
docs/docs/integrations/vectorstores/yugabytedb.ipynb
Normal file
664
docs/docs/integrations/vectorstores/yugabytedb.ipynb
Normal file
@@ -0,0 +1,664 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "1957f5cb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: YugabyteDB\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ef1f0986",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# YugabyteDBVectorStore\n",
|
||||
"\n",
|
||||
"This notebook covers how to get started with the YugabyteDB vector store in langchain, using the `langchain-yugabytedb` package.\n",
|
||||
"\n",
|
||||
"YugabyteDB is a cloud-native distributed PostgreSQL-compatible database that combines strong consistency with ultra-resilience, seamless scalability, geo-distribution, and highly flexible data locality to deliver business-critical, transactional applications.\n",
|
||||
"\n",
|
||||
"[YugabyteDB](https://www.yugabyte.com/ai/) combines the power of the `pgvector` PostgreSQL extension with an inherently distributed architecture. This future-proofed foundation helps you build GenAI applications using RAG retrieval that demands high-performance vector search.\n",
|
||||
"\n",
|
||||
"YugabyteDB’s unique approach to vector indexing addresses the limitations of single-node PostgreSQL systems when dealing with large-scale vector datasets.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"### Minimum Version\n",
|
||||
"`langchain-yugabytedb` module requires YugabyteDB `v2025.1.0.0` or higher.\n",
|
||||
"\n",
|
||||
"### Connecting to YugabyteDB database\n",
|
||||
"\n",
|
||||
"In order to get started with `YugabyteDBVectorStore`, lets start a local YugabyteDB node for development purposes - \n",
|
||||
"\n",
|
||||
"### Start YugabyteDB RF-1 Universe."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5a8147d9",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "plaintext"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"docker run -d --name yugabyte_node01 --hostname yugabyte01 \\\n",
|
||||
" -p 7000:7000 -p 9000:9000 -p 15433:15433 -p 5433:5433 -p 9042:9042 \\\n",
|
||||
" yugabytedb/yugabyte:2.25.2.0-b359 bin/yugabyted start --background=false \\\n",
|
||||
" --master_flags=\"allowed_preview_flags_csv=ysql_yb_enable_advisory_locks,ysql_yb_enable_advisory_locks=true\" \\\n",
|
||||
" --tserver_flags=\"allowed_preview_flags_csv=ysql_yb_enable_advisory_locks,ysql_yb_enable_advisory_locks=true\"\n",
|
||||
"\n",
|
||||
"docker exec -it yugabyte_node01 bin/ysqlsh -h yugabyte01 -c \"CREATE extension vector;\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "541e4507",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"For production deployment, performance benchmarking, or deploying a true multi-node on multi-host setup, see Deploy [YugabyteDB](https://docs.yugabyte.com/stable/deploy/)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "36fdc060",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Installation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "432f461c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --upgrade --quiet langchain\n",
|
||||
"%pip install --upgrade --quiet langchain-openai langchain-community tiktoken\n",
|
||||
"%pip install --upgrade --quiet psycopg-binary"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "64e28aa6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install -qU \"langchain-yugabytedb\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3f9951f4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Set your YugabyteDB Values\n",
|
||||
"\n",
|
||||
"YugabyteDB clients connect to the cluster using a PostgreSQL compliant connection string. YugabyteDB connection parameters are provided below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b4715d61",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"YUGABYTEDB_USER = \"yugabyte\" # @param {type: \"string\"}\n",
|
||||
"YUGABYTEDB_PASSWORD = \"\" # @param {type: \"string\"}\n",
|
||||
"YUGABYTEDB_HOST = \"localhost\" # @param {type: \"string\"}\n",
|
||||
"YUGABYTEDB_PORT = \"5433\" # @param {type: \"string\"}\n",
|
||||
"YUGABYTEDB_DB = \"yugabyte\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "93df377e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Initialization\n",
|
||||
"\n",
|
||||
"### Environment Setup\n",
|
||||
"\n",
|
||||
"This notebook uses the OpenAI API through `OpenAIEmbeddings`. We suggest obtaining an OpenAI API key and export it as an environment variable with the name `OPENAI_API_KEY`.\n",
|
||||
"\n",
|
||||
"### Connecting to YugabyteDB Universe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dc37144c-208d-4ab3-9f3a-0407a69fe052",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_yugabytedb import YBEngine, YugabyteDBVectorStore\n",
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"\n",
|
||||
"TABLE_NAME = \"my_doc_collection\"\n",
|
||||
"VECTOR_SIZE = 1536\n",
|
||||
"\n",
|
||||
"CONNECTION_STRING = (\n",
|
||||
" f\"postgresql+asyncpg://{YUGABYTEDB_USER}:{YUGABYTEDB_PASSWORD}@{YUGABYTEDB_HOST}\"\n",
|
||||
" f\":{YUGABYTEDB_PORT}/{YUGABYTEDB_DB}\"\n",
|
||||
")\n",
|
||||
"engine = YBEngine.from_connection_string(url=CONNECTION_STRING)\n",
|
||||
"\n",
|
||||
"embeddings = OpenAIEmbeddings()\n",
|
||||
"engine.init_vectorstore_table(\n",
|
||||
" table_name=TABLE_NAME,\n",
|
||||
" vector_size=VECTOR_SIZE,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"yugabyteDBVectorStore = YugabyteDBVectorStore.create_sync(\n",
|
||||
" engine=engine,\n",
|
||||
" table_name=TABLE_NAME,\n",
|
||||
" embedding_service=embeddings,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ac6071d4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Manage vector store\n",
|
||||
"\n",
|
||||
"### Add items to vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "17f5efc0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"docs = [\n",
|
||||
" Document(page_content=\"Apples and oranges\"),\n",
|
||||
" Document(page_content=\"Cars and airplanes\"),\n",
|
||||
" Document(page_content=\"Train\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"yugabyteDBVectorStore.add_documents(docs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7b92b5f6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"['b40e7f47-3a4e-4b88-b6e2-cb3465dde6bd', '275823d2-1a47-440d-904b-c07b132fd72b', 'f0c5a9bc-1456-40fe-906b-4e808d601470']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dcf1b905",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Delete items from vector store\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ef61e188",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"yugabyteDBVectorStore.delete(ids=[\"275823d2-1a47-440d-904b-c07b132fd72b\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f8f751e1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Update items from vector store\n",
|
||||
"\n",
|
||||
"Note: Update operation is not supported by YugabyteDBVectorStore."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c3620501",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Query vector store\n",
|
||||
"\n",
|
||||
"Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent. \n",
|
||||
"\n",
|
||||
"### Query directly\n",
|
||||
"\n",
|
||||
"Performing a simple similarity search can be done as follows:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aa0a16fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"query = \"I'd like a fruit.\"\n",
|
||||
"docs = yugabyteDBVectorStore.similarity_search(query)\n",
|
||||
"print(docs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3ed9d733",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you want to execute a similarity search and receive the corresponding scores you can run:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5efd2eaa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"query = \"I'd like a fruit.\"\n",
|
||||
"docs = yugabyteDBVectorStore.similarity_search(query, k=1)\n",
|
||||
"print(docs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0c235cdc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Query by turning into retriever\n",
|
||||
"\n",
|
||||
"You can also transform the vector store into a retriever for easier usage in your chains. \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3460093",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"retriever = yugabyteDBVectorStore.as_retriever(search_kwargs={\"k\": 1})\n",
|
||||
"retriever.invoke(\"I'd like a fruit.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5e657ae6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## ChatMessageHistory\n",
|
||||
"\n",
|
||||
"The chat message history abstraction helps to persist chat message history in a YugabyteDB table.\n",
|
||||
"\n",
|
||||
"`YugabyteDBChatMessageHistory` is parameterized using a table_name and a session_id.\n",
|
||||
"\n",
|
||||
"The table_name is the name of the table in the database where the chat messages will be stored.\n",
|
||||
"\n",
|
||||
"The session_id is a unique identifier for the chat session. It can be assigned by the caller using uuid.uuid4()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0677c927",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import uuid\n",
|
||||
"\n",
|
||||
"from langchain_core.messages import SystemMessage, AIMessage, HumanMessage\n",
|
||||
"from langchain_yugabytedb import YugabyteDBChatMessageHistory\n",
|
||||
"import psycopg\n",
|
||||
"\n",
|
||||
"# Establish a synchronous connection to the database\n",
|
||||
"# (or use psycopg.AsyncConnection for async)\n",
|
||||
"conn_info = \"dbname=yugabyte user=yugabyte host=localhost port=5433\"\n",
|
||||
"sync_connection = psycopg.connect(conn_info)\n",
|
||||
"\n",
|
||||
"# Create the table schema (only needs to be done once)\n",
|
||||
"table_name = \"chat_history\"\n",
|
||||
"YugabyteDBChatMessageHistory.create_tables(sync_connection, table_name)\n",
|
||||
"\n",
|
||||
"session_id = str(uuid.uuid4())\n",
|
||||
"\n",
|
||||
"# Initialize the chat history manager\n",
|
||||
"chat_history = YugabyteDBChatMessageHistory(\n",
|
||||
" table_name, session_id, sync_connection=sync_connection\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Add messages to the chat history\n",
|
||||
"chat_history.add_messages(\n",
|
||||
" [\n",
|
||||
" SystemMessage(content=\"Meow\"),\n",
|
||||
" AIMessage(content=\"woof\"),\n",
|
||||
" HumanMessage(content=\"bark\"),\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(chat_history.messages)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "901c75dc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Usage for retrieval-augmented generation\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"One of the primary advantages of the vector stores is to provide contextual data to the LLMs. LLMs often are trained with stale data and might not have the relevant domain specific knowledge which results in halucinations in LLMs responses. Take the following example - \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d0f51eb3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage, AIMessage\n",
|
||||
"\n",
|
||||
"my_api_key = getpass.getpass(\"Enter your API Key: \")\n",
|
||||
"\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0.7, api_key=my_api_key)\n",
|
||||
"# Start with a system message to set the persona/behavior of the AI\n",
|
||||
"messages = [\n",
|
||||
" SystemMessage(\n",
|
||||
" content=\"You are a helpful and friendly assistant named 'YugaAI'. You love to answer questions about YugabyteDB and distributed sql.\"\n",
|
||||
" ),\n",
|
||||
" # First human turn\n",
|
||||
" HumanMessage(content=\"Hi YugaAI! Where's the headquarters of YugabyteDB?\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"print(\"--- First Interaction ---\")\n",
|
||||
"print(f\"Human: {messages[1].content}\") # Print the human message\n",
|
||||
"response1 = llm.invoke(messages)\n",
|
||||
"print(f\"YugaAI: {response1.content}\")\n",
|
||||
"\n",
|
||||
"print(\"\\n--- Second Interaction ---\")\n",
|
||||
"print(f\"Human: {messages[2].content}\") # Print the new human message\n",
|
||||
"response2 = llm.invoke(messages) # Send the *entire* message history\n",
|
||||
"print(f\"YugaAI: {response2.content}\")\n",
|
||||
"\n",
|
||||
"# Add the second AI response to the history\n",
|
||||
"messages.append(AIMessage(content=response2.content))\n",
|
||||
"\n",
|
||||
"# --- 5. Another Turn with a different topic ---\n",
|
||||
"messages.append(\n",
|
||||
" HumanMessage(\n",
|
||||
" content=\"Can you tell me the current preview release version of YugabyteDB?\"\n",
|
||||
" )\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"\\n--- Third Interaction ---\")\n",
|
||||
"print(f\"Human: {messages[4].content}\") # Print the new human message\n",
|
||||
"response3 = llm.invoke(messages) # Send the *entire* message history\n",
|
||||
"print(f\"YugaAI: {response3.content}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8500e27b",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "Log"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"--- First Interaction ---\n",
|
||||
"Human: Hi YugaAI! Where's the headquarters of YugabyteDB?\n",
|
||||
"YugaAI: Hello! YugabyteDB's headquarters is located in Sunnyvale, California, USA.\n",
|
||||
"\n",
|
||||
"--- Second Interaction ---\n",
|
||||
"Human: And what are YugabyteDB's supported APIs?\n",
|
||||
"YugaAI: YugabyteDB's headquarters is located in Sunnyvale, California, USA.\n",
|
||||
"\n",
|
||||
"YugabyteDB supports several APIs, including:\n",
|
||||
"1. YSQL (PostgreSQL-compatible SQL)\n",
|
||||
"2. YCQL (Cassandra-compatible query language)\n",
|
||||
"3. YEDIS (Redis-compatible key-value store)\n",
|
||||
"\n",
|
||||
"These APIs allow developers to interact with YugabyteDB using familiar interfaces and tools.\n",
|
||||
"\n",
|
||||
"--- Third Interaction ---\n",
|
||||
"Human: Can you tell me the current preview release version of YugabyteDB?\n",
|
||||
"YugaAI: The current preview release version of YugabyteDB is 2.11.0. This version includes new features, improvements, and bug fixes that are being tested by the community before the official stable release."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7fe4301c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The current preview release of YugabyteDB is `v2.25.2.0`, however LLMs is providing stale information which is 2-3 years old. This is where the vector stores complement the LLMs by providing a way to store and retrive relevant information."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d4e19220",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Construct a RAG for providing contextual information\n",
|
||||
"\n",
|
||||
"We will provide the relevant information to the LLMs by reading the YugabyteDB documentation. Let's first read the YugabyteDB docs and add data into YugabyteDB Vectorstore by loading, splitting and chuncking data from a html source. We will then store the vector embeddings generated by OpenAI embeddings into YugabyteDB Vectorstore.\n",
|
||||
"\n",
|
||||
"#### Generate Embeddings "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "05ec4ebc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"from langchain_community.document_loaders import WebBaseLoader\n",
|
||||
"from langchain_text_splitters import CharacterTextSplitter\n",
|
||||
"from langchain_yugabytedb import YBEngine, YugabyteDBVectorStore\n",
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"\n",
|
||||
"my_api_key = getpass.getpass(\"Enter your API Key: \")\n",
|
||||
"url = \"https://docs.yugabyte.com/preview/releases/ybdb-releases/v2.25/\"\n",
|
||||
"\n",
|
||||
"loader = WebBaseLoader(url)\n",
|
||||
"\n",
|
||||
"documents = loader.load()\n",
|
||||
"\n",
|
||||
"print(f\"Number of documents loaded: {len(documents)}\")\n",
|
||||
"\n",
|
||||
"# For very large HTML files, you'll want to split the text into smaller\n",
|
||||
"# chunks before sending them to an LLM, as LLMs have token limits.\n",
|
||||
"for i, doc in enumerate(documents):\n",
|
||||
" text_splitter = CharacterTextSplitter(\n",
|
||||
" separator=\"\\n\\n\", # Split by double newline (common paragraph separator)\n",
|
||||
" chunk_size=1000, # Each chunk will aim for 1000 characters\n",
|
||||
" chunk_overlap=200, # Allow 200 characters overlap between chunks\n",
|
||||
" length_function=len,\n",
|
||||
" is_separator_regex=False,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Apply the splitter to the loaded documents\n",
|
||||
" chunks = text_splitter.split_documents(documents)\n",
|
||||
"\n",
|
||||
" print(f\"\\n--- After Splitting ({len(chunks)} chunks) ---\")\n",
|
||||
"\n",
|
||||
" CONNECTION_STRING = \"postgresql+psycopg://yugabyte:@localhost:5433/yugabyte\"\n",
|
||||
" TABLE_NAME = \"yb_relnotes_chunks\"\n",
|
||||
" VECTOR_SIZE = 1536\n",
|
||||
" engine = YBEngine.from_connection_string(url=CONNECTION_STRING)\n",
|
||||
" engine.init_vectorstore_table(\n",
|
||||
" table_name=TABLE_NAME,\n",
|
||||
" vector_size=VECTOR_SIZE,\n",
|
||||
" )\n",
|
||||
" embeddings = OpenAIEmbeddings(api_key=my_api_key)\n",
|
||||
"\n",
|
||||
" # The PGVector.from_documents method handles:\n",
|
||||
" # 1. Creating the table if it doesn't exist (with 'embedding' column).\n",
|
||||
" # 2. Generating embeddings for each chunk using the provided embeddings model.\n",
|
||||
" # 3. Inserting the chunk text, metadata, and embeddings into the table.\n",
|
||||
" vectorstore = YugabyteDBVectorStore.from_documents(\n",
|
||||
" engine=engine, table_name=TABLE_NAME, documents=chunks, embedding=embeddings\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" print(f\"Successfully stored {len(chunks)} chunks in PostgreSQL table: {TABLE_NAME}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e6483d89",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Configure the YugabyteDB retriever"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "18a84445",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"retriever = vectorstore.as_retriever(search_kwargs={\"k\": 3})\n",
|
||||
"print(\n",
|
||||
" f\"Retriever created, set to retrieve top {retriever.search_kwargs['k']} documents.\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initialize the Chat Model (e.g., OpenAI's GPT-3.5 Turbo)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0, api_key=my_api_key)\n",
|
||||
"\n",
|
||||
"# Define the RAG prompt template\n",
|
||||
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||
" [\n",
|
||||
" (\n",
|
||||
" \"system\",\n",
|
||||
" \"You are a helpful and friendly assistant named 'YugaAI'. You love to answer questions about YugabyteDB and distributed sql.\",\n",
|
||||
" ),\n",
|
||||
" (\"human\", \"Context: {context}\\nQuestion: {question}\"),\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"# Build the RAG chain\n",
|
||||
"# 1. Take the input question.\n",
|
||||
"# 2. Pass it to the retriever to get relevant documents.\n",
|
||||
"# 3. Format the documents into a string for the context.\n",
|
||||
"# 4. Pass the context and question to the prompt template.\n",
|
||||
"# 5. Send the prompt to the LLM.\n",
|
||||
"# 6. Parse the LLM's string output.\n",
|
||||
"rag_chain = (\n",
|
||||
" {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
|
||||
" | prompt\n",
|
||||
" | llm\n",
|
||||
" | StrOutputParser()\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "04d12dc5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now, let's try asking the same question `Can you tell me the current preview release version of YugabyteDB?` again to the LLM"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "846e9963",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Invoke the RAG chain with a question\n",
|
||||
"rag_query = \"Can you tell me the current preview release version of YugabyteDB?\"\n",
|
||||
"print(f\"\\nQuerying RAG chain: '{rag_query}'\")\n",
|
||||
"rag_response = rag_chain.invoke(rag_query)\n",
|
||||
"print(\"\\n--- RAG Chain Response ---\")\n",
|
||||
"print(rag_response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c6fc3e7f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Querying RAG chain: 'Can you tell me the current preview release version of YugabyteDB?'\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "efc2e0c7",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "log"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"--- RAG Chain Response ---\n",
|
||||
"The current preview release version of YugabyteDB is v2.25.2.0."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "af388f24",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## API reference\n",
|
||||
" \n",
|
||||
"For detailed information of all YugabyteDBVectorStore features and configurations head to the langchain-yugabytedb github repo: https://github.com/yugabyte/langchain-yugabytedb\""
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
617
docs/docs/integrations/vectorstores/zeusdb.ipynb
Normal file
617
docs/docs/integrations/vectorstores/zeusdb.ipynb
Normal file
@@ -0,0 +1,617 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ef1f0986",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# ⚡ ZeusDB Vector Store\n",
|
||||
"\n",
|
||||
"ZeusDB is a high-performance, Rust-powered vector database with enterprise features like quantization, persistence and logging.\n",
|
||||
"\n",
|
||||
"This notebook covers how to get started with the ZeusDB Vector Store to efficiently use ZeusDB with LangChain."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "107c485d-13a3-4309-9fda-5a0440862d3c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "36fdc060",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d978e3fd-d130-436f-841d-d133c0fae8fb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Install the ZeusDB LangChain integration package from PyPi:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "42ca8320-b866-4f37-944e-96eda54231d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pip install -qU langchain-zeusdb"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2a0e518a-ae8a-464b-8b47-9deb9d4ab063",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Setup in Jupyter Notebooks*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1d092ea6-8553-4686-9563-b8318225a04a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"> 💡 Tip: If you’re working inside Jupyter or Google Colab, use the %pip magic command so the package is installed into the active kernel:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "64e28aa6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install -qU langchain-zeusdb"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c12fe175-a299-47d3-869f-9367b6aa572d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "31554e69-40b2-4201-9f92-57e73ac66d33",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Getting Started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b696b3dd-0fed-4ed2-a79a-5b32598508c0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This example uses OpenAIEmbeddings, which requires an OpenAI API key – [Get your OpenAI API key here](https://platform.openai.com/api-keys)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2b79766e-7725-4be0-a183-4947b56892c5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you prefer, you can also use this package with any other embedding provider (Hugging Face, Cohere, custom functions, etc.)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b5266cc7-28da-459e-a28d-128382ed5a20",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Install the LangChain OpenAI integration package from PyPi:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1ed941cd-5e06-4c61-9235-90bd0b0b0452",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pip install -qU langchain-openai\n",
|
||||
"\n",
|
||||
"# Use this command if inside Jupyter Notebooks\n",
|
||||
"#%pip install -qU langchain-openai"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0f49b2ec-d047-455d-8c05-da041112dd8a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Please choose an option below for your OpenAI key integration"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ed2d9bf6-be53-4fc1-9611-158f03fd71b7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Option 1: 🔑 Enter your API key each time* "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eff5b6a5-4c57-4531-896e-54bcb2b1dec2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Use getpass in Jupyter to securely input your key for the current session:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "08a50da9-5ed1-40dc-a390-07b031369761",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import getpass\n",
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7321917e-8586-42e4-9822-b68cfd74f233",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Option 2: 🗂️ Use a .env file*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b9297b6b-bd7e-457f-95af-5b41c7ab9b41",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Keep your key in a local .env file and load it automatically with python-dotenv"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "85a139dc-f439-4e4e-bc46-76d9478c304d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from dotenv import load_dotenv\n",
|
||||
"\n",
|
||||
"load_dotenv() # reads .env and sets OPENAI_API_KEY"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1af364e3-df59-4963-aaaa-0e83f6ec5e32",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"🎉🎉 That's it! You are good to go."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3146180e-026e-4421-a490-ffd14ceabac3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "93df377e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Initialization"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "fb55dfe8-2c98-45b6-ba90-7a3667ceee0c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Import required Packages and Classes\n",
|
||||
"from langchain_zeusdb import ZeusDBVectorStore\n",
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"from zeusdb import VectorDatabase"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dc37144c-208d-4ab3-9f3a-0407a69fe052",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Initialize embeddings\n",
|
||||
"embeddings = OpenAIEmbeddings(model=\"text-embedding-3-small\")\n",
|
||||
"\n",
|
||||
"# Create ZeusDB index\n",
|
||||
"vdb = VectorDatabase()\n",
|
||||
"index = vdb.create(index_type=\"hnsw\", dim=1536, space=\"cosine\")\n",
|
||||
"\n",
|
||||
"# Create vector store\n",
|
||||
"vector_store = ZeusDBVectorStore(zeusdb_index=index, embedding=embeddings)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f45fa43c-8b54-4a75-b7b0-92ac0ac506c6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ac6071d4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Manage vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "edf53787-ebda-4306-afc3-f7d440dcb1ff",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.1 Add items to vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "17f5efc0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"document_1 = Document(\n",
|
||||
" page_content=\"ZeusDB is a high-performance vector database\",\n",
|
||||
" metadata={\"source\": \"https://docs.zeusdb.com\"},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"document_2 = Document(\n",
|
||||
" page_content=\"Product Quantization reduces memory usage significantly\",\n",
|
||||
" metadata={\"source\": \"https://docs.zeusdb.com\"},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"document_3 = Document(\n",
|
||||
" page_content=\"ZeusDB integrates seamlessly with LangChain\",\n",
|
||||
" metadata={\"source\": \"https://docs.zeusdb.com\"},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"documents = [document_1, document_2, document_3]\n",
|
||||
"\n",
|
||||
"vector_store.add_documents(documents=documents, ids=[\"1\", \"2\", \"3\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c738c3e0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.2 Update items in vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f0aa8b71",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"updated_document = Document(\n",
|
||||
" page_content=\"ZeusDB now supports advanced Product Quantization with 4x-256x compression\",\n",
|
||||
" metadata={\"source\": \"https://docs.zeusdb.com\", \"updated\": True},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"vector_store.add_documents([updated_document], ids=[\"1\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dcf1b905",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.3 Delete items from vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ef61e188",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"vector_store.delete(ids=[\"3\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1a0091af-777d-4651-888a-3b346d7990f5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c3620501",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Query vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4ba3fdb2-b7d6-4f0f-b8c9-91f63596018b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 3.1 Query directly"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "400a9b25-9587-4116-ab59-6888602ec2b1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Performing a simple similarity search:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aa0a16fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"results = vector_store.similarity_search(query=\"high performance database\", k=2)\n",
|
||||
"\n",
|
||||
"for doc in results:\n",
|
||||
" print(f\"* {doc.page_content} [{doc.metadata}]\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3ed9d733",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you want to execute a similarity search and receive the corresponding scores:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5efd2eaa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"results = vector_store.similarity_search_with_score(query=\"memory optimization\", k=2)\n",
|
||||
"\n",
|
||||
"for doc, score in results:\n",
|
||||
" print(f\"* [SIM={score:.3f}] {doc.page_content} [{doc.metadata}]\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0c235cdc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 3.2 Query by turning into retriever"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "59292cb5-5dc8-4158-9137-89d0f6ca711d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can also transform the vector store into a retriever for easier usage in your chains:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3460093",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"retriever = vector_store.as_retriever(search_type=\"mmr\", search_kwargs={\"k\": 2})\n",
|
||||
"\n",
|
||||
"retriever.invoke(\"vector database features\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cc2d2b63-99d8-45c4-85e6-6a9409551ada",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "persistence_section",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## ZeusDB-Specific Features"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "memory_section",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 4.1 Memory-Efficient Setup with Product Quantization"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "12832d02-d9ea-4c35-a20f-05c85d1d7723",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"For large datasets, use Product Quantization to reduce memory usage:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "quantization_example",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create memory-optimized vector store\n",
|
||||
"quantization_config = {\"type\": \"pq\", \"subvectors\": 8, \"bits\": 8, \"training_size\": 10000}\n",
|
||||
"\n",
|
||||
"vdb_quantized = VectorDatabase()\n",
|
||||
"quantized_index = vdb_quantized.create(\n",
|
||||
" index_type=\"hnsw\", dim=1536, quantization_config=quantization_config\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"quantized_vector_store = ZeusDBVectorStore(\n",
|
||||
" zeusdb_index=quantized_index, embedding=embeddings\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(f\"Created quantized store: {quantized_index.info()}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6ffe0613-b2a7-484e-9219-1166b65c49c5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 4.2 Persistence"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fbc323ee-4c6c-43fc-beba-675d820ca078",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Save and load your vector store to disk:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "834354d1-55ad-48fe-84e1-a5eacff3f6bb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"How to Save your vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f9d1332b-a7ac-4a4b-a060-f2061599d3f1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Save the vector store\n",
|
||||
"vector_store.save_index(\"my_zeusdb_index.zdb\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "23370621-5b51-4313-800f-3a2fb9de52d2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"How to Load your vector store"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f9ed5778-58e4-4724-b69d-3c7b48cda429",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load the vector store\n",
|
||||
"loaded_store = ZeusDBVectorStore.load_index(\n",
|
||||
" path=\"my_zeusdb_index.zdb\", embedding=embeddings\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(f\"Loaded store with {loaded_store.get_vector_count()} vectors\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "610cfe63-d4a8-4ef0-88a8-cf9cc3cbbfce",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "901c75dc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Usage for retrieval-augmented generation\n",
|
||||
"\n",
|
||||
"For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n",
|
||||
"\n",
|
||||
"- [How-to: Question and answer with RAG](https://python.langchain.com/docs/how_to/#qa-with-rag)\n",
|
||||
"- [Retrieval conceptual docs](https://python.langchain.com/docs/concepts/retrieval/)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1d9d9d51-3798-410f-b1b3-f9736ea8c238",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "25b08eb0-99ab-4919-a201-5243fdfa39e9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## API reference"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "77fdca8b-f75e-4100-9f1d-7a017567dc59",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"For detailed documentation of all ZeusDBVectorStore features and configurations head to the Doc reference: https://docs.zeusdb.com/en/latest/vector_database/integrations/langchain.html"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -57,8 +57,8 @@ SEARCH_TOOL_FEAT_TABLE = {
|
||||
"available_data": "URL, Snippet, Title, Search Rank, Site Links, Authors",
|
||||
"link": "/docs/integrations/tools/searchapi",
|
||||
},
|
||||
"SerpAPI": {
|
||||
"pricing": "100 Free Searches/Month",
|
||||
"SerpApi": {
|
||||
"pricing": "250 Free Searches/Month",
|
||||
"available_data": "Answer",
|
||||
"link": "/docs/integrations/tools/serpapi",
|
||||
},
|
||||
|
||||
@@ -119,7 +119,7 @@ export default function ChatModelTabs(props) {
|
||||
value: "anthropic",
|
||||
label: "Anthropic",
|
||||
model: "claude-3-7-sonnet-20250219",
|
||||
comment: "# Note: Model versions may become outdated. Check https://docs.anthropic.com/en/docs/models-overview for latest versions",
|
||||
comment: "# Note: Model versions may become outdated. Check https://docs.anthropic.com/en/docs/about-claude/models/overview for latest versions",
|
||||
apiKeyName: "ANTHROPIC_API_KEY",
|
||||
packageName: "langchain[anthropic]",
|
||||
},
|
||||
@@ -239,6 +239,13 @@ ${llmVarName} = ChatWatsonx(
|
||||
model: "deepseek-chat",
|
||||
apiKeyName: "DEEPSEEK_API_KEY",
|
||||
packageName: "langchain-deepseek",
|
||||
},
|
||||
{
|
||||
value: "chatocigenai",
|
||||
label: "ChatOCIGenAI",
|
||||
model: "cohere.command-r-plus-08-2024",
|
||||
apiKeyName: "OCI_API_KEY",
|
||||
packageName: "langchain-oci",
|
||||
}
|
||||
].map((item) => ({
|
||||
...item,
|
||||
|
||||
@@ -34,6 +34,7 @@ export default function EmbeddingTabs(props) {
|
||||
fakeEmbeddingParams,
|
||||
hideFakeEmbedding,
|
||||
customVarName,
|
||||
hideOCIGenAIEmbeddings
|
||||
} = props;
|
||||
|
||||
const openAIParamsOrDefault = openaiParams ?? `model="text-embedding-3-large"`;
|
||||
@@ -183,6 +184,15 @@ export default function EmbeddingTabs(props) {
|
||||
default: false,
|
||||
shouldHide: hideFakeEmbedding,
|
||||
},
|
||||
{
|
||||
value: "OCIGenAIEmbeddings",
|
||||
label: "OCIGenAIEmbeddings",
|
||||
text: `from langchain_oci.embeddings import OCIGenAIEmbeddings`,
|
||||
apiKeyName: "OCI_API_KEY",
|
||||
packageName: "langchain-oci",
|
||||
default: false,
|
||||
shouldHide: hideOCIGenAIEmbeddings,
|
||||
},
|
||||
];
|
||||
|
||||
const modelOptions = tabItems
|
||||
|
||||
@@ -39,6 +39,17 @@ const FEATURE_TABLES = {
|
||||
"local": false,
|
||||
"apiLink": "https://python.langchain.com/api_reference/mistralai/chat_models/langchain_mistralai.chat_models.ChatMistralAI.html"
|
||||
},
|
||||
{
|
||||
"name": "ChatAIMLAPI",
|
||||
"package": "langchain-aimlapi",
|
||||
"link": "aimlapi/",
|
||||
"structured_output": true,
|
||||
"tool_calling": true,
|
||||
"json_mode": true,
|
||||
"multimodal": true,
|
||||
"local": false,
|
||||
"apiLink": "https://python.langchain.com/api_reference/aimlapi/chat_models/langchain_aimlapi.chat_models.ChatAIMLAPI.html"
|
||||
},
|
||||
{
|
||||
"name": "ChatFireworks",
|
||||
"package": "langchain-fireworks",
|
||||
@@ -247,6 +258,17 @@ const FEATURE_TABLES = {
|
||||
"multimodal": true,
|
||||
"local": false,
|
||||
"apiLink": "https://python.langchain.com/api_reference/perplexity/chat_models/langchain_perplexity.chat_models.ChatPerplexity.html"
|
||||
},
|
||||
{
|
||||
"name": "ChatOCIGenAI",
|
||||
"package": "langchain-oci",
|
||||
"link": "oci_generative_ai",
|
||||
"structured_output": true,
|
||||
"tool_calling": true,
|
||||
"json_mode": true,
|
||||
"multimodal": true,
|
||||
"local": false,
|
||||
"apiLink": "https://github.com/oracle/langchain-oracle"
|
||||
}
|
||||
],
|
||||
},
|
||||
@@ -301,6 +323,12 @@ const FEATURE_TABLES = {
|
||||
package: "langchain-fireworks",
|
||||
apiLink: "https://python.langchain.com/api_reference/fireworks/llms/langchain_fireworks.llms.Fireworks.html"
|
||||
},
|
||||
{
|
||||
name: "AimlapiLLM",
|
||||
link: "aimlapi",
|
||||
package: "langchain-aimlapi",
|
||||
apiLink: "https://python.langchain.com/api_reference/aimlapi/llms/langchain_aimlapi.llms.AimlapiLLM.html"
|
||||
},
|
||||
{
|
||||
name: "OllamaLLM",
|
||||
link: "ollama",
|
||||
@@ -382,6 +410,12 @@ const FEATURE_TABLES = {
|
||||
package: "langchain-fireworks",
|
||||
apiLink: "https://python.langchain.com/api_reference/fireworks/embeddings/langchain_fireworks.embeddings.FireworksEmbeddings.html"
|
||||
},
|
||||
{
|
||||
name: "AI/ML API",
|
||||
link: "/docs/integrations/text_embedding/aimlapi",
|
||||
package: "langchain-aimlapi",
|
||||
apiLink: "https://python.langchain.com/api_reference/aimlapi/embeddings/langchain_aimlapi.embeddings.AimlapiEmbeddings.html"
|
||||
},
|
||||
{
|
||||
name: "MistralAI",
|
||||
link: "/docs/integrations/text_embedding/mistralai",
|
||||
@@ -418,6 +452,13 @@ const FEATURE_TABLES = {
|
||||
package: "langchain-nvidia",
|
||||
apiLink: "https://python.langchain.com/api_reference/nvidia_ai_endpoints/embeddings/langchain_nvidia_ai_endpoints.embeddings.NVIDIAEmbeddings.html"
|
||||
},
|
||||
{
|
||||
name: "OCIGenAIEmbeddings",
|
||||
link: "oci_generative_ai",
|
||||
package: "langchain-oci",
|
||||
apiLink: "https://github.com/oracle/langchain-oracle"
|
||||
|
||||
}
|
||||
]
|
||||
},
|
||||
document_retrievers: {
|
||||
|
||||
Reference in New Issue
Block a user