1 line
8.3 KiB
JSON
1 line
8.3 KiB
JSON
{"repo": "chriscow/minds", "files": {"_examples/chat-structured-output/main.go": {"5c3cc5abb9331082872b200e6fd5b6e61a044941": {"t": 1785745622.2806969, "n": 2838, "h": "ce6bcadfbb3d0693", "c": "package main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/chriscow/minds\"\n\t\"github.com/chriscow/minds/providers/gemini\"\n\t\"github.com/chriscow/minds/providers/openai\"\n\t\"github.com/fatih/color\"\n)\n\nconst prompt = \"Generate sample data for a person in JSON format\"\n\nvar (\n\tcyan = color.New(color.FgCyan).SprintFunc()\n\tgreen = color.New(color.FgGreen).SprintFunc()\n\tpurple = color.New(color.FgHiMagenta).SprintFunc()\n)\n\ntype SampleData struct {\n\tName string `json:\"name\"`\n\tAge int `json:\"age\"`\n}\n\n// This example demonstrates how to generate structured output from an LLM.\n// The structured output is defined by the SampleData struct above.\n// The example uses two different LLM providers, Gemini and OpenAI, to generate the same output.\nfunc main() {\n\tctx := context.Background()\n\treq := minds.Request{Messages: minds.Messages{{Role: minds.RoleUser, Content: prompt}}}\n\twithGemini(ctx, req)\n\twithOpenAI(ctx, req)\n\twithDeepSeek(ctx, req)\n}\n\nfunc withGemini(ctx context.Context, req minds.Request) {\n\tschema, _ := gemini.GenerateSchema(SampleData{})\n\tllm, err := gemini.NewProvider(ctx, gemini.WithResponseSchema(schema))\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", cyan(\"Gemini\"), err)\n\t\treturn\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", cyan(\"Gemini\"), err)\n\t\treturn\n\t}\n\tprintResult(cyan(\"Gemini\"), resp)\n}\n\nfunc withOpenAI(ctx context.Context, req minds.Request) {\n\tschema, _ := minds.NewResponseSchema(\"SampleData\", \"some sample data\", SampleData{})\n\tllm, err := openai.NewProvider(openai.WithResponseSchema(*schema))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", green(\"OpenAI\"), err)\n\t\treturn\n\t}\n\n\tprintResult(green(\"OpenAI\"), resp)\n}\n\nfunc withDeepSeek(ctx context.Context, req minds.Request) {\n\tyellow := color.New(color.FgYellow).SprintFunc()\n\tfmt.Printf(\"[%s] %s\\n\", purple(\"DeepSeek\"), yellow(\"WARNING: DeepSeek does not support structured output in this example\"))\n\n\tbaseURl := \"https://api.deepseek.com\"\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tmodel := \"deepseek-chat\"\n\tschema, _ := minds.NewResponseSchema(\"SampleData\", \"some sample data\", SampleData{})\n\n\tllm, err := openai.NewProvider(\n\t\topenai.WithAPIKey(apiKey),\n\t\topenai.WithModel(model),\n\t\topenai.WithBaseURL(baseURl),\n\t\topenai.WithResponseSchema(*schema),\n\t)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", purple(\"DeepSeek\"), err)\n\t\treturn\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", purple(\"DeepSeek\"), err)\n\t\treturn\n\t}\n\n\tprintResult(\"DeepSeek\", resp)\n}\n\nfunc printResult(name string, resp minds.Response) {\n\tvar result SampleData\n\tjson.Unmarshal([]byte(resp.String()), &result)\n\n\tfmt.Printf(\"[%s] Name: %s, Age: %d\\n\", name, result.Name, result.Age)\n}\n"}, "3d29be4b2bd57e57142263f55d6bad9008b5d2ce": {"t": 1785754944.8911881, "n": 2838, "h": "ce6bcadfbb3d0693", "c": "package main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/chriscow/minds\"\n\t\"github.com/chriscow/minds/providers/gemini\"\n\t\"github.com/chriscow/minds/providers/openai\"\n\t\"github.com/fatih/color\"\n)\n\nconst prompt = \"Generate sample data for a person in JSON format\"\n\nvar (\n\tcyan = color.New(color.FgCyan).SprintFunc()\n\tgreen = color.New(color.FgGreen).SprintFunc()\n\tpurple = color.New(color.FgHiMagenta).SprintFunc()\n)\n\ntype SampleData struct {\n\tName string `json:\"name\"`\n\tAge int `json:\"age\"`\n}\n\n// This example demonstrates how to generate structured output from an LLM.\n// The structured output is defined by the SampleData struct above.\n// The example uses two different LLM providers, Gemini and OpenAI, to generate the same output.\nfunc main() {\n\tctx := context.Background()\n\treq := minds.Request{Messages: minds.Messages{{Role: minds.RoleUser, Content: prompt}}}\n\twithGemini(ctx, req)\n\twithOpenAI(ctx, req)\n\twithDeepSeek(ctx, req)\n}\n\nfunc withGemini(ctx context.Context, req minds.Request) {\n\tschema, _ := gemini.GenerateSchema(SampleData{})\n\tllm, err := gemini.NewProvider(ctx, gemini.WithResponseSchema(schema))\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", cyan(\"Gemini\"), err)\n\t\treturn\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", cyan(\"Gemini\"), err)\n\t\treturn\n\t}\n\tprintResult(cyan(\"Gemini\"), resp)\n}\n\nfunc withOpenAI(ctx context.Context, req minds.Request) {\n\tschema, _ := minds.NewResponseSchema(\"SampleData\", \"some sample data\", SampleData{})\n\tllm, err := openai.NewProvider(openai.WithResponseSchema(*schema))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", green(\"OpenAI\"), err)\n\t\treturn\n\t}\n\n\tprintResult(green(\"OpenAI\"), resp)\n}\n\nfunc withDeepSeek(ctx context.Context, req minds.Request) {\n\tyellow := color.New(color.FgYellow).SprintFunc()\n\tfmt.Printf(\"[%s] %s\\n\", purple(\"DeepSeek\"), yellow(\"WARNING: DeepSeek does not support structured output in this example\"))\n\n\tbaseURl := \"https://api.deepseek.com\"\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tmodel := \"deepseek-chat\"\n\tschema, _ := minds.NewResponseSchema(\"SampleData\", \"some sample data\", SampleData{})\n\n\tllm, err := openai.NewProvider(\n\t\topenai.WithAPIKey(apiKey),\n\t\topenai.WithModel(model),\n\t\topenai.WithBaseURL(baseURl),\n\t\topenai.WithResponseSchema(*schema),\n\t)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", purple(\"DeepSeek\"), err)\n\t\treturn\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tfmt.Printf(\"[%s] error: %v\", purple(\"DeepSeek\"), err)\n\t\treturn\n\t}\n\n\tprintResult(\"DeepSeek\", resp)\n}\n\nfunc printResult(name string, resp minds.Response) {\n\tvar result SampleData\n\tjson.Unmarshal([]byte(resp.String()), &result)\n\n\tfmt.Printf(\"[%s] Name: %s, Age: %d\\n\", name, result.Name, result.Age)\n}\n"}, "2e38b7cb8833ab674a9a26dc2d5e2de01ee06575": {"t": 1785754946.4376864, "n": 1704, "h": "2833637c2301b2be", "c": "package main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/chriscow/minds\"\n\t\"github.com/chriscow/minds/providers/gemini\"\n\t\"github.com/chriscow/minds/providers/openai\"\n)\n\nconst prompt = \"Generate sample data for a person in JSON format\"\n\ntype SampleData struct {\n\tName string `json:\"name\"`\n\tAge int `json:\"age\"`\n}\n\n// This example demonstrates how to generate structured output from an LLM.\n// The structured output is defined by the SampleData struct above.\n// The example uses two different LLM providers, Gemini and OpenAI, to generate the same output.\nfunc main() {\n\tctx := context.Background()\n\n\tresp := withGemini(ctx)\n\tprintResult(resp)\n\n\tresp = withOpenAI(ctx)\n\tprintResult(resp)\n}\n\nfunc withGemini(ctx context.Context) minds.Response {\n\tschema, _ := gemini.GenerateSchema(SampleData{})\n\tllm, err := gemini.NewProvider(ctx, gemini.WithResponseSchema(schema))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treq := minds.Request{\n\t\tMessages: minds.Messages{\n\t\t\t{\n\t\t\t\tContent: prompt,\n\t\t\t},\n\t\t},\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn resp\n}\n\nfunc withOpenAI(ctx context.Context) minds.Response {\n\tschema, _ := minds.NewResponseSchema(\"SampleData\", \"some sample data\", SampleData{})\n\tllm, err := openai.NewProvider(openai.WithResponseSchema(*schema))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treq := minds.Request{\n\t\tMessages: minds.Messages{\n\t\t\t{\n\t\t\t\tContent: prompt,\n\t\t\t},\n\t\t},\n\t}\n\n\tresp, err := llm.GenerateContent(ctx, req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn resp\n}\n\nfunc printResult(resp minds.Response) {\n\tvar result SampleData\n\tjson.Unmarshal([]byte(resp.String()), &result)\n\n\tfmt.Printf(\"Name: %s, Age: %d\\n\", result.Name, result.Age)\n}\n"}}}} |