【langchain使用指南】如何从模型返回结构化数据
LangChain集成了多种国内外AI模型,但功能支持程度不同。国外主流模型如Anthropic、MistralAI等通常支持工具调用、结构化输出等高级功能,而国内模型需要单独查阅文档确认。使用前需安装对应包(如langchain-google-genai或langchain-deepseek),并通过环境变量设置API密钥。例如,调用谷歌Gemini需设置GOOGLE_API_KEY,调用Dee
·
下面这些模型是langchain集成了高级功能的模型,基本都是国外模型,虽然所有这些 LangChain 类都支持指定的高级功能,但您可能有 打开特定于提供程序的文档,了解哪些托管模型或后端支持 该功能。
| Provider | Tool calling | Structured output | JSON mode | Local | Multimodal | Package |
|---|---|---|---|---|---|---|
| ChatAnthropic | ✅ | ✅ | ❌ | ❌ | ✅ | langchain-anthropic |
| ChatMistralAI | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-mistralai |
| ChatFireworks | ✅ | ✅ | ✅ | ❌ | ❌ | langchain-fireworks |
| AzureChatOpenAI | ✅ | ✅ | ✅ | ❌ | ✅ | langchain-openai |
| ChatOpenAI | ✅ | ✅ | ✅ | ❌ | ✅ | langchain-openai |
| ChatTogether | ✅ | ✅ | ✅ | ❌ | ❌ | langchain-together |
| ChatVertexAI | ✅ | ✅ | ❌ | ❌ | ✅ | langchain-google-vertexai |
| ChatGoogleGenerativeAI | ✅ | ✅ | ❌ | ❌ | ✅ | langchain-google-genai |
| ChatGroq | ✅ | ✅ | ✅ | ❌ | ❌ | langchain-groq |
| ChatCohere | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-cohere |
| ChatBedrock | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-aws |
| ChatHuggingFace | ✅ | ✅ | ❌ | ✅ | ❌ | langchain-huggingface |
| ChatNVIDIA | ✅ | ✅ | ✅ | ✅ | ✅ | langchain-nvidia-ai-endpoints |
| ChatOllama | ✅ | ✅ | ✅ | ✅ | ❌ | langchain-ollama |
| ChatLlamaCpp | ✅ | ✅ | ❌ | ✅ | ❌ | langchain-community |
| ChatAI21 | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-ai21 |
| ChatUpstage | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-upstage |
| ChatDatabricks | ✅ | ✅ | ❌ | ❌ | ❌ | databricks-langchain |
| ChatWatsonx | ✅ | ✅ | ✅ | ❌ | ❌ | langchain-ibm |
| ChatXAI | ✅ | ✅ | ❌ | ❌ | ❌ | langchain-xai |
| ChatPerplexity | ❌ | ✅ | ✅ | ❌ | ✅ | langchain-perplexity |
| ChatOCIGenAI | ✅ | ✅ | ✅ | ❌ | ✅ | langchain-oci |
langchain支持的所有模型如下所示,但是没有像集成像国外模型那样的功能,需要手动阅读文档去查看
| Name | Description |
|---|---|
| Abso | This will help you get started with ChatAbso chat models. For detaile... |
| AI21 Labs | This notebook covers how to get started with AI21 chat models. |
| Alibaba Cloud PAI EAS | Alibaba Cloud PAI (Platform for AI) is a lightweight and cost-efficie... |
| Anthropic | This notebook provides a quick overview for getting started with Anth... |
| Anyscale | This notebook demonstrates the use of langchain.chat_models.ChatAnysc... |
| AzureAIChatCompletionsModel | This will help you get started with AzureAIChatCompletionsModel chat ... |
| Azure OpenAI | This guide will help you get started with AzureOpenAI chat models. Fo... |
| Azure ML Endpoint | Azure Machine Learning is a platform used to build, train, and deploy... |
| Baichuan Chat | Baichuan chat models API by Baichuan Intelligent Technology. For more... |
| Baidu Qianfan | Baidu AI Cloud Qianfan Platform is a one-stop large model development... |
| AWS Bedrock | This doc will help you get started with AWS Bedrock chat models. Amaz... |
| Cerebras | This notebook provides a quick overview for getting started with Cere... |
| CloudflareWorkersAI | This will help you get started with CloudflareWorkersAI chat models. ... |
| Cohere | This notebook covers how to get started with Cohere chat models. |
| ContextualAI | This will help you get started with Contextual AI's Grounded Language... |
| Coze Chat | ChatCoze chat models API by coze.com. For more information, see https... |
| Dappier AI | Dappier: Powering AI with Dynamic, Real-Time Data Models |
| Databricks | Databricks Lakehouse Platform unifies data, analytics, and AI on one ... |
| DeepInfra | DeepInfra is a serverless inference as a service that provides access... |
| DeepSeek | This will help you get started with DeepSeek's hosted chat models. Fo... |
| Eden AI | Eden AI is revolutionizing the AI landscape by uniting the best AI pr... |
| EverlyAI | EverlyAI allows you to run your ML models at scale in the cloud. It a... |
| Featherless AI | This will help you get started with FeatherlessAi chat models. For de... |
| Fireworks | This doc helps you get started with Fireworks AI chat models. For det... |
| ChatFriendli | Friendli enhances AI application performance and optimizes cost savin... |
| Goodfire | This will help you get started with Goodfire chat models. For detaile... |
| Google Gemini | Access Google's Generative AI models, including the Gemini family, di... |
| Google Cloud Vertex AI | This page provides a quick overview for getting started with VertexAI... |
| GPTRouter | GPTRouter is an open source LLM API Gateway that offers a universal A... |
| DigitalOcean Gradient | This will help you getting started with DigitalOcean Gradient Chat Mo... |
| GreenNode | GreenNode is a global AI solutions provider and a NVIDIA Preferred Pa... |
| Groq | This will help you get started with Groq chat models. For detailed do... |
| ChatHuggingFace | This will help you get started with langchainhuggingface chat models.... |
| IBM watsonx.ai | ChatWatsonx is a wrapper for IBM watsonx.ai foundation models. |
| JinaChat | This notebook covers how to get started with JinaChat chat models. |
| Kinetica | This notebook demonstrates how to use Kinetica to transform natural l... |
| Konko | Konko API is a fully managed Web API designed to help application dev... |
| LiteLLM | LiteLLM is a library that simplifies calling Anthropic, Azure, Huggin... |
| Llama 2 Chat | This notebook shows how to augment Llama-2 LLMs with the Llama2Chat w... |
| Llama API | This notebook shows how to use LangChain with LlamaAPI - a hosted ver... |
| LlamaEdge | LlamaEdge allows you to chat with LLMs of GGUF format both locally an... |
| Llama.cpp | llama.cpp python library is a simple Python bindings for @ggerganov |
| maritalk | MariTalk is an assistant developed by the Brazilian company Maritaca ... |
| MiniMax | Minimax is a Chinese startup that provides LLM service for companies ... |
| MistralAI | This will help you get started with Mistral chat models. For detailed... |
| MLX | This notebook shows how to get started using MLX LLM's as chat models. |
| ModelScope | ModelScope (Home | GitHub) is built upon the notion of “Model-as-a-Se... |
| Moonshot | Moonshot is a Chinese startup that provides LLM service for companies... |
| Naver | This notebook provides a quick overview for getting started with Nave... |
| Nebius | This page will help you get started with Nebius AI Studio chat models... |
| Netmind | This will help you get started with Netmind chat models. For detailed... |
| NVIDIA AI Endpoints | This will help you get started with NVIDIA chat models. For detailed ... |
| ChatOCIModelDeployment | This will help you get started with OCIModelDeployment chat models. F... |
| OCIGenAI | This notebook provides a quick overview for getting started with OCIG... |
| ChatOctoAI | OctoAI offers easy access to efficient compute and enables users to i... |
| Ollama | Ollama allows you to run open-source large language models, such as g... |
| OpenAI | This notebook provides a quick overview for getting started with Open... |
| Outlines | This will help you get started with Outlines chat models. For detaile... |
| Perplexity | This page will help you get started with Perplexity chat models. For ... |
| Pipeshift | This will help you get started with Pipeshift chat models. For detail... |
| ChatPredictionGuard | Prediction Guard is a secure, scalable GenAI platform that safeguards... |
| PremAI | PremAI is an all-in-one platform that simplifies the creation of robu... |
| PromptLayer ChatOpenAI | This example showcases how to connect to PromptLayer to start recordi... |
| Qwen | This will help you get started with Qwen chat models. For detailed do... |
| Qwen QwQ | This will help you get started with QwQ chat models. For detailed doc... |
| Reka | This notebook provides a quick overview for getting started with Reka... |
| RunPod Chat Model | Get started with RunPod chat models. |
| SambaNovaCloud | This will help you get started with SambaNovaCloud chat models. For d... |
| SambaStudio | This will help you get started with SambaStudio chat models. For deta... |
| ChatSeekrFlow | Seekr provides AI-powered solutions for structured, explainable, and ... |
| Snowflake Cortex | Snowflake Cortex gives you instant access to industry-leading large l... |
| solar | Deprecated since version 0.0.34: Use langchain_upstage.ChatUpstage in... |
| SparkLLM Chat | SparkLLM chat models API by iFlyTek. For more information, see iFlyTe... |
| Nebula (Symbl.ai) | This notebook covers how to get started with Nebula - Symbl.ai's chat... |
| Tencent Hunyuan | Tencent's hybrid model API (Hunyuan API) |
| Together | This page will help you get started with Together AI chat models. For... |
| Tongyi Qwen | Tongyi Qwen is a large language model developed by Alibaba's Damo Aca... |
| Upstage | This notebook covers how to get started with Upstage chat models. |
| vectara | Vectara is the trusted AI Assistant and Agent platform, which focuses... |
| vLLM Chat | vLLM can be deployed as a server that mimics the OpenAI API protocol.... |
| Volc Engine Maas | This notebook provides you with a guide on how to get started with vo... |
| Chat Writer | This notebook provides a quick overview for getting started with Writ... |
| xAI | This page will help you get started with xAI chat models. For detaile... |
| Xinference | Xinference is a powerful and versatile library designed to serve LLMs, |
| YandexGPT | This notebook goes over how to use Langchain with YandexGPT chat mode... |
| ChatYI | This will help you get started with Yi chat models. For detailed docu... |
| Yuan2.0 | This notebook shows how to use YUAN2 API in LangChain with the langch... |
| ZHIPU AI | This notebook shows how to use ZHIPU AI API in LangChain with the lan... |
我们需要怎么从langchain导入模型,通常是需要模型的api进行导入,api要自行准备,通过设置为环境变量,下面演示一下谷歌的gemini和国内的deepseek
如果要导入对应大厂的模型,需要先安装对应模型的langchain包,首先来演示gemini:
pip install -qU "langchain[google-genai]"
import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
os.environ["GOOGLE_API_KEY"] = ""#输入你的api_key,或者可以输入到.env文件放在同一目录下
load_dotenv(override = True)
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
model = init_chat_model("gemini-2.5-flash",model_provider="google_genai")
接着到deepseek的,依然需要安装对应包,其他模型类似
pip install langchain-deepseek
import os
from detenv import load_dotenv
from langchain.chat_models import init_chat_model
os.environ["DEEPSEEK_API_KEY"] = ""#填写你的api_key
load_dotenv(override = True)
DeepSeek_API_KEY = os.getenv("DEEPSEEK_API_KEY")
model = init_chat_model(model = "deepseek-chat", model_provider = "deepseek")
更多推荐

所有评论(0)