langchain 部署組件-LangServe

原文:🦜?🏓 LangServe | 🦜?🔗 Langchain

LangServe?

🚩 We will be releasing a hosted version of LangServe for one-click deployments of LangChain applications.?Sign up here?to get on the waitlist.

Overview?

LangServe?helps developers deploy?LangChain?runnables and chains?as a REST API.

This library is integrated with?FastAPI?and uses?pydantic?for data validation.

In addition, it provides a client that can be used to call into runnables deployed on a server. A javascript client is available in?LangChainJS.

Features?

  • Input and Output schemas automatically inferred from your LangChain object, and enforced on every API call, with rich error messages
  • API docs page with JSONSchema and Swagger (insert example link)
  • Efficient?/invoke,?/batch?and?/stream?endpoints with support for many concurrent requests on a single server
  • /stream_log?endpoint for streaming all (or some) intermediate steps from your chain/agent
  • Playground page at?/playground?with streaming output and intermediate steps
  • Built-in (optional) tracing to?LangSmith, just add your API key (see?Instructions])
  • All built with battle-tested open-source Python libraries like FastAPI, Pydantic, uvloop and asyncio.
  • Use the client SDK to call a LangServe server as if it was a Runnable running locally (or call the HTTP API directly)
  • LangServe Hub

Limitations?

  • Client callbacks are not yet supported for events that originate on the server
  • OpenAPI docs will not be generated when using Pydantic V2. Fast API does not support?mixing pydantic v1 and v2 namespaces. See section below for more details.

Hosted LangServe?

We will be releasing a hosted version of LangServe for one-click deployments of LangChain applications.?Sign up here?to get on the waitlist.

Security?

  • Vulnerability in Versions 0.0.13 - 0.0.15 -- playground endpoint allows accessing arbitrary files on server.?Resolved in 0.0.16.

Installation?

For both client and server:

pip install "langserve[all]"

or?pip install "langserve[client]"?for client code, and?pip install "langserve[server]"?for server code.

LangChain CLI 🛠??

Use the?LangChain?CLI to bootstrap a?LangServe?project quickly.

To use the langchain CLI make sure that you have a recent version of?langchain-cli?installed. You can install it with?pip install -U langchain-cli.

langchain app new ../path/to/directory

Examples?

Get your LangServe instance started quickly with?LangChain Templates.

For more examples, see the templates?index?or the?examples?directory.

Server?

Here's a server that deploys an OpenAI chat model, an Anthropic chat model, and a chain that uses the Anthropic model to tell a joke about a topic.

#!/usr/bin/env python
from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langserve import add_routesapp = FastAPI(title="LangChain Server",version="1.0",description="A simple api server using Langchain's Runnable interfaces",
)add_routes(app,ChatOpenAI(),path="/openai",
)add_routes(app,ChatAnthropic(),path="/anthropic",
)model = ChatAnthropic()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
add_routes(app,prompt | model,path="/joke",
)if __name__ == "__main__":import uvicornuvicorn.run(app, host="localhost", port=8000)

Docs?

If you've deployed the server above, you can view the generated OpenAPI docs using:

?? If using pydantic v2, docs will not be generated for invoke/batch/stream/stream_log. See?Pydantic?section below for more details.

curl localhost:8000/docs

make sure to?add?the?/docs?suffix.

?? Index page?/?is not defined by?design, so?curl localhost:8000?or visiting the URL will return a 404. If you want content at?/?define an endpoint?@app.get("/").

Client?

Python SDK

from langchain.schema import SystemMessage, HumanMessage
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnableMap
from langserve import RemoteRunnableopenai = RemoteRunnable("http://localhost:8000/openai/")
anthropic = RemoteRunnable("http://localhost:8000/anthropic/")
joke_chain = RemoteRunnable("http://localhost:8000/joke/")joke_chain.invoke({"topic": "parrots"})# or async
await joke_chain.ainvoke({"topic": "parrots"})prompt = [SystemMessage(content='Act like either a cat or a parrot.'),HumanMessage(content='Hello!')
]# Supports astream
async for msg in anthropic.astream(prompt):print(msg, end="", flush=True)prompt = ChatPromptTemplate.from_messages([("system", "Tell me a long story about {topic}")]
)# Can define custom chains
chain = prompt | RunnableMap({"openai": openai,"anthropic": anthropic,
})chain.batch([{ "topic": "parrots" }, { "topic": "cats" }])

In TypeScript (requires LangChain.js version 0.0.166 or later):

import { RemoteRunnable } from "langchain/runnables/remote";const chain = new RemoteRunnable({url: `http://localhost:8000/joke/`,
});
const result = await chain.invoke({topic: "cats",
});

Python using?requests:

import requests
response = requests.post("http://localhost:8000/joke/invoke/",json={'input': {'topic': 'cats'}}
)
response.json()

You can also use?curl:

curl --location --request POST 'http://localhost:8000/joke/invoke/' \--header 'Content-Type: application/json' \--data-raw '{"input": {"topic": "cats"}}'

Endpoints?

The following code:

...
add_routes(app,runnable,path="/my_runnable",
)

adds of these endpoints to the server:

  • POST /my_runnable/invoke?- invoke the runnable on a single input
  • POST /my_runnable/batch?- invoke the runnable on a batch of inputs
  • POST /my_runnable/stream?- invoke on a single input and stream the output
  • POST /my_runnable/stream_log?- invoke on a single input and stream the output, including output of intermediate steps as it's generated
  • GET /my_runnable/input_schema?- json schema for input to the runnable
  • GET /my_runnable/output_schema?- json schema for output of the runnable
  • GET /my_runnable/config_schema?- json schema for config of the runnable

These endpoints match the?LangChain Expression Language interface?-- please reference this documentation for more details.

Playground?

You can find a playground page for your runnable at?/my_runnable/playground. This exposes a simple UI to?configure?and invoke your runnable with streaming output and intermediate steps.

Widgets?

The playground supports?widgets?and can be used to test your runnable with different inputs.

In addition, for configurable runnables, the playground will allow you to configure the runnable and share a link with the configuration:

Sharing

?

Legacy Chains?

LangServe works with both Runnables (constructed via?LangChain Expression Language) and legacy chains (inheriting from?Chain). However, some of the input schemas for legacy chains may be incomplete/incorrect, leading to errors. This can be fixed by updating the?input_schema?property of those chains in LangChain. If you encounter any errors, please open an issue on THIS repo, and we will work to address it.

Deployment?

Deploy to GCP?

You can deploy to GCP Cloud Run using the following command:

gcloud run deploy [your-service-name] --source . --port 8001 --allow-unauthenticated --region us-central1 --set-env-vars=OPENAI_API_KEY=your_key

Pydantic?

LangServe provides support for Pydantic 2 with some limitations.

  1. OpenAPI docs will not be generated for invoke/batch/stream/stream_log when using Pydantic V2. Fast API does not support?[mixing pydantic v1 and v2 namespaces].
  2. LangChain uses the v1 namespace in Pydantic v2. Please read the?following guidelines to ensure compatibility with LangChain

Except for these limitations, we expect the API endpoints, the playground and any other features to work as expected.

Advanced?

Handling Authentication?

If you need to add authentication to your server, please reference FastAPI's?security documentation?and?middleware documentation.

Files?

LLM applications often deal with files. There are different architectures that can be made to implement file processing; at a high level:

  1. The file may be uploaded to the server via a dedicated endpoint and processed using a separate endpoint
  2. The file may be uploaded by either value (bytes of file) or reference (e.g., s3 url to file content)
  3. The processing endpoint may be blocking or non-blocking
  4. If significant processing is required, the processing may be offloaded to a dedicated process pool

You should determine what is the appropriate architecture for your application.

Currently, to upload files by value to a runnable, use base64 encoding for the file (multipart/form-data?is not supported yet).

Here's an?example?that shows how to use base64 encoding to send a file to a remote runnable.

Remember, you can always upload files by reference (e.g., s3 url) or upload them as multipart/form-data to a dedicated endpoint.

Custom Input and Output Types?

Input and Output types are defined on all runnables.

You can access them via the?input_schema?and?output_schema?properties.

LangServe?uses these types for validation and documentation.

If you want to override the default inferred types, you can use the?with_types?method.

Here's a toy example to illustrate the idea:

from typing import Anyfrom fastapi import FastAPI
from langchain.schema.runnable import RunnableLambdaapp = FastAPI()def func(x: Any) -> int:"""Mistyped function that should accept an int but accepts anything."""return x + 1runnable = RunnableLambda(func).with_types(input_schema=int,
)add_routes(app, runnable)

Custom User Types?

Inherit from?CustomUserType?if you want the data to de-serialize into a pydantic model rather than the equivalent dict representation.

At the moment, this type only works?server?side and is used to specify desired?decoding?behavior. If inheriting from this type the server will keep the decoded type as a pydantic model instead of converting it into a dict.

from fastapi import FastAPI
from langchain.schema.runnable import RunnableLambdafrom langserve import add_routes
from langserve.schema import CustomUserTypeapp = FastAPI()class Foo(CustomUserType):bar: intdef func(foo: Foo) -> int:"""Sample function that expects a Foo type which is a pydantic model"""assert isinstance(foo, Foo)return foo.bar# Note that the input and output type are automatically inferred!
# You do not need to specify them.
# runnable = RunnableLambda(func).with_types( # <-- Not needed in this case
#     input_schema=Foo,
#     output_schema=int,
# 
add_routes(app, RunnableLambda(func), path="/foo")

Playground Widgets?

The playground allows you to define custom widgets for your runnable from the backend.

  • A widget is specified at the field level and shipped as part of the JSON schema of the input type
  • A widget must contain a key called?type?with the value being one of a well known list of widgets
  • Other widget keys will be associated with values that describe paths in a JSON object

General schema:

type JsonPath = number | string | (number | string)[];
type NameSpacedPath = { title: string; path: JsonPath }; // Using title to mimick json schema, but can use namespace
type OneOfPath = { oneOf: JsonPath[] };type Widget = {type: string // Some well known type (e.g., base64file, chat etc.)[key: string]: JsonPath | NameSpacedPath | OneOfPath;
};

File Upload Widget?

Allows creation of a file upload input in the UI playground for files that are uploaded as base64 encoded strings. Here's the full?example.

Snippet:

try:from pydantic.v1 import Field
except ImportError:from pydantic import Fieldfrom langserve import CustomUserType# ATTENTION: Inherit from CustomUserType instead of BaseModel otherwise
#            the server will decode it into a dict instead of a pydantic model.
class FileProcessingRequest(CustomUserType):"""Request including a base64 encoded file."""# The extra field is used to specify a widget for the playground UI.file: str = Field(..., extra={"widget": {"type": "base64file"}})num_chars: int = 100

Example widget:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/160648.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/160648.shtml
英文地址,請注明出處:http://en.pswp.cn/news/160648.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

OpenLayers入門,OpenLayers6的WebGLPointsLayer圖層樣式和運算符詳解,四種symbolType類型案例

專欄目錄: OpenLayers入門教程匯總目錄 前言 本章講解使用OpenLayers6的WebGL圖層顯示大量點情況下,列舉出所有WebGLPointsLayer圖層所支持的所有樣式運算符大全。 補充說明 本篇主要介紹OpenLayers6.x版本的webgl圖層,OpenLayers7.x和OpenLayers8.x主要更新內容就是webgl…

GB28181學習(十七)——基于jrtplib實現tcp被動和主動發流

前言 GB/T28181-2022實時流的傳輸方式介紹&#xff1a;https://blog.csdn.net/www_dong/article/details/134255185 基于jrtplib實現tcp被動和主動收流介紹&#xff1a;https://blog.csdn.net/www_dong/article/details/134451387 本文主要介紹下級平臺或設備發流功能&#…

生活如果真能像隊列一樣的話

生活如果真能像隊列一樣&#xff0c;那該多好啊。 —————————————————————————————————————————— 背包&#xff0c;隊列 可以先看他們的API&#xff1a;都含有一個無參構造函數&#xff0c;添加單個元素的方法&#xff0c;測試集合…

php項目從寶塔面板切換轉到phpstudy小皮面板

寶塔面板轉phpstudy面板 版本 寶塔面板8.0.1 phpstudy面板8.1.1.3 步驟 1、寶塔面板,找到項目文件夾,打包、下載到本地、解壓 2、本地windows系統安裝phpstudy面板,選擇盡可能一樣的配置 比如寶塔php7.4.33,可能phpstudy面板只有php7.4.3,也行 大環境一定要一致,比如…

力扣算法練習BM46—最小的K個數

題目 給定一個長度為 n 的可能有重復值的數組&#xff0c;找出其中不去重的最小的 k 個數。例如數組元素是4,5,1,6,2,7,3,8這8個數字&#xff0c;則最小的4個數字是1,2,3,4(任意順序皆可)。 數據范圍&#xff1a;0≤k,n≤10000&#xff0c;數組中每個數的大小0≤val≤1000 要…

linux signal 機制

ref&#xff1a; Linux操作系統學習筆記&#xff08;十六&#xff09;進程間通信之信號 | Ty-Chens Home https://www.cnblogs.com/renxinyuan/p/3867593.html 當執行kill -9 PID時系統發生了什么 -

Codeforces Round 910 (Div. 2) D. Absolute Beauty

D. Absolute Beauty 有兩個長度為 n n n 的整數數組 a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1?,a2?,…,an? 和 b 1 , b 2 , … , b n b_1,b_2,\ldots,b_n b1?,b2?,…,bn? 。他將數組 b b b 的美麗值定義為 ∑ i 1 n ∣ a i ? b i ∣ . \sum_{i1}^{n} |a_i - b…

基于材料生成算法優化概率神經網絡PNN的分類預測 - 附代碼

基于材料生成算法優化概率神經網絡PNN的分類預測 - 附代碼 文章目錄 基于材料生成算法優化概率神經網絡PNN的分類預測 - 附代碼1.PNN網絡概述2.變壓器故障診街系統相關背景2.1 模型建立 3.基于材料生成優化的PNN網絡5.測試結果6.參考文獻7.Matlab代碼 摘要&#xff1a;針對PNN神…

JDK命令使用總結

目錄 javacjava javac 將源碼(*.java)編譯成字節碼(*.class) javac HelloWorld.javajava 運行字節碼(*.class) 不能加后綴名 java HelloWorld直接運行單文件源碼(*.java) Java11以上才支持 java HelloWorld.java

ROSNS3(一)

https://github.com/malintha/rosns3 第一步&#xff1a;clone和構建rosns3客戶端 第二步&#xff1a;運行 最詳細的ubuntu 安裝 docker教程 - 知乎 1. unable to find source space /home/muta/src 解決方法&#xff1a; 將副將將碰到的bug&#xff0c;解決方法_#include &…

【C++ Primer Plus學習記錄】遞增運算符(++)和遞減運算符(--)

遞增運算符&#xff08;&#xff09;和遞減運算符&#xff08;--&#xff09;&#xff1a;前綴版本位于操作數前面&#xff0c;如x&#xff1b;后綴版本位于操作數后面&#xff0c;如x。兩個版本對操作數的影響是一樣的&#xff0c;但是影響的時間不同。這就像吃飯前買單和吃飯…

Python從零開始快速搭建一個語音對話機器人

文章目錄 01-初心緣由02-準備工作03-語音機器人的搭建思路04-語音生成音頻文件05-音頻文件轉文字STT06-與圖靈機器人對話07-文字轉語音08-語音對話機器人的完整代碼09-結束語10-有問必答關于Python技術儲備一、Python所有方向的學習路線二、Python基礎學習視頻三、精品Python學…

SSH連接遠程服務器報錯:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED 解決方法

一.錯誤描述 報錯信息里提示了路徑信息/root/.ssh/known_hosts:20 二.解決方案 方法一 輸入以下指令&#xff1a; ssh-keygen -R XXX&#xff08;需要連接遠程服務器的ip&#xff09; 按照我的例子ip:10.165.7.136&#xff0c;會返回以下信息: 重新嘗試連接&#xff1a; 輸…

C++學習 --set

目錄 1&#xff0c; 什么是set 2&#xff0c; 創建set 2-1&#xff0c; 標準數據類型 2-2&#xff0c; 自定義數據類型 2-3&#xff0c; 其他創建方式 3&#xff0c; 操作set 3-1&#xff0c; 賦值 3-2&#xff0c; 添加元素&#xff08;insert&#xff09; 3-2-1&…

MySQL的樂觀鎖和悲觀鎖

1、樂觀鎖&#xff1a; 樂觀鎖在操作數據的時候&#xff0c;是保持一種樂觀的狀態&#xff0c;認為別的線程是不會同時修改數據的&#xff0c;所以是不會上鎖的&#xff0c;但是在更新的時候&#xff0c;會判斷一下在這個期間內是否有別的線程修改過數據。 主要的流程&#x…

規劃類3d全景線上云展館幫助企業輕松拓展海外市場

科技3D線上云展館作為一種基于VR虛擬現實和互聯網技術的新一代展覽平臺。可以在線上虛擬空間中模擬真實的展館&#xff0c;讓觀眾無需親自到場&#xff0c;即可獲得沉浸式的參觀體驗。通過這個展館&#xff0c;您可以充分、全面、立體展示您的產品、服務以及各種創意作品&#…

python運算符重載之成員關系和屬性運算

1 python運算符重載之成員關系和屬性運算 1.1 重載成員關系運算符 1.1.1 contains,iter,getitem python使用成員關系運算符in時&#xff0c; 按優先級調用方法&#xff1a;contains>iter>getitem&#xff1b; class MyIters:def __init__(self,value):self.datavalu…

2023年【安全生產監管人員】考試題及安全生產監管人員找解析

題庫來源&#xff1a;安全生產模擬考試一點通公眾號小程序 安全生產監管人員考試題參考答案及安全生產監管人員考試試題解析是安全生產模擬考試一點通題庫老師及安全生產監管人員操作證已考過的學員匯總&#xff0c;相對有效幫助安全生產監管人員找解析學員順利通過考試。 1、…

【樹莓派】Camera Module 使用

工具 RPI4RPI Camera Module 2 硬件安裝 直接插到板子的相機帶子插口上即可 使用前提 libcamera-hello運行這個命令能夠成功&#xff0c;否則需要裝相應的包 在 RPI4 上測試 libcamera-jpeg -o 00001.jpg -t 2000 --width 640 --height 480t 表示程序運行&#xff08;預…