跳到主要内容
Anyscale 是一个完全托管的 Ray 平台,您可以在其上构建、部署和管理可扩展的 AI 和 Python 应用程序。 本示例将介绍如何使用 LangChain 与 Anyscale Endpoint 进行交互。
##Installing the langchain packages needed to use the integration
pip install -qU langchain-community
ANYSCALE_API_BASE = "..."
ANYSCALE_API_KEY = "..."
ANYSCALE_MODEL_NAME = "..."
import os

os.environ["ANYSCALE_API_BASE"] = ANYSCALE_API_BASE
os.environ["ANYSCALE_API_KEY"] = ANYSCALE_API_KEY
from langchain.chains import LLMChain
from langchain_community.llms import Anyscale
from langchain_core.prompts import PromptTemplate
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)
llm = Anyscale(model_name=ANYSCALE_MODEL_NAME)
llm_chain = prompt | llm
question = "When was George Washington president?"

llm_chain.invoke({"question": question})
借助 Ray,我们可以无需异步实现即可分发查询。这不仅适用于 Anyscale LLM 模型,也适用于任何其他未实现 _acall_agenerate 的 LangChain LLM 模型。
prompt_list = [
    "When was George Washington president?",
    "Explain to me the difference between nuclear fission and fusion.",
    "Give me a list of 5 science fiction books I should read next.",
    "Explain the difference between Spark and Ray.",
    "Suggest some fun holiday ideas.",
    "Tell a joke.",
    "What is 2+2?",
    "Explain what is machine learning like I am five years old.",
    "Explain what is artifical intelligence.",
]
import ray


@ray.remote(num_cpus=0.1)
def send_query(llm, prompt):
    resp = llm.invoke(prompt)
    return resp


futures = [send_query.remote(llm, prompt) for prompt in prompt_list]
results = ray.get(futures)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.