姿态万千的意思是什么| bea是什么意思| 甲状腺五类是什么意思| 用什么洗脸可以美白| LOP是什么胎位| 杨枝甘露是什么东西| 孕妇为什么怕热| 宫腔镜检查主要查什么| 男人身体虚吃什么补| 滋阴降火吃什么药| 迎风流泪用什么眼药水| 万艾可是什么| 龘读什么| 大同有什么好玩的| 羊水偏少对胎儿有什么影响| 20岁长白头发是什么原因造成的| 鸦片鱼又叫什么鱼| 喝陈皮水有什么好处| 天庭饱满是什么意思| 狗奴是什么意思| 便秘吃什么好| 怀孕前壁和后壁有什么区别| 水果之王是什么水果| 内蒙有什么特产| 又当又立是什么意思| 照猫画虎什么意思| 安代表什么生肖| 四个火字念什么| 多囊不能吃什么食物| 尖酸刻薄什么意思| 手足口病的症状是什么| 处女男和什么星座最配| 指甲月牙代表什么意思| 什么人容易得胆汁淤积| sjb什么意思| 冠军是什么意思| 盆腔炎吃什么消炎药| 男人为什么会得前列腺炎| 宫颈纳囊多发是什么意思| 市辖区是什么意思| 脑梗需要注意什么| 旧人是什么意思| 牛腩烧什么好吃| 草鱼喜欢吃什么| 囊肿和肿瘤有什么区别| 亦或是什么意思| 心肾不交有什么症状| ad滴剂什么牌子好| 76年属什么生肖| 氮肥是什么肥料| 割包皮看什么科| 正常的心电图是什么样的图形| 保育费是什么意思| 黑头发有什么好处| 上午11点是什么时辰| 技校是什么学历| 怀孕为什么会流血| 十二月份的是什么星座| 龙猫是什么动物| 5月份什么星座| 红海为什么叫红海| 五朵金花是什么意思| 加拿大属于什么洲| 孕酮低是什么原因造成的| 笔触是什么意思| 身体出虚汗是什么原因| 五行缺木是什么意思| 十月6号是什么星座| 仕途是什么意思| 秘辛是什么意思| 天秤座跟什么星座最配| 特首是什么意思| 舌头中间疼是什么原因| 偶数和奇数是什么意思| 鱼缸为什么不能送人| 八月十八号是什么星座| 为什么手指关节会痛| momax是什么牌子| 左耳朵嗡嗡响是什么原因引起的| 大便量少是什么原因| 右下腹疼痛挂什么科| 夏季有什么水果| 经常晕车是什么原因| 磁共振是检查什么的| 升血压吃什么药| 狼毒是什么| 太阳穴胀痛是什么原因| 奇货可居是什么意思| 蛇为什么怕雄黄| 梦见很多狗是什么意思| 双侧腋下见淋巴结什么意思| 核桃不能和什么一起吃| 一什么一什么| 糖类抗原153偏高是什么原因| 孙子的儿子叫什么| 护士最高职称是什么| 胎儿偏小是什么原因| 挥霍是什么意思| 汉族人是什么人种| 奶奶和孙女是什么关系| 独美是什么意思| 眼睑是什么位置图片| 鸡蛋吃多了有什么危害| 小学什么时候报名| 睡觉后脑勺出汗多是什么原因| 什么是债权| 感冒引起的咳嗽吃什么药| 五谷丰登是什么生肖| 拉肚子吃什么水果好| 世界上最大的湖是什么湖| 时柱亡神是什么意思| 哀伤是什么意思| 胃阴虚吃什么药| 鸡胗是鸡的什么部位| 梦见老人死了又活了是什么意思| 02年的属什么| 胃胀气打嗝是什么原因| 梦泪什么意思| 猫眼石是什么| ad是什么病的简称| 八月十八号是什么星座| 强硬是什么意思| 老年人吃什么营养品好| 尖锐湿疣用什么药| 肝火旺盛吃什么中成药| 农历3月是什么星座| 五行属火适合什么行业| tao是什么意思| 聘书是什么| 慵懒是什么意思| 舒筋健腰丸主治什么| 茉莉龙珠是什么茶| 载脂蛋白b高是什么原因| 阴阳先生是干什么的| 什么叫红颜知己| 慕斯蛋糕是什么意思| 天秤座后面是什么星座| 机关单位和事业单位有什么区别| 紫色加红色是什么颜色| pet是什么意思| 肉燕是什么做的| 镜花水月什么意思| fujixerox是什么牌子| 寒冷性荨麻疹是什么原因引起的| 聚精会神的看是什么词语| 高硼硅玻璃是什么材质| 外婆菜是什么菜| 低压偏高是什么原因| 吃粥配什么菜| 肛门下坠是什么原因| 风声鹤唳的意思是什么| 尿白细胞弱阳性什么意思| 点头之交是什么意思| 膀胱炎做什么检查能看出来| 吃姜对身体有什么好处| 什么能美白皮肤而且效果快| 心动过速吃什么药| 左肺上叶钙化灶什么意思| 带状疱疹什么引起的| 收到是什么意思| 腹泻不能吃什么食物| 1992年属什么| 早上口苦是什么原因| 下发是什么意思| 便潜血阳性什么意思| 宫腔粘连带是什么意思| category是什么意思| puma是什么品牌| 柠檬什么季节成熟| 划扣是什么意思| 水煮鱼用什么鱼| 夏天白鸽煲什么汤最好| 早泄吃什么药最好| 妄语是什么意思| 囊性结构是什么意思| 儿童乐园有什么好玩的| 10月6日什么星座| 槐米是什么| 上颌窦囊肿是什么意思| 尼古丁是什么东西| 自主意识是什么意思| 吃什么解油腻| 女性下体长什么样| 末梢神经炎是什么症状| 肝属于五行中的什么| 便血是什么样的| 三十六计的第一计是什么| 什么是性侵| 排骨汤用什么排骨| 血糖在化验单上叫什么| gbs是什么意思| 金开什么字| 苹果a1661是什么型号| 领空是什么意思| 便秘喝什么药| 疱疹性咽峡炎吃什么药| 机缘是什么意思| 土豆不能和什么一起吃| 梦到自己快要死了是什么意思| 晚上尿床是什么原因| 老鼠为什么怕猫| 不完全性右束支传导阻滞是什么意思| 叶公好龙是什么故事| 手指甲白是什么原因| 小便无力吃什么药| 脚后跟疼用什么药好| 溶菌酶是什么| 乙肝表面抗体定量偏高什么意思| 宀与什么有关| 狗咬人后狗为什么会死| 买单是什么意思| 什么原因引起血糖高| 为什么会有地震| 4月5日是什么星座| original是什么牌子| 混血是什么意思| 湿气重的人适合吃什么| 为什么会有颈纹| 解脲脲原体阳性是什么| 吃什么可以回奶| 银子发黑是什么原因| 大米粉做什么好吃| 受贿是什么意思| 妇科检查清洁度二度是什么意思| 惹上官司是犯了什么煞| 女人左眼跳是什么预兆| 反流性食管炎吃什么食物好| 火棉胶婴儿是什么意思| 什么叫打卡| 尿里有潜血是什么原因| 氯化钠敷脸有什么作用| 45岁属什么的生肖| 为什么怀孕会孕酮低| 扁桃体发炎什么症状| 数字2代表什么意思| 疏离感是什么意思| 思想感情是什么意思| uv是什么意思| 年轻人创业做什么好| 精液什么颜色| 肋间神经痛挂什么科| 信指什么生肖| 牙疼有什么好办法| 请问紫苏叶有什么功效| ak是什么意思| 孕妇肚子疼是什么原因| 喝隔夜茶有什么好处和坏处| m型发际线适合什么发型| 302是什么意思| 女性体毛多是什么原因| 牙龈经常出血是什么原因| 蛋白粉什么时间喝最好| 黄金为什么值钱| 液体套是什么| 朔望月是什么意思| 八朵玫瑰花代表什么意思| 闷骚什么意思| 梦到自己头发白了是什么意思| 默念是什么意思| 十斋日是什么意思| 脊髓病变是什么病| 百鸟归巢什么意思| 薏米有什么作用| 什么是保健食品| 百度
Skip to content

googleapis/langchain-google-cloud-sql-pg-python

Cloud SQL for PostgreSQL for LangChain

preview pypi versions

The Cloud SQL for PostgreSQL for LangChain package provides a first class experience for connecting to Cloud SQL instances from the LangChain ecosystem while providing the following benefits:

  • Simplified & Secure Connections: easily and securely create shared connection pools to connect to Google Cloud databases utilizing IAM for authorization and database authentication without needing to manage SSL certificates, configure firewall rules, or enable authorized networks.
  • Improved performance & Simplified management: use a single-table schema can lead to faster query execution, especially for large collections.
  • Improved metadata handling: store metadata in columns instead of JSON, resulting in significant performance improvements.
  • Clear separation: clearly separate table and extension creation, allowing for distinct permissions and streamlined workflows.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Cloud SQL Admin API.
  4. Setup Authentication.

Installation

Install this library in a virtual environment using venv. venv is a tool that creates isolated Python environments. These isolated environments can have separate versions of Python packages, which allows you to isolate one project's dependencies from the dependencies of other projects.

With venv, it's possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Supported Python Versions

Python >= 3.9

Mac/Linux

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install langchain-google-cloud-sql-pg

Windows

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install langchain-google-cloud-sql-pg

Example Usage

Code samples and snippets live in the samples/ folder.

Vector Store Usage

Use a Vector Store to store embedded data and perform vector search.

from langchain_google_cloud_sql_pg import PostgresVectorstore, PostgresEngine
from langchain.embeddings import VertexAIEmbeddings


engine = PostgresEngine.from_instance("project-id", "region", "my-instance", "my-database")
engine.init_vectorstore_table(
    table_name="my-table",
    vector_size=768,  # Vector size for `VertexAIEmbeddings()`
)
embeddings_service = VertexAIEmbeddings(model_name="textembedding-gecko@003")
vectorstore = PostgresVectorStore.create_sync(
    engine,
    table_name="my-table",
    embeddings=embedding_service
)

See the full Vector Store tutorial.

Document Loader Usage

Use a document loader to load data as Documents.

from langchain_google_cloud_sql_pg import PostgresEngine, PostgresLoader


engine = PostgresEngine.from_instance("project-id", "region", "my-instance", "my-database")
loader = PostgresSQLLoader.create_sync(
    engine,
    table_name="my-table-name"
)
docs = loader.lazy_load()

See the full Document Loader tutorial.

Chat Message History Usage

Use Chat Message History to store messages and provide conversation history to LLMs.

from langchain_google_cloud_sql_pg import PostgresChatMessageHistory, PostgresEngine


engine = PostgresEngine.from_instance("project-id", "region", "my-instance", "my-database")
engine.init_chat_history_table(table_name="my-message-store")
history = PostgresChatMessageHistory.create_sync(
    engine,
    table_name="my-message-store",
    session_id="my-session_id"
)

See the full Chat Message History tutorial.

Langgraph Checkpoint Usage

Use PostgresSaver to save snapshots of the graph state at a given point in time.

from langchain_google_cloud_sql_pg import PostgresSaver, PostgresEngine

engine = PostgresEngine.from_instance("project-id", "region", "my-instance", "my-database")
checkpoint = PostgresSaver.create_sync(engine)

See the full Checkpoint tutorial.

Example Usage

Code examples can be found in the samples/ folder.

Converting between Sync & Async Usage

Async functionality improves the speed and efficiency of database connections through concurrency, which is key for providing enterprise quality performance and scaling in GenAI applications. This package uses a native async Postgres driver, asyncpg, to optimize Python's async functionality.

LangChain supports async programming, since LLM based application utilize many I/O-bound operations, such as making API calls to language models, databases, or other services. All components should provide both async and sync versions of all methods.

asyncio is a Python library used for concurrent programming and is used as the foundation for multiple Python asynchronous frameworks. asyncio uses async / await syntax to achieve concurrency for non-blocking I/O-bound tasks using one thread with cooperative multitasking instead of multi-threading.

Converting Sync to Async

Update sync methods to await async methods

engine = await PostgresEngine.afrom_instance("project-id", "region", "my-instance", "my-database")
await engine.ainit_vectorstore_table(table_name="my-table", vector_size=768)
vectorstore = await PostgresVectorStore.create(
   engine,
   table_name="my-table",
   embedding_service=VertexAIEmbeddings(model_name="textembedding-gecko@003")
)

Run the code: notebooks

ipython and jupyter notebooks support the use of the await keyword without any additional setup

Run the code: FastAPI

Update routes to use async def.

@app.get("/invoke/")
async def invoke(query: str):
   return await retriever.ainvoke(query)

Run the code: Local python file

It is recommend to create a top-level async method definition: async def to wrap multiple async methods. Then use asyncio.run() to run the the top-level entrypoint, e.g. "main()"

async def main():
   response = await retriever.ainvoke(query)
   print(response)

asyncio.run(main())

Contributions

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

License

Apache 2.0 - See LICENSE for more information.

Disclaimer

This is not an officially supported Google product.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 16

下巴痘痘反复长是什么原因 77是什么意思 15度穿什么 大便颗粒状是什么原因造成的 频繁是什么意思
40岁男人学什么乐器好 热伤风感冒吃什么药 痔疮长什么样子的图片 过继是什么意思 界限性脑电图是什么意思
94狗跟什么属相配最好 腿部浮肿吃什么药 滑档是什么意思 梦见杀蛇是什么预兆 气短是什么感觉
感冒适合吃什么饭菜 退休是什么意思 为什么不建议做融合手术 宝宝湿疹用什么药膏 查肝肾功能挂什么科
抗炎和消炎有什么区别hcv8jop7ns0r.cn 需要一半留下一半是什么字hcv8jop9ns5r.cn 呓语是什么意思hcv8jop6ns9r.cn 燃眉之急是什么意思hcv9jop2ns7r.cn 合肥原名叫什么名字hcv8jop8ns1r.cn
50岁掉牙齿是什么原因hcv7jop5ns1r.cn 碳酸盐质玉是什么玉hcv8jop7ns3r.cn 老年性脑改变是什么意思hlguo.com 11月29日什么星座hcv8jop5ns8r.cn 1700年是什么朝代xinjiangjialails.com
热感冒吃什么食物好hcv9jop7ns9r.cn 定日是什么意思hcv8jop3ns8r.cn 男人要吃什么才能壮阳hcv8jop1ns1r.cn 画蛇添足的故事告诉我们什么道理hcv9jop7ns1r.cn 胸口闷是什么原因hcv8jop3ns2r.cn
供给侧改革什么意思hcv8jop1ns4r.cn rm是什么意思hcv8jop0ns5r.cn 叔公是什么辈分hcv8jop2ns6r.cn 埋怨是什么意思hcv9jop2ns6r.cn 耳结是什么原因造成的hcv8jop9ns9r.cn
百度