From 67a23fbaa2456bf25ac376a3e93139dae404f3b2 Mon Sep 17 00:00:00 2001 From: Wonder Date: Sun, 22 Mar 2026 21:25:36 +0800 Subject: [PATCH] =?UTF-8?q?=20=E2=99=BB=EF=B8=8FRefactor:=20=E8=A7=A3?= =?UTF-8?q?=E8=80=A6chatAgent=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/chatAgent.go | 24 ++++++++++++++++++++++++ main.go | 26 ++++++++++---------------- 2 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 agent/chatAgent.go diff --git a/agent/chatAgent.go b/agent/chatAgent.go new file mode 100644 index 0000000..b27aa25 --- /dev/null +++ b/agent/chatAgent.go @@ -0,0 +1,24 @@ +package agent + +import ( + "context" + "fmt" + "log" + + "github.com/tmc/langchaingo/llms" +) + +func Call(llm llms.Model, ctx context.Context) { + + response, err := llms.GenerateFromSinglePrompt( + ctx, + llm, + "Hello! My name is wonder!", + ) + if err != nil { + log.Fatal(err) + } + + fmt.Println("AI:", response) + +} diff --git a/main.go b/main.go index 6c8db24..a8e8a79 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "agent/agent" "context" "fmt" "log" @@ -15,6 +16,8 @@ var ( apiKey string baseUrl string model = "stepfun-ai/Step-3.5-Flash" + ctx context.Context + llm llms.Model ) func init() { @@ -26,10 +29,8 @@ func init() { baseUrl = os.Getenv("BASE_URL") fmt.Printf("API Key: %s\n", apiKey) fmt.Printf("Base URL: %s\n", baseUrl) -} -func main() { - llm, err := openai.New( + llm, err = openai.New( openai.WithBaseURL(baseUrl), openai.WithToken(apiKey), openai.WithModel(model), @@ -39,17 +40,10 @@ func main() { log.Fatal(err) } - ctx := context.Background() - - response, err := llms.GenerateFromSinglePrompt( - ctx, - llm, - "Hello! My name is wonder!", - ) - if err != nil { - log.Fatal(err) - } - - fmt.Println("AI:", response) - + ctx = context.Background() +} + +func main() { + + agent.Call(llm, ctx) }