diff --git a/agents/conversational.go b/agents/conversational.go index 1a493b7d..528a7a7b 100644 --- a/agents/conversational.go +++ b/agents/conversational.go @@ -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": "", + }, + } +} diff --git a/agents/conversational_prompt.go b/agents/conversational_prompt.go deleted file mode 100644 index d74334b3..00000000 --- a/agents/conversational_prompt.go +++ /dev/null @@ -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": "", - }, - } -} diff --git a/agents/prompts/conversational_format_instructions.txt b/agents/prompts/conversational_format_instructions.txt new file mode 100644 index 00000000..01b956da --- /dev/null +++ b/agents/prompts/conversational_format_instructions.txt @@ -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] diff --git a/agents/prompts/conversational_prefix.txt b/agents/prompts/conversational_prefix.txt new file mode 100644 index 00000000..46053949 --- /dev/null +++ b/agents/prompts/conversational_prefix.txt @@ -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}} \ No newline at end of file diff --git a/agents/prompts/conversational_suffix.txt b/agents/prompts/conversational_suffix.txt new file mode 100644 index 00000000..3756ad9c --- /dev/null +++ b/agents/prompts/conversational_suffix.txt @@ -0,0 +1,8 @@ +Begin! + +Previous conversation history: +{{.history}} + +New input: {{.input}} + +Thought:{{.agent_scratchpad}} \ No newline at end of file diff --git a/chains/api.go b/chains/api.go index ece99127..bebac6f0 100644 --- a/chains/api.go +++ b/chains/api.go @@ -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 { diff --git a/chains/llm_math.go b/chains/llm_math.go index f6cee7e6..aa8c0cf5 100644 --- a/chains/llm_math.go +++ b/chains/llm_math.go @@ -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) diff --git a/chains/prompts/llm_api_url.txt b/chains/prompts/llm_api_url.txt new file mode 100644 index 00000000..a5ccadf2 --- /dev/null +++ b/chains/prompts/llm_api_url.txt @@ -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] +} \ No newline at end of file diff --git a/chains/prompts/llm_api_url_response.txt b/chains/prompts/llm_api_url_response.txt new file mode 100644 index 00000000..796b8244 --- /dev/null +++ b/chains/prompts/llm_api_url_response.txt @@ -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: \ No newline at end of file diff --git a/chains/prompts/llm_math.txt b/chains/prompts/llm_math.txt new file mode 100644 index 00000000..0b32feb4 --- /dev/null +++ b/chains/prompts/llm_math.txt @@ -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}}