package main
import (
"context"
"encoding/json"
"fmt"
"time"
"vikat.local/core"
"vikat.local/core/schemas"
"vikat.local/plugins/jsonparser"
)
func main() {
// Create JSON parser plugin
jsonPlugin := jsonparser.NewJsonParserPlugin(jsonparser.PluginConfig{
Usage: jsonparser.AllRequests,
CleanupInterval: 2 * time.Minute,
MaxAge: 10 * time.Minute,
})
// Initialize Vikat with the plugin
client, err := vikat.Init(context.Background(), schemas.VikatConfig{
Account: &MyAccount{},
LLMPlugins: []schemas.LLMPlugin{jsonPlugin},
})
if err != nil {
panic(err)
}
defer client.Shutdown()
// Request structured JSON response
request := &schemas.VikatChatRequest{
Provider: schemas.OpenAI,
Model: "gpt-4o-mini",
Input: []schemas.ChatMessage{
{
Role: schemas.ChatMessageRoleUser,
Content: schemas.ChatMessageContent{
ContentStr: vikat.Ptr("Return user profile as JSON: {\"name\": \"John Doe\", \"email\": \"john@example.com\"}"),
},
},
},
}
// Stream the response
stream, vikatErr := client.ChatCompletionStreamRequest(schemas.NewVikatContext(context.Background(), schemas.NoDeadline), request)
if vikatErr != nil {
panic(vikatErr)
}
fmt.Println("Streaming JSON response:")
for chunk := range stream {
if chunk.VikatChatResponse != nil && len(chunk.VikatChatResponse.Choices) > 0 {
choice := chunk.VikatChatResponse.Choices[0]
if choice.ChatStreamResponseChoice != nil && choice.ChatStreamResponseChoice.Delta != nil {
content := *choice.ChatStreamResponseChoice.Delta.Content
fmt.Printf("Chunk: %s\n", content)
// With JSON parser, you can parse each chunk immediately
var jsonData map[string]interface{}
if err := json.Unmarshal([]byte(content), &jsonData); err == nil {
fmt.Printf("✅ Valid JSON parsed successfully\n")
} else {
fmt.Printf("❌ Invalid JSON: %v\n", err)
}
}
}
}
}