promps: move prompts to embedded text files (#451)

This commit is contained in:
chyroc
2023-12-29 08:55:43 +08:00
committed by GitHub
parent a39989164e
commit 310afd2c66
10 changed files with 112 additions and 113 deletions
+26
View File
@@ -2,6 +2,7 @@ package agents
import (
"context"
_ "embed"
"fmt"
"regexp"
"strings"
@@ -9,6 +10,7 @@ import (
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/prompts"
"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/tools"
)
@@ -148,3 +150,27 @@ func (a *ConversationalAgent) parseOutput(output string) ([]schema.AgentAction,
{Tool: strings.TrimSpace(matches[1]), ToolInput: strings.TrimSpace(matches[2]), Log: output},
}, nil, nil
}
//go:embed prompts/conversational_prefix.txt
var _defaultConversationalPrefix string //nolint:gochecknoglobals
//go:embed prompts/conversational_format_instructions.txt
var _defaultConversationalFormatInstructions string //nolint:gochecknoglobals
//go:embed prompts/conversational_suffix.txt
var _defaultConversationalSuffix string //nolint:gochecknoglobals
func createConversationalPrompt(tools []tools.Tool, prefix, instructions, suffix string) prompts.PromptTemplate {
template := strings.Join([]string{prefix, instructions, suffix}, "\n\n")
return prompts.PromptTemplate{
Template: template,
TemplateFormat: prompts.TemplateFormatGoTemplate,
InputVariables: []string{"input", "agent_scratchpad"},
PartialVariables: map[string]any{
"tool_names": toolNames(tools),
"tool_descriptions": toolDescriptions(tools),
"history": "",
},
}
}
-61
View File
@@ -1,61 +0,0 @@
//nolint:all
package agents
import (
"strings"
"github.com/tmc/langchaingo/prompts"
"github.com/tmc/langchaingo/tools"
)
const _defaultConversationalPrefix = `Assistant is a large language model trained by Meta.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
TOOLS:
------
Assistant has access to the following tools:
{{.tool_descriptions}}`
const _defaultConversationalFormatInstructions = `To use a tool, please use the following format:
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{{.tool_names}}]
Action Input: the input to the action
Observation: the result of the action
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
Thought: Do I need to use a tool? No
AI: [your response here]
`
const _defaultConversationalSuffix = `Begin!
Previous conversation history:
{{.history}}
New input: {{.input}}
Thought:{{.agent_scratchpad}}`
func createConversationalPrompt(tools []tools.Tool, prefix, instructions, suffix string) prompts.PromptTemplate {
template := strings.Join([]string{prefix, instructions, suffix}, "\n\n")
return prompts.PromptTemplate{
Template: template,
TemplateFormat: prompts.TemplateFormatGoTemplate,
InputVariables: []string{"input", "agent_scratchpad"},
PartialVariables: map[string]any{
"tool_names": toolNames(tools),
"tool_descriptions": toolDescriptions(tools),
"history": "",
},
}
}
@@ -0,0 +1,11 @@
To use a tool, please use the following format:
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{{.tool_names}}]
Action Input: the input to the action
Observation: the result of the action
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
Thought: Do I need to use a tool? No
AI: [your response here]
+14
View File
@@ -0,0 +1,14 @@
Assistant is a large language model trained by Meta.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
TOOLS:
------
Assistant has access to the following tools:
{{.tool_descriptions}}
+8
View File
@@ -0,0 +1,8 @@
Begin!
Previous conversation history:
{{.history}}
New input: {{.input}}
Thought:{{.agent_scratchpad}}
+6 -30
View File
@@ -3,6 +3,7 @@ package chains
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
@@ -15,38 +16,13 @@ import (
"github.com/tmc/langchaingo/schema"
)
const (
// nolint: lll
_llmAPIURLPrompt = `
You are given the API Documentation:
//go:embed prompts/llm_api_url.txt
var _llmAPIURLPrompt string //nolint:gochecknoglobals
{{.api_docs}}
//go:embed prompts/llm_api_url_response.txt
var _llmAPIURLResponseTmpPrompt string //nolint:gochecknoglobals
Your task is to construct a full API JSON object based on the provided input. The input could be a question that requires an API call for its answer, or a direct or indirect instruction to consume an API. The input will be unpredictable and could come from a user or an agent.
Your goal is to create an API call that accurately reflects the intent of the input. Be sure to exclude any unnecessary data in the API call to ensure efficiency.
Input: {{.input}}
Respond with a JSON object.
{
"method": [the HTTP method for the API call, such as GET or POST],
"headers": [object representing the HTTP headers required for the API call, always add a "Content-Type" header],
"url": [full for the API call],
"body": [object containing the data sent with the request, if needed]
}`
// nolint: lll
_llmAPIResponsePrompt = _llmAPIURLPrompt + `
Here is the response from the API:
{{.api_response}}
Now, summarize this response. Your summary should reflect the original input and highlight the key information from the API response that answers or relates to that input. Try to make your summary concise, yet informative.
Summary:`
)
var _llmAPIResponsePrompt = _llmAPIURLPrompt + _llmAPIURLResponseTmpPrompt //nolint:gochecknoglobals
// HTTPRequest http requester interface.
type HTTPRequest interface {
+4 -22
View File
@@ -2,6 +2,7 @@ package chains
import (
"context"
_ "embed"
"fmt"
"regexp"
"strings"
@@ -14,27 +15,8 @@ import (
"go.starlark.net/starlark"
)
const (
quote = "```"
_llmMathPrompt = `Translate a math problem into a expression that can be evaluated as Starlark.
Use the output of running this code to answer the question.
---
Question: (Question with math problem.)
` + quote + `starlark
$(single line expression that solves the problem)
` + quote + `
---
Question: What is 37593 * 67?
` + quote + `starlark
37593 * 67
` + quote + `
---
Question: {{.question}}
`
)
//go:embed prompts/llm_math.txt
var _llmMathPrompt string //nolint:gochecknoglobals
// LLMMathChain is a chain used for evaluating math expressions.
type LLMMathChain struct {
@@ -53,7 +35,7 @@ func NewLLMMathChain(llm llms.LanguageModel) LLMMathChain {
// Call gets relevant documents from the retriever and gives them to the combine
// documents chain.
func (c LLMMathChain) Call(ctx context.Context, values map[string]any, options ...ChainCallOption) (map[string]any, error) { //nolint: lll
func (c LLMMathChain) Call(ctx context.Context, values map[string]any, options ...ChainCallOption) (map[string]any, error) { // nolint: lll
question, ok := values["question"].(string)
if !ok {
return nil, fmt.Errorf("%w: %w", ErrInvalidInputValues, ErrInputValuesWrongType)
+18
View File
@@ -0,0 +1,18 @@
You are given the API Documentation:
{{.api_docs}}
Your task is to construct a full API JSON object based on the provided input. The input could be a question that requires an API call for its answer, or a direct or indirect instruction to consume an API. The input will be unpredictable and could come from a user or an agent.
Your goal is to create an API call that accurately reflects the intent of the input. Be sure to exclude any unnecessary data in the API call to ensure efficiency.
Input: {{.input}}
Respond with a JSON object.
{
"method": [the HTTP method for the API call, such as GET or POST],
"headers": [object representing the HTTP headers required for the API call, always add a "Content-Type" header],
"url": [full for the API call],
"body": [object containing the data sent with the request, if needed]
}
+8
View File
@@ -0,0 +1,8 @@
Here is the response from the API:
{{.api_response}}
Now, summarize this response. Your summary should reflect the original input and highlight the key information from the API response that answers or relates to that input. Try to make your summary concise, yet informative.
Summary:
+17
View File
@@ -0,0 +1,17 @@
Translate a math problem into a expression that can be evaluated as Starlark.
Use the output of running this code to answer the question.
---
Question: (Question with math problem.)
```starlark
$(single line expression that solves the problem)
```
---
Question: What is 37593 * 67?
```starlark
37593 * 67
```
---
Question: {{.question}}