func main() {
llm, err := ollama.New(
ollama.WithModel("qwen2.5:14b"),
ollama.WithFormat("json"),
ollama.WithHTTPClient(httpClient),
)
if err != nil {
panic(err)
}
content := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, "What is the weather like in Chicago?"),
}
generateContent, err := llm.GenerateContent(context.Background(), content, llms.WithTools(llmTools))
if err != nil {
panic(err)
}
println(generateContent.Choices[0].Content)
AgentsTest(llm)
}
func AgentsTest(llm llms.Model) {
agent := agents.NewOneShotAgent(llm, agentTools, agents.WithMaxIterations(1))
executor := agents.NewExecutor(agent)
input := "这段话的中文意思是什么:hello world"
answer, err := chains.Run(context.Background(), executor, input)
if err != nil {
panic(err)
}
fmt.Println(answer)
}
var agentTools = []tools.Tool{
&TranslateTools{},
}
var llmTools = []llms.Tool{
{
Type: "function",
Function: &llms.FunctionDefinition{
Name: "getCurrentWeather",
Description: "Get the current weather in a given location",
Parameters: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]any{
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": []string{"location"},
},
},
},
}
var _ tools.Tool = (*TranslateTools)(nil)
type TranslateTools struct {
}
func (t TranslateTools) Name() string {
return "translation_content"
}
func (t TranslateTools) Description() string {
return "翻译用户输入的content从source_language到target_language"
}
func (t TranslateTools) Call(ctx context.Context, input string) (string, error) {
fmt.Println("input:", input)
return input, nil
}
response error
{
panic: unable to parse agent output:
goroutine 1 [running]:
main.AgentsTest({0xee41a8?, 0xc0000fec00?})
D:/code/golang/langchaingo_demo/main.go:60 +0x11f
main.main()
D:/code/golang/langchaingo_demo/main.go:51 +0x3a5
exit status 2
response error