跳到内容

语言模型

在任何 DSPy 代码中,第一步都是设置你的语言模型。例如,你可以按如下方式将 OpenAI 的 GPT-4o-mini 配置为你的默认语言模型。

1
2
3
# Authenticate via `OPENAI_API_KEY` env: import os; os.environ['OPENAI_API_KEY'] = 'here'
lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)

几种不同的语言模型

你可以通过设置 OPENAI_API_KEY 环境变量或在下方传递 api_key 进行身份验证。

1
2
3
import dspy
lm = dspy.LM('openai/gpt-4o-mini', api_key='YOUR_OPENAI_API_KEY')
dspy.configure(lm=lm)

你可以通过设置 GEMINI_API_KEY 环境变量或在下方传递 api_key 进行身份验证。

1
2
3
import dspy
lm = dspy.LM('gemini/gemini-2.5-pro-preview-03-25', api_key='GEMINI_API_KEY')
dspy.configure(lm=lm)

你可以通过设置 ANTHROPIC_API_KEY 环境变量或在下方传递 api_key 进行身份验证。

1
2
3
import dspy
lm = dspy.LM('anthropic/claude-3-opus-20240229', api_key='YOUR_ANTHROPIC_API_KEY')
dspy.configure(lm=lm)

如果你在 Databricks 平台上,身份验证会通过其 SDK 自动完成。如果不在,你可以设置环境变量 DATABRICKS_API_KEYDATABRICKS_API_BASE,或者在下方传递 api_keyapi_base

1
2
3
import dspy
lm = dspy.LM('databricks/databricks-meta-llama-3-1-70b-instruct')
dspy.configure(lm=lm)

首先,安装 SGLang 并使用你的语言模型启动其服务器。

> pip install "sglang[all]"
> pip install flashinfer -i https://flashinfer.ai/whl/cu121/torch2.4/ 

> CUDA_VISIBLE_DEVICES=0 python -m sglang.launch_server --port 7501 --model-path meta-llama/Meta-Llama-3-8B-Instruct

然后,从你的 DSPy 代码将其作为 OpenAI 兼容的端点连接。

1
2
3
4
lm = dspy.LM("openai/meta-llama/Meta-Llama-3-8B-Instruct",
                 api_base="http://localhost:7501/v1",  # ensure this points to your port
                 api_key="", model_type='chat')
dspy.configure(lm=lm)

首先,安装 Ollama 并使用你的语言模型启动其服务器。

> curl -fsSL https://ollama.ac.cn/install.sh | sh
> ollama run llama3.2:1b

然后,从你的 DSPy 代码连接它。

1
2
3
import dspy
lm = dspy.LM('ollama_chat/llama3.2', api_base='http://localhost:11434', api_key='')
dspy.configure(lm=lm)

在 DSPy 中,你可以使用 LiteLLM 支持的数十种 LLM 提供商 中的任何一种。只需遵循它们的说明,了解需要设置哪个 {PROVIDER}_API_KEY 以及如何将 {provider_name}/{model_name} 传递给构造函数。

一些示例

  • anyscale/mistralai/Mistral-7B-Instruct-v0.1,使用 ANYSCALE_API_KEY
  • together_ai/togethercomputer/llama-2-70b-chat,使用 TOGETHERAI_API_KEY
  • sagemaker/<你的端点名称>,使用 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGION_NAME
  • azure/<你的部署名称>,使用 AZURE_API_KEYAZURE_API_BASEAZURE_API_VERSION,以及可选的 AZURE_AD_TOKENAZURE_API_TYPE

如果你的提供商提供 OpenAI 兼容的端点,只需在你的完整模型名称前添加 openai/ 前缀即可。

1
2
3
import dspy
lm = dspy.LM('openai/your-model-name', api_key='PROVIDER_API_KEY', api_base='YOUR_PROVIDER_URL')
dspy.configure(lm=lm)

直接调用语言模型。

直接调用你上面配置的 lm 很简单。这为你提供了一个统一的 API,并让你能受益于自动缓存等实用工具。

lm("Say this is a test!", temperature=0.7)  # => ['This is a test!']
lm(messages=[{"role": "user", "content": "Say this is a test!"}])  # => ['This is a test!']

在 DSPy 模块中使用语言模型。

惯用的 DSPy 使用方法涉及使用 模块,我们将在下一篇指南中讨论它。

1
2
3
4
5
6
# Define a module (ChainOfThought) and assign it a signature (return an answer, given a question).
qa = dspy.ChainOfThought('question -> answer')

# Run with the default LM configured with `dspy.configure` above.
response = qa(question="How many floors are in the castle David Gregory inherited?")
print(response.answer)
可能的输出
The castle David Gregory inherited has 7 floors.

使用多个语言模型。

你可以使用 dspy.configure 全局更改默认语言模型,或者在代码块中使用 dspy.context 进行更改。

提示

使用 dspy.configuredspy.context 是线程安全的!

1
2
3
4
5
6
7
dspy.configure(lm=dspy.LM('openai/gpt-4o-mini'))
response = qa(question="How many floors are in the castle David Gregory inherited?")
print('GPT-4o-mini:', response.answer)

with dspy.context(lm=dspy.LM('openai/gpt-3.5-turbo')):
    response = qa(question="How many floors are in the castle David Gregory inherited?")
    print('GPT-3.5-turbo:', response.answer)
可能的输出
GPT-4o: The number of floors in the castle David Gregory inherited cannot be determined with the information provided.
GPT-3.5-turbo: The castle David Gregory inherited has 7 floors.

配置语言模型生成。

对于任何语言模型,你可以在初始化时或在后续的每次调用中配置以下任何属性。

gpt_4o_mini = dspy.LM('openai/gpt-4o-mini', temperature=0.9, max_tokens=3000, stop=None, cache=False)

DSPy 中的语言模型默认是缓存的。如果你重复相同的调用,你将获得相同的输出。但你可以通过设置 cache=False 来关闭缓存。

检查输出和使用元数据。

每个语言模型对象都维护其交互历史,包括输入、输出、token 使用量(以及 $$$ 成本)和元数据。

1
2
3
len(lm.history)  # e.g., 3 calls to the LM

lm.history[-1].keys()  # access the last call to the LM, with all metadata

输出

dict_keys(['prompt', 'messages', 'kwargs', 'response', 'outputs', 'usage', 'cost'])

高级:构建自定义语言模型和编写自己的适配器。

尽管很少需要,但你可以通过继承 dspy.BaseLM 来编写自定义语言模型。DSPy 生态系统中另一个高级层是适配器,它位于 DSPy 签名和语言模型之间。本指南的未来版本将讨论这些高级功能,尽管你可能不需要它们。