diff --git a/backend/agent/basicAgent.go b/backend/agent/basicAgent.go index d3ed2f6..b69a749 100644 --- a/backend/agent/basicAgent.go +++ b/backend/agent/basicAgent.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/tmc/langchaingo/llms" "github.com/tmc/langchaingo/outputparser" @@ -98,10 +99,20 @@ func CallWithAgentDefined[T any](llm llms.Model, ctx context.Context, topic stri llm, "用户正在学习Go语言和React框架,你是用户的助手。\n"+prompt, ) + + + if err != nil { log.Fatal(err) } + + if !strings.Contains(responseString, "```json") { + responseString = "```json\n" + responseString + "```" + } + + fmt.Printf("AI: %s\n\n",responseString) + response, err = parse.Parse(responseString) if err != nil { fmt.Printf("Parse err: %v", err) diff --git a/backend/prompt/guodegang.txt b/backend/prompt/guodegang.txt index d52982d..b3c6bb4 100644 --- a/backend/prompt/guodegang.txt +++ b/backend/prompt/guodegang.txt @@ -4,7 +4,6 @@ 3. 语气轻松诙谐、接地气,但内容要准确专业 4. 偶尔使用相声常用语,比如"您听好了"、"这就好比"、"想当年"、"您猜怎么着" 5. 每讲完一个要点,可以用"这话怎么说呢?"来过渡 -6. 最后用"您记住了吗?下次要是有人问起来,您就把这么一通摆出来,准保让他对您刮目相看!"收尾 注意: - 不要过度夸张,保持内容的准确性和专业性 diff --git a/backend/utils/tool.go b/backend/utils/tool.go new file mode 100644 index 0000000..345d4d9 --- /dev/null +++ b/backend/utils/tool.go @@ -0,0 +1,26 @@ +package utils + +import ( + "encoding/json" + "fmt" + + "github.com/tmc/langchaingo/outputparser" +) + +// Parse parses the output of an LLM call. +func (p outputparser.Defined[T)(T) Parse(text string) (T, error) { + var target T + + // Removes '```json' and '```' from the start and end of the text. + const opening = "```json" + const closing = "```" + if text[:len(opening)] != opening || text[len(text)-len(closing):] != closing { + return target, nil + // return target, fmt.Errorf("input text should start with %s and end with %s", opening, closing) + } + parseableJSON := text[len(opening) : len(text)-len(closing)] + if err := json.Unmarshal([]byte(parseableJSON), &target); err != nil { + return target, fmt.Errorf("could not parse generated JSON: %w", err) + } + return target, nil +} \ No newline at end of file diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 42d48da..c1436ad 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,14 +1,16 @@ import Card from "./components/Card"; +import Guodegang from "./components/Guodegang"; import { Surface } from "@heroui/react"; function App() { return ( <> + ); diff --git a/frontend/src/components/Guodegang.tsx b/frontend/src/components/Guodegang.tsx new file mode 100644 index 0000000..4a8d9fe --- /dev/null +++ b/frontend/src/components/Guodegang.tsx @@ -0,0 +1,84 @@ +import { Card } from "@heroui/react"; +import { useEffect, useState } from "react"; + +interface GuodegangData { + name: string; + content: string; +} + +interface Props { + topic: string; +} + +export default function Guodegang({ topic }: Props) { + const [data, setData] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchData = async () => { + try { + const response = await fetch( + `http://localhost:8080/guodegang/${encodeURIComponent(topic)}`, + ); + if (!response.ok) { + throw new Error("API Error"); + } + const result = await response.json(); + setData(result.message); + setError(null); + } catch (error) { + console.error("Failed to fetch data: ", error); + setError("Failed to load data. Please try again."); + } finally { + setLoading(false); + } + }; + fetchData(); + }, [topic]); + + if (loading) { + return ( +
+ +
Loading...
+
+
+ ); + } + + if (error) { + return ( +
+ +
{error}
+
+
+ ); + } + + return ( +
+ +
+ 🎤 +
+ +
+
+ + {data!.name} + +
+
+ + +

+ {data!.content} +

+
+
+
+
+ ); +}