Files

1 line
4.2 KiB
JSON

{"repo": "1mizhgun1/purify", "files": {"src/ai/deepseek/request.go": {"176223e3a1f94bcfe76952cf4d640f056703288c": {"t": 1785745651.3179202, "n": 1780, "h": "214dab8db21b6780", "c": "package deepseek\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/pkg/errors\"\n\t\"purify/src/config\"\n)\n\nvar client = http.DefaultClient\n\nfunc SendRequest(prompt string, cfg config.AIConfig) (string, error) {\n\turl := fmt.Sprintf(\"%s%s\", cfg.BaseURL, cfg.CompletionsURL)\n\trequestBody := makeRequestData(cfg.Model, prompt)\n\n\treq, err := http.NewRequest(http.MethodPost, url, strings.NewReader(requestBody))\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to create request\")\n\t}\n\n\treq.Header.Set(\"Authorization\", fmt.Sprintf(\"Bearer %s\", os.Getenv(\"DEEPSEEK_API_KEY\")))\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to send request\")\n\t}\n\n\trespBody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to read response body\")\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn \"\", errors.Errorf(\"status code: %d, respBody: %s\", resp.StatusCode, string(respBody))\n\t}\n\n\tvar result respChoices\n\tif err = json.Unmarshal(respBody, &result); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to unmarshal response body\")\n\t}\n\n\tfor _, choice := range result.Choices {\n\t\tif choice.Index == 0 {\n\t\t\treturn choice.Message.Content, nil\n\t\t}\n\t}\n\n\treturn \"\", errors.Errorf(\"no answer found in response\")\n}\n\nfunc makeRequestData(model string, prompt string) string {\n\treturn fmt.Sprintf(`{\"model\":\"%s\",\"messages\":[{\"role\":\"user\",\"content\":\"%s\"}],\"stream\":false}`, model, prompt)\n}\n\ntype respChoices struct {\n\tChoices []respChoice `json:\"choices\"`\n}\n\ntype respChoice struct {\n\tIndex int `json:\"index\"`\n\tMessage respMessage `json:\"message\"`\n}\n\ntype respMessage struct {\n\tContent string `json:\"content\"`\n}\n"}, "22ba7963c2b9be3fcb4a8b948e874ea62e43dd17": {"t": 1785755376.267096, "n": 1780, "h": "214dab8db21b6780", "c": "package deepseek\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/pkg/errors\"\n\t\"purify/src/config\"\n)\n\nvar client = http.DefaultClient\n\nfunc SendRequest(prompt string, cfg config.AIConfig) (string, error) {\n\turl := fmt.Sprintf(\"%s%s\", cfg.BaseURL, cfg.CompletionsURL)\n\trequestBody := makeRequestData(cfg.Model, prompt)\n\n\treq, err := http.NewRequest(http.MethodPost, url, strings.NewReader(requestBody))\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to create request\")\n\t}\n\n\treq.Header.Set(\"Authorization\", fmt.Sprintf(\"Bearer %s\", os.Getenv(\"DEEPSEEK_API_KEY\")))\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to send request\")\n\t}\n\n\trespBody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to read response body\")\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn \"\", errors.Errorf(\"status code: %d, respBody: %s\", resp.StatusCode, string(respBody))\n\t}\n\n\tvar result respChoices\n\tif err = json.Unmarshal(respBody, &result); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to unmarshal response body\")\n\t}\n\n\tfor _, choice := range result.Choices {\n\t\tif choice.Index == 0 {\n\t\t\treturn choice.Message.Content, nil\n\t\t}\n\t}\n\n\treturn \"\", errors.Errorf(\"no answer found in response\")\n}\n\nfunc makeRequestData(model string, prompt string) string {\n\treturn fmt.Sprintf(`{\"model\":\"%s\",\"messages\":[{\"role\":\"user\",\"content\":\"%s\"}],\"stream\":false}`, model, prompt)\n}\n\ntype respChoices struct {\n\tChoices []respChoice `json:\"choices\"`\n}\n\ntype respChoice struct {\n\tIndex int `json:\"index\"`\n\tMessage respMessage `json:\"message\"`\n}\n\ntype respMessage struct {\n\tContent string `json:\"content\"`\n}\n"}}}}