跳至内容

GitHub Copilot Chat

GenAIScript 与 GitHub Copilot Chat 集成, 通过提供一个聊天参与者功能,允许您在聊天对话的上下文中运行脚本, 以及一个自定义提示功能,以便更高效地通过 Copilot Chat 生成 GenAIScript。

genaiscript 自定义提示

GenAIScript会自动保存提示文件和其他文件到.genaiscript/docs目录中,以便为使用Copilot Chat生成GenAIScript脚本提供更好的上下文。

播放

genaiscript.prompt.md 文件是一个可复用的提示文件,用于为GitHub Copilot Chat提供额外上下文,帮助其回答GenAIScript代码生成相关的查询。

.genaiscript/prompts/genaiscript.prompt.md
## Role
You are an expert at the GenAIScript programming language (https://microsoft.github.io/genaiscript). Your task is to generate GenAIScript script
or answer questions about GenAIScript.
## Reference
- [GenAIScript docs](../../.genaiscript/docs/llms-full.txt)
- [GenAIScript ambient type definitions](../../.genaiscript/genaiscript.d.ts)
## Guidance for Code Generation
- you always generate TypeScript code using ESM models for Node.JS.
- you prefer using APIs from GenAIScript 'genaiscript.d.ts' rather node.js. Avoid node.js imports.
- you keep the code simple, avoid exception handlers or error checking.
- you add TODOs where you are unsure so that the user can review them
- you use the global types in genaiscript.d.ts are already loaded in the global context, no need to import them.
- save generated code in the `./genaisrc` folder with `.genai.mts` extension

要在聊天中使用此提示,

启用可复用提示

目前,可复用提示功能处于实验阶段,需要在设置中启用。

  1. 打开设置(Ctrl+,)并搜索GitHub Copilot Chat。 将chat.promptFiles设置设为true。

  2. .genaiscript/prompts添加到搜索提示文件的文件夹列表中。

  3. 打开命令面板 (Ctrl+Shift+P) 并选择 GitHub Copilot: 构建本地工作区索引 来创建 本地索引 以帮助处理大型文件的查询(比如 GenAIScript 文档!)。

增强型聊天会话

这是您如何使用genaiscript提示符开始聊天会话的方法。

  1. 选择附加上下文📎图标(Ctrl+/),然后选择提示..., 接着选择genaiscript提示。

  2. 包含编写脚本或回答关于GenAIScript问题的说明, write a script that summarizes a video(编写一个总结视频的脚本)。

由于提示词会注入GenAIScript的全部文档内容(撰写本文时已达700+KB),建议使用具有大上下文窗口的模型,如Sonnet或Gemini。

还需注意的是,每次迭代都会传回整个对话内容,因此这种技术最适合作为一次性详细请求使用。

@genaiscript 聊天参与者

@genaiscript chat participant 允许您在脱离GitHub Copilot Chat对话上下文的情况下运行脚本。 这对于在交互式聊天会话中利用现有脚本非常有用。

A screenshot of the chat participant window.

选择要运行的脚本

/run 命令期望第一个参数是脚本ID(例如 /run poem)。查询的其余部分会作为 env.vars.question 变量传递给脚本。

终端窗口
@genaiscript /run summarize

如果省略/run命令,GenAIScript会查找名为copilotchat的脚本。如果找到,就会运行该脚本。 否则,它会要求您从可用脚本列表中选择一个脚本。

终端窗口
@genaiscript add comments to the current editor

上下文

用户在Copilot Chat中选择的上下文被转换为变量并传递给脚本:

  • 提示内容通过env.vars.question传入。在使用/run的情况下,脚本ID会被移除。
  • 当前编辑器文本通过 env.vars["copilot.editor"] 传入
  • 当前编辑器选中的内容会传入 env.vars["copilot.selection"]
  • 所有其他文件引用都通过env.files传递

示例

  • mermaid 会根据用户提示生成图表。
mermaid.genai.mjs
def("CODE", env.files)
$`Generate a class diagram using mermaid of the code symbols in the CODE.`
  • websearcher 会根据用户提示在网络上搜索,并在回答中使用上下文中的文件。
websearcher.genai.mjs
const res = await retrieval.webSearch(env.vars.question)
def("QUESTION", env.vars.question)
def("WEB_SEARCH", res)
def("FILE", env.files, { ignoreEmpty: true })
$`Answer QUESTION using WEB_SEARCH and FILE.`
  • dataanalyst 使用Python代码解释器工具来解决数据计算问题。
dataanalyst.genai.mjs
script({
tools: [
"fs_read_file",
"python_code_interpreter_copy_files_to_container",
"python_code_interpreter_read_file",
"python_code_interpreter_run",
],
})
def("DATA", env.files.map(({ filename }) => filename).join("\n"))
def("QUESTION", env.vars.question)
$`Run python code to answer the data analyst question
in QUESTION using the data in DATA.
Return the python code that was used to compute the answer.
`

历史记录

消息历史记录通过env.vars["copilot.history"]传递。它是一个由HistoryMessageUser | HistoryMessageAssistant组成的数组:

[
{
"role": "user",
"content": "write a poem"
},
{
"role": "assistant",
"content": "I am an assistant"
}
]

持续对话

您可以使用@genaiscript聊天功能将脚本执行嵌入到现有对话中,或者根据脚本结果继续与Copilot对话。脚本的执行结果会被放回聊天历史记录,后续任何Copilot都可以使用这些结果。

  • @genaiscript /run tool 将运行 tool 脚本并将结果放回聊天历史记录中。
  • analyze the results 将会继续对话并展示脚本的执行结果。

默认脚本

以下脚本可以作为起始模板,在用户不使用/run命令时创建默认脚本。

genaisrc/copilotchat.genai.mts
script({
title: "Reasoning Agent",
description:
"A reasoning agent that can answer questions about files, git, github, documentation, web queries, video analysis.",
model: "large",
system: [
// List of system components and tools available for the script
"system",
"system.assistant",
"system.safety_harmful_content",
"system.safety_jailbreak",
"system.safety_protected_material",
"system.tools",
"system.files",
"system.files_schema",
"system.diagrams",
"system.annotations",
"system.git_info",
"system.github_info",
"system.safety_harmful_content",
"system.safety_validate_harmful_content",
"system.agent_fs",
"system.agent_git",
"system.agent_github",
"system.agent_interpreter",
"system.agent_docs",
"system.agent_web",
"system.agent_video",
"system.agent_data",
"system.vision_ask_images",
"system.think",
],
group: "mcp", // Group categorization for the script
parameters: {
question: {
type: "string",
description: "the user question",
},
"copilot.editor": {
type: "string",
description: "the content of the opened editor, if any",
default: "",
},
"copilot.selection": {
type: "string",
description: "the content of the opened editor, if any",
default: "",
},
},
flexTokens: 20000, // Flexible token limit for the script
})
// Extract the 'question' parameter from the environment variables
const { question } = env.vars
const editor = env.vars["copilot.editor"]
const selection = env.vars["copilot.selection"]
const history = env.vars["copilot.history"]
$`## Tasks
- make a plan to answer the QUESTION step by step
using the information in the Context section
- answer the QUESTION
## Output
- The final output will be inserted into the Visual Studio Code Copilot Chat window.
- do NOT include the plan in the output
## Guidance
- use the agent tools to help you
- do NOT be lazy, always finish the tasks
- do NOT skip any steps
`
// Define a variable QUESTION with the value of 'question'
def("QUESTION", question, {
lineNumbers: false,
detectPromptInjection: "available",
})
$`## Context`
// Define a variable FILE with the file data from the environment variables
// The { ignoreEmpty: true, flex: 1 } options specify to ignore empty files and to use flexible token allocation
if (history?.length > 0)
defData("HISTORY", history, { flex: 1, format: "yaml", sliceTail: 10 })
if (env.files.length)
def("FILE", env.files, {
lineNumbers: false,
ignoreEmpty: true,
flex: 1,
detectPromptInjection: "available",
})
if (editor)
def("EDITOR", editor, {
flex: 4,
ignoreEmpty: true,
detectPromptInjection: "available",
})
if (selection)
def("SELECTION", selection, {
flex: 5,
ignoreEmpty: true,
detectPromptInjection: "available",
})

不支持的功能

以下功能目前在聊天参与者中不受支持:

  • 工具 (#tool)
  • Workspace 参考