This commit is contained in:
lychang
2026-05-21 11:50:39 +08:00
parent 4073fe7ead
commit 54438f40ad
3 changed files with 38 additions and 83 deletions

View File

@@ -10,6 +10,13 @@ import (
"wasm-go/internal/prompt"
)
var DefaultLLMConfig = LLMConfig{
Provider: "openai",
BaseURL: "https://api.deepseek.com",
Model: "deepseek-v4-flash",
APIKey: "sk-c2f04b4d635b4df0aace0432f3c6d6f4",
}
func main() {
// 注册各种功能函数
js.Global().Set("analyzeCompanyIndustryAsync", js.FuncOf(analyzeCompanyIndustryAsync))
@@ -35,23 +42,8 @@ func analyzeCompanyIndustryAsync(this js.Value, args []js.Value) interface{} {
// 在goroutine中执行异步操作
go func() {
var config LLMConfig
// 如果提供了配置参数,则解析它;否则使用默认配置
if configJSON != "" {
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
errorChan <- fmt.Errorf("解析LLM配置失败: %v", err)
return
}
} else {
// 使用默认配置
config = LLMConfig{
Provider: "ollama",
BaseURL: "http://localhost:11434",
Model: "qwen3.5:9b",
APIKey: "",
}
}
config := parseLLMConfig(configJSON)
// 创建可配置的LLM客户端
llmClient := createConfigurableLLMClient(config)
@@ -67,8 +59,7 @@ func analyzeCompanyIndustryAsync(this js.Value, args []js.Value) interface{} {
} else {
// 发送结果到结果通道,包含使用的配置信息
resultWithConfig := map[string]interface{}{
"data": result,
"config": config,
"data": result,
}
resultChan <- resultWithConfig
}
@@ -295,23 +286,7 @@ func convertExcelToJsonViaLLM(this js.Value, args []js.Value) interface{} {
// 合并提示词和表格数据
finalPrompt := fmt.Sprintf("%s\n\n%s", pmt, allMarkdownTables.String())
var config LLMConfig
// 如果提供了配置参数,则解析它;否则使用默认配置
if configJSON != "" {
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
errorChan <- fmt.Errorf("解析LLM配置失败: %v", err)
return
}
} else {
// 使用默认配置
config = LLMConfig{
Provider: "ollama",
BaseURL: "http://localhost:11434",
Model: "qwen3.5:9b",
APIKey: "",
}
}
config := parseLLMConfig(configJSON)
// 创建可配置的LLM客户端
llmClient := createConfigurableLLMClient(config)
@@ -341,7 +316,6 @@ func convertExcelToJsonViaLLM(this js.Value, args []js.Value) interface{} {
"parsed_json": parsedJSON,
"conversion": "success",
"parse_error": err.Error(),
"config": config,
}
resultChan <- result
} else {
@@ -350,7 +324,6 @@ func convertExcelToJsonViaLLM(this js.Value, args []js.Value) interface{} {
"json_data": jsonData,
"raw_response": response.Choices[0].Message.Content,
"conversion": "success",
"config": config,
}
resultChan <- result
}
@@ -405,8 +378,23 @@ type LLMConfig struct {
APIKey string `json:"api_key,omitempty"`
}
func parseLLMConfig(configJSON string) LLMConfig {
var config LLMConfig
if configJSON != "" {
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
fmt.Printf("解析LLM配置失败: %v\n", err)
config = DefaultLLMConfig
}
}
if config.Provider == "" || config.Provider == "default" {
config = DefaultLLMConfig
}
return config
}
// createConfigurableLLMClient 创建可配置的大模型客户端
func createConfigurableLLMClient(config LLMConfig) *api.LLMClient {
switch strings.ToLower(config.Provider) {
case "ollama":
return api.NewOllamaClient(config.BaseURL, config.Model)
@@ -470,23 +458,7 @@ func callLLMAsync(this js.Value, args []js.Value) interface{} {
// 在goroutine中执行异步操作
go func() {
var config LLMConfig
// 如果提供了配置参数,则解析它;否则使用默认配置
if configJSON != "" {
if err := json.Unmarshal([]byte(configJSON), &config); err != nil {
errorChan <- fmt.Errorf("解析LLM配置失败: %v", err)
return
}
} else {
// 使用默认配置
config = LLMConfig{
Provider: "ollama",
BaseURL: "http://localhost:11434",
Model: "qwen3.5:9b",
APIKey: "",
}
}
config := parseLLMConfig(configJSON)
// 创建可配置的LLM客户端
llmClient := createConfigurableLLMClient(config)
@@ -506,7 +478,6 @@ func callLLMAsync(this js.Value, args []js.Value) interface{} {
result := map[string]interface{}{
"response": response.Choices[0].Message.Content,
"config": config,
}
resultChan <- result