Snowflake Cortex
Snowflake Cortex 让您即时访问由Mistral、Reka、Meta和Google等公司的研究人员训练的行业领先的大型语言模型(LLMs),包括由Snowflake开发的开放企业级模型Snowflake Arctic。
本示例介绍了如何使用LangChain与Snowflake Cortex进行交互。
安装和设置
我们首先使用以下命令安装snowflake-snowpark-python
库。然后我们配置连接到Snowflake的凭据,作为环境变量或直接传递它们。
%pip install --upgrade --quiet snowflake-snowpark-python
import getpass
import os
# First step is to set up the environment variables, to connect to Snowflake,
# you can also pass these snowflake credentials while instantiating the model
if os.environ.get("SNOWFLAKE_ACCOUNT") is None:
os.environ["SNOWFLAKE_ACCOUNT"] = getpass.getpass("Account: ")
if os.environ.get("SNOWFLAKE_USERNAME") is None:
os.environ["SNOWFLAKE_USERNAME"] = getpass.getpass("Username: ")
if os.environ.get("SNOWFLAKE_PASSWORD") is None:
os.environ["SNOWFLAKE_PASSWORD"] = getpass.getpass("Password: ")
if os.environ.get("SNOWFLAKE_DATABASE") is None:
os.environ["SNOWFLAKE_DATABASE"] = getpass.getpass("Database: ")
if os.environ.get("SNOWFLAKE_SCHEMA") is None:
os.environ["SNOWFLAKE_SCHEMA"] = getpass.getpass("Schema: ")
if os.environ.get("SNOWFLAKE_WAREHOUSE") is None:
os.environ["SNOWFLAKE_WAREHOUSE"] = getpass.getpass("Warehouse: ")
if os.environ.get("SNOWFLAKE_ROLE") is None:
os.environ["SNOWFLAKE_ROLE"] = getpass.getpass("Role: ")
from langchain_community.chat_models import ChatSnowflakeCortex
from langchain_core.messages import HumanMessage, SystemMessage
# By default, we'll be using the cortex provided model: `mistral-large`, with function: `complete`
chat = ChatSnowflakeCortex()
上面的单元格假设您的Snowflake凭据已设置在环境变量中。如果您更愿意手动指定它们,请使用以下代码:
chat = ChatSnowflakeCortex(
# Change the default cortex model and function
model="mistral-large",
cortex_function="complete",
# Change the default generation parameters
temperature=0,
max_tokens=10,
top_p=0.95,
# Specify your Snowflake Credentials
account="YOUR_SNOWFLAKE_ACCOUNT",
username="YOUR_SNOWFLAKE_USERNAME",
password="YOUR_SNOWFLAKE_PASSWORD",
database="YOUR_SNOWFLAKE_DATABASE",
schema="YOUR_SNOWFLAKE_SCHEMA",
role="YOUR_SNOWFLAKE_ROLE",
warehouse="YOUR_SNOWFLAKE_WAREHOUSE"
)
调用聊天模型
我们现在可以使用invoke
或stream
方法来调用聊天模型。
消息 = [ 系统消息(内容="你是一个友好的助手。"), 人类消息(内容="什么是大型语言模型?"), ] 聊天.调用(消息)
流
# Sample input prompt
messages = [
SystemMessage(content="You are a friendly assistant."),
HumanMessage(content="What are large language models?"),
]
# Invoke the stream method and print each chunk as it arrives
print("Stream Method Response:")
for chunk in chat._stream(messages):
print(chunk.message.content)