diff --git a/cmd/wasm/main.go b/cmd/wasm/main.go index 6971ccf..6de9692 100644 --- a/cmd/wasm/main.go +++ b/cmd/wasm/main.go @@ -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 diff --git a/internal/api/client.go b/internal/api/client.go index d440029..0cc0b10 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -136,11 +136,6 @@ func NewTaxHealthService(userID, privateKey string) *TaxHealthService { // GetCompanyInfo 查询公司信息 func (s *CompanyInfoService) GetCompanyInfo(companyName string, apis []string) (*CompanyInfoResponse, error) { - // 生成随机数和时间戳 - r, _ := rand.Int(rand.Reader, big.NewInt(10000)) - nonce := strconv.FormatInt(r.Int64(), 10) - timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10) - // 构建查询参数 params := url.Values{} params.Set("name", companyName) @@ -148,20 +143,9 @@ func (s *CompanyInfoService) GetCompanyInfo(companyName string, apis []string) ( apiURL := "https://a.cmdp.cn/cmds/v1/company/info-rest/custom?" + params.Encode() - // 生成签名 - signData := s.userID + timestamp + nonce + apiURL - signature, err := s.generateSignature([]byte(signData)) - if err != nil { - return nil, fmt.Errorf("生成签名失败: %v", err) - } - // 设置请求头 headers := map[string]string{ - "Accept": "application/json", - "x-gi-user-id": s.userID, - "x-gi-user-sig": signature, - "x-gi-nonce": nonce, - "x-gi-timestamp": timestamp, + "Accept": "application/json", } // 发送GET请求 @@ -182,7 +166,8 @@ func (s *CompanyInfoService) GetCompanyInfo(companyName string, apis []string) ( // EvaluateTaxHealth 执行税务健康测评 func (s *TaxHealthService) EvaluateTaxHealth(request TaxEvalRequest) (*TaxEvalResponse, error) { // 生成随机数和时间戳 - nonce := "12345" + r, _ := rand.Int(rand.Reader, big.NewInt(10000)) + nonce := strconv.FormatInt(r.Int64(), 10) timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10) apiURL := "https://api.cmdp.cn/v1/eval/tax-risk" // 序列化请求体 @@ -246,8 +231,6 @@ func (s *TaxHealthService) QueryTaxEvalResult(orderNo string) (*TaxEvalQueryResp "x-gi-nonce": nonce, "x-gi-timestamp": timestamp, } - fmt.Println(headers) - fmt.Println(apiURL) // 发送GET请求 response, err := s.httpClient.Get(apiURL, headers) if err != nil { diff --git a/web/index.html b/web/index.html index 4267b60..86d5462 100644 --- a/web/index.html +++ b/web/index.html @@ -317,15 +317,16 @@ - + - + @@ -897,13 +898,13 @@ try { const config = JSON.parse(savedConfig); if (document.getElementById('llmProviderSelect')) { - document.getElementById('llmProviderSelect').value = config.provider || 'ollama'; + document.getElementById('llmProviderSelect').value = config.provider || 'default'; } if (document.getElementById('llmBaseUrlInput')) { - document.getElementById('llmBaseUrlInput').value = config.base_url || 'http://localhost:11434'; + document.getElementById('llmBaseUrlInput').value = config.base_url || ''; } if (document.getElementById('llmModelInput')) { - document.getElementById('llmModelInput').value = config.model || 'qwen3.5:9b'; + document.getElementById('llmModelInput').value = config.model || ''; } if (document.getElementById('llmApiKeyInput')) { document.getElementById('llmApiKeyInput').value = config.api_key || ''; @@ -932,9 +933,9 @@ // 返回默认配置 return { - provider: 'ollama', - base_url: 'http://localhost:11434', - model: 'qwen3.5:9b', + provider: 'default', + base_url: '', + model: '', api_key: '' }; }