AI 命令#

您可以使用 %ai 单元魔法来运行特殊命令。如果模型的名称与命令相同,您需要使用 provider:model 格式来引用该模型;model 简写将无法使用。

[1]:
%reload_ext jupyter_ai

获取帮助#

%ai help 将显示所有支持的命令列表。

[2]:
%ai help

Usage: %%ai [OPTIONS] MODEL_ID

  Invokes a language model identified by MODEL_ID, with the prompt being
  contained in all lines after the first. Both local model IDs and global
  model IDs (with the provider ID explicitly prefixed, followed by a colon)
  are accepted.

  To view available language models, please run `%ai list`.

Options:
  -f, --format [code|html|image|json|markdown|math|md|text]
                                  IPython display to use when rendering
                                  output. [default="markdown"]
  -n, --region-name TEXT          AWS region name, e.g. 'us-east-1'. Required
                                  for SageMaker provider; does nothing with
                                  other providers.
  -q, --request-schema TEXT       The JSON object the endpoint expects, with
                                  the prompt being substituted into any value
                                  that matches the string literal '<prompt>'.
                                  Required for SageMaker provider; does
                                  nothing with other providers.
  -p, --response-path TEXT        A JSONPath string that retrieves the
                                  language model's output from the endpoint's
                                  JSON response. Required for SageMaker
                                  provider; does nothing with other providers.
  -m, --model-parameters TEXT     A JSON value that specifies extra values
                                  that will be passed to the model. The
                                  accepted value parsed to a dict, unpacked
                                  and passed as-is to the provider class.
  --help                          Show this message and exit.
------------------------------------------------------------------------------
Usage: %ai [OPTIONS] COMMAND [ARGS]...

  Invokes a subcommand.

Options:
  --help  Show this message and exit.

Commands:
  delete    Delete an alias. See `%ai delete --help` for options.
  error     Explains the most recent error.
  help      Show this message and exit.
  list      List language models. See `%ai list --help` for options.
  register  Register a new alias. See `%ai register --help` for options.
  update    Update the target of an alias. See `%ai update --help` for
            options.

[3]:
%ai register --help

Usage: %ai register [OPTIONS] NAME TARGET

  Register a new alias called NAME for the model or chain named TARGET.

Options:
  --help  Show this message and exit.
[4]:
%ai list

[4]:

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

bedrock

Not applicable.

N/A

bedrock-chat

Not applicable.

N/A

anthropic

ANTHROPIC_API_KEY

anthropic-chat

ANTHROPIC_API_KEY

azure-chat-openai

OPENAI_API_KEY

This provider does not define a list of models.

cohere

COHERE_API_KEY

gpt4all

Not applicable.

N/A

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

openai

OPENAI_API_KEY

openai-chat

OPENAI_API_KEY

qianfan

QIANFAN_AK, QIANFAN_SK

sagemaker-endpoint

Not applicable.

N/A

Specify an endpoint name as the model ID. In addition, you must specify a region name, request schema, and response path. For more information, see the documentation about SageMaker endpoints deployment and about using magic commands with SageMaker endpoints.

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

[5]:
%ai list openai

[5]:

Provider

Environment variable

Set?

Models

openai

OPENAI_API_KEY

模型别名#

使用语法 %ai register NAME TARGET,您可以为现有别名的目标创建一个新别名。目标必须使用完整的 provider:model 语法指定。您无法为另一个别名创建别名。

[6]:
%ai register mychat openai-chat:gpt-4

[6]:

Registered new alias mychat

[7]:
%ai list

[7]:

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

bedrock

Not applicable.

N/A

bedrock-chat

Not applicable.

N/A

anthropic

ANTHROPIC_API_KEY

anthropic-chat

ANTHROPIC_API_KEY

azure-chat-openai

OPENAI_API_KEY

This provider does not define a list of models.

cohere

COHERE_API_KEY

gpt4all

Not applicable.

N/A

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

openai

OPENAI_API_KEY

openai-chat

OPENAI_API_KEY

qianfan

QIANFAN_AK, QIANFAN_SK

sagemaker-endpoint

Not applicable.

N/A

Specify an endpoint name as the model ID. In addition, you must specify a region name, request schema, and response path. For more information, see the documentation about SageMaker endpoints deployment and about using magic commands with SageMaker endpoints.

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

mychat

openai-chat:gpt-4

[8]:
%%ai gpt4
What model is this?

[8]:

Apologies for the confusion, but your question is not clear. Could you please provide more details or context? Are you asking about a specific car, phone, laptop, or other product model? Without this crucial information, it’s impossible to give an accurate answer.

[9]:
%ai update mychat openai:babbage-002

[9]:

Updated target of alias mychat

[10]:
%%ai mychat
Tell me about mathematical symbols

[10]:

No HTML or other code.

What is the difference between an assignment and a function call? Why is an assignment called a value assignment and a function call a value function?

A value function is a function that takes no arguments, is returning a value. A function call is when you type in the name of a function.

Below are the symbols used in the COVID-19 pandemic:

The STARS symbol stands for the Swedish National Board of Health and Welfare.

The HEALTHY symbol stands for the Swedish National Board of Health and Welfare.

The HEALTHY symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for the Swedish National Board of Health and Welfare.

The COVID-19 symbol stands for

[11]:
%ai list

[11]:

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

bedrock

Not applicable.

N/A

bedrock-chat

Not applicable.

N/A

anthropic

ANTHROPIC_API_KEY

anthropic-chat

ANTHROPIC_API_KEY

azure-chat-openai

OPENAI_API_KEY

This provider does not define a list of models.

cohere

COHERE_API_KEY

gpt4all

Not applicable.

N/A

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

openai

OPENAI_API_KEY

openai-chat

OPENAI_API_KEY

qianfan

QIANFAN_AK, QIANFAN_SK

sagemaker-endpoint

Not applicable.

N/A

Specify an endpoint name as the model ID. In addition, you must specify a region name, request schema, and response path. For more information, see the documentation about SageMaker endpoints deployment and about using magic commands with SageMaker endpoints.

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

mychat

openai:babbage-002

[12]:
%ai delete mychat

[12]:

Deleted alias mychat

[13]:
%ai list

[13]:

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

bedrock

Not applicable.

N/A

bedrock-chat

Not applicable.

N/A

anthropic

ANTHROPIC_API_KEY

anthropic-chat

ANTHROPIC_API_KEY

azure-chat-openai

OPENAI_API_KEY

This provider does not define a list of models.

cohere

COHERE_API_KEY

gpt4all

Not applicable.

N/A

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

openai

OPENAI_API_KEY

openai-chat

OPENAI_API_KEY

qianfan

QIANFAN_AK, QIANFAN_SK

sagemaker-endpoint

Not applicable.

N/A

Specify an endpoint name as the model ID. In addition, you must specify a region name, request schema, and response path. For more information, see the documentation about SageMaker endpoints deployment and about using magic commands with SageMaker endpoints.

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

自定义链#

您可以在本地变量中定义一个LangChain链,并将其作为魔法命令 %ai register 中的目标使用。

[14]:
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import Cohere

llm = Cohere(model="command", max_tokens=256, temperature=0.75)
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}? Provide only one name. Do not provide any other text than the name. Do not provide other info",
)
chain = LLMChain(llm=llm, prompt=prompt)

[15]:
# 仅指定输入变量运行链。
print(chain.invoke("colorful socks"))

{'product': 'colorful socks', 'text': ' FunkyHues'}
[16]:
%ai register company chain

[16]:

Registered new alias company

[17]:
%ai list

[17]:

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

bedrock

Not applicable.

N/A

bedrock-chat

Not applicable.

N/A

anthropic

ANTHROPIC_API_KEY

anthropic-chat

ANTHROPIC_API_KEY

azure-chat-openai

OPENAI_API_KEY

This provider does not define a list of models.

cohere

COHERE_API_KEY

gpt4all

Not applicable.

N/A

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

openai

OPENAI_API_KEY

openai-chat

OPENAI_API_KEY

qianfan

QIANFAN_AK, QIANFAN_SK

sagemaker-endpoint

Not applicable.

N/A

Specify an endpoint name as the model ID. In addition, you must specify a region name, request schema, and response path. For more information, see the documentation about SageMaker endpoints deployment and about using magic commands with SageMaker endpoints.

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

company

custom chain

[18]:
%%ai company
colorful socks

[18]:

Vox Socks

[19]:
%%ai company --format text
colorful socks

[19]:
 Spectra Socks
[ ]: