Use external tools
使用外部工具
Compensate for the weaknesses of GPTs by feeding them the outputs of other tools. For example, a text retrieval system can tell GPTs about relevant documents. A code execution engine can help GPTs do math and run code. If a task can be done more reliably or efficiently by a tool rather than by a GPT, offload it to get the best of both.
通过向 GPT 提供其他工具的输出来弥补 GPT 的弱点。例如,文本检索系统可以告诉 GPT 相关文档的信息,代码执行引擎可以帮助 GPT 进行数学计算和运行代码。如果一个任务可以通过工具而不是GPT更可靠或更高效地完成,那么可以将其转移,以充分发挥两者的优势。
Tactic: Use embeddings-based search to implement efficient knowledge retrieval
技巧:使用基于嵌入的搜索实现高效的知识检索
A model can leverage external sources of information if provided as part of its input. This can help the model to generate more informed and up-to-date responses. For example, if a user asks a question about a specific movie, it may be useful to add high quality information about the movie (e.g. actors, director, etc…) to the model’s input. Embeddings can be used to implement efficient knowledge retrieval, so that relevant information can be added to the model input dynamically at run-time.
如果模型的输入中包含外部信息,模型可以利用外部信息源,这可以帮助模型生成更具见解和最新的回答。例如,如果用户询问有关特定电影的问题,将关于电影的高质量信息(例如演员、导演等)添加到模型的输入中可能会很有用。可以使用嵌入来实现高效的知识检索,以便在运行时动态地将相关信息添加到模型输入中。
A text embedding is a vector that can measure the relatedness between text strings. Similar or relevant strings will be closer together than unrelated strings. This fact, along with the existence of fast vector search algorithms means that embeddings can be used to implement efficient knowledge retrieval. In particular, a text corpus can be split up into chunks, and each chunk can be embedded and stored. Then a given query can be embedded and vector search can be performed to find the embedded chunks of text from the corpus that are most related to the query (i.e. closest together in the embedding space).
文本嵌入是一种可以衡量文本字符串相关性的向量,相似或相关的字符串比不相关的字符串更接近。这个事实以及快速向量搜索算法的存在意味着嵌入可以用来实现高效的知识检索。特别是,可以将文本语料库分成多个块,对每个块进行嵌入和存储。然后可以对给定的查询进行嵌入,并进行向量搜索,以找到与查询最相关的文本块(即,在嵌入空间中最接近的文本块)。
Example implementations can be found in the OpenAI Cookbook. See the tactic “Instruct the model to use retrieved knowledge to answer queries” for an example of how to use knowledge retrieval to minimize the likelihood that a model will make up incorrect facts.
在 OpenAI Cookbook 中可以找到示例实现。有关如何使用知识检索来减少模型编造错误事实的示例,请参阅技巧文章“指导模型使用检索到的知识来回答查询”。
Tactic: Use code execution to perform more accurate calculations or call external APIs
技巧:使用代码执行进行更准确的计算或调用外部API
GPTs cannot be relied upon to perform arithmetic or long calculations accurately on their own. In cases where this is needed, a model can be instructed to write and run code instead of making its own calculations. In particular, a model can be instructed to put code that is meant to be run into a designated format such as triple backtics. After an output is produced, the code can be extracted and run. Finally, if necessary, the output from the code execution engine (i.e. Python interpreter) can be provided as an input to the model for the next query.
GPT 模型本身无法可靠地进行算术或长时间的计算。在需要这样做的情况下,可以指示模型编写并运行代码,而不是进行自己的计算。特别是,可以指示模型将需要运行的代码放入指定的格式中,例如三个反引号。生成输出后,可以提取和运行代码。最后,如果需要,可以将代码执行引擎(比如 Python 解释器)的输出作为下一个查询的模型输入。
SYSTEM 系统 | You can write and execute Python code by enclosing it in triple backticks, e.g. ```code goes here```. Use this to perform calculations. 您可以通过将其放在三个反引号中来编写和执行Python代码,例如code goes here。使用它来执行计算。 |
USER 用户 | Find all real-valued roots of the following polynomial: 3*x**5 - 5*x**4 - 3*x**3 - 7*x - 10. 找出以下多项式的所有实根:3x**5 - 5x4 - 3*x3 - 7*x - 10。 |
Another good use case for code execution is calling external APIs. If a model is instructed in the proper use of an API, it can write code that makes use of it. A model can be instructed in how to use an API by providing it with documentation and/or code samples showing how to use the API.
另一个使用代码执行的好用例是调用外部 API。如果正确指导模型使用 API,它可以编写使用 API 的代码。可以通过为模型提供文档和代码示例来指导模型如何使用 API。
SYSTEM 系统 | You can write and execute Python code by enclosing it in triple backticks. Also note that you have access to the following module to help users send messages to their friends: 您可以通过将其放在三个反引号中来编写和执行Python代码。还要注意,您可以使用以下模块来帮助用户向他们的朋友发送消息: ```python import message message.write(to="John", message="Hey, want to meetup after work?")``` |
WARNING: Executing code produced by a model is not inherently safe and precautions should be taken in any application that seeks to do this. In particular, a sandboxed code execution environment is needed to limit the harm that untrusted code could cause.
警告:执行模型生成的代码本质上是不安全的,因此在试图执行此操作的任何应用程序中应采取预防措施。特别是,需要使用沙盒式代码执行环境来限制不受信任的代码可能造成的伤害。
Tactic: Give the model access to specific functions
技巧:为模型提供特定函数的访问权限
The Chat Completions API allows passing a list of function descriptions in requests. This enables models to generate function arguments according to the provided schemas. Generated function arguments are returned by the API in JSON format and can be used to execute function calls. Output provided by function calls can then be fed back into a model in the following request to close the loop. This is the recommended way of using GPT models to call external functions. To learn more see the function calling section in our introductory GPT guide and more function calling examples in the OpenAI Cookbook.
Chat Completions API 允许在请求中传递函数描述列表,这使得模型可以根据提供的模式生成函数参数。API 以 JSON 格式返回生成的函数参数,并可用于执行函数调用。然后,函数调用提供的输出可以作为下一个请求的模型输入,以完成循环。这是使用 GPT 模型调用外部函数的推荐方法。要了解更多信息,请参阅我们的入门指南中的函数调用部分以及 OpenAI Cookbook 中的更多函数调用示例。
“点赞有美意,赞赏是鼓励”