-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supports console capture and analysis of error info
- Loading branch information
0 parents
commit f85c592
Showing
18 changed files
with
767 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#### What type of PR is this? | ||
<!-- | ||
Add one of the following kinds: | ||
build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) | ||
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) | ||
docs: Documentation only changes | ||
feat: A new feature | ||
optimize: A new optimization | ||
fix: A bug fix | ||
perf: A code change that improves performance | ||
refactor: A code change that neither fixes a bug nor adds a feature | ||
style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc) | ||
test: Adding missing tests or correcting existing tests | ||
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation | ||
--> | ||
|
||
#### Check the PR title. | ||
<!-- | ||
The description of the title will be attached in Release Notes, | ||
so please describe it from user-oriented, what this PR does / why we need it. | ||
Please check your PR title with the below requirements: | ||
--> | ||
- [ ] This PR title match the format: \<type\>(optional scope): \<description\> | ||
- [ ] The description of this PR title is user-oriented and clear enough for others to understand. | ||
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. | ||
|
||
|
||
#### (Optional) Translate the PR title into Chinese. | ||
|
||
|
||
#### (Optional) More detailed description for this PR(en: English/zh: Chinese). | ||
<!-- | ||
Provide more detailed info for review(e.g., it's recommended to provide perf data if this is a perf type PR). | ||
--> | ||
en: | ||
zh(optional): | ||
|
||
|
||
#### (Optional) Which issue(s) this PR fixes: | ||
<!-- | ||
Automatically closes linked issue when PR is merged. | ||
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: License Check | ||
|
||
on: [ push, pull_request, workflow_dispatch ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
license: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Check License | ||
uses: apache/skywalking-eyes@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Go Link Check | ||
|
||
on: [ push, pull_request, workflow_dispatch ] | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.20 | ||
|
||
- name: Golangci Lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
.vscode | ||
|
||
example/dev.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
header: | ||
license: | ||
spdx-id: Apache-2.0 | ||
copyright-owner: ahaostudy | ||
content: | | ||
Copyright ahaostudy | ||
|
||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
paths-ignore: | ||
- '.idea/' | ||
- '.vscode/' | ||
- '.git/' | ||
- '.github/' | ||
- '.gitignore' | ||
- '**/**.md' | ||
- '**/**.mod' | ||
- '**/**.sum' | ||
- '**/**.env' | ||
- 'LICENSE' | ||
|
||
comment: on-failure | ||
|
||
license-location-threshold: 100 | ||
|
||
dependency: | ||
files: | ||
- go.mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright ahaostudy | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package bigmodel | ||
|
||
// BigModel interface | ||
type BigModel interface { | ||
// Chat Receive a query for large model calls and write the output results to the Result channel in real time | ||
Chat(query string) chan Result | ||
} | ||
|
||
type Result struct { | ||
Type int | ||
Content string | ||
} | ||
|
||
const ( | ||
TypeData = iota | ||
TypeDone | ||
TypeError | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* Copyright ahaostudy | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package bigmodel | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"strings" | ||
|
||
"github.com/ahaostudy/code-dignostic/utils" | ||
) | ||
|
||
// ChatGPT big model | ||
type ChatGPT struct { | ||
url string | ||
|
||
model string | ||
baseURL string | ||
apiKey string | ||
} | ||
|
||
func NewChatGPT(apiKey string, opts ...Option) BigModel { | ||
gpt := &ChatGPT{ | ||
model: "gpt-3.5-turbo", | ||
baseURL: "https://api.openai.com", | ||
apiKey: apiKey, | ||
} | ||
for _, opt := range opts { | ||
opt(gpt) | ||
} | ||
gpt.url = strings.TrimSuffix(gpt.baseURL, "/") + "/v1/chat/completions" | ||
return gpt | ||
} | ||
|
||
type Option func(*ChatGPT) | ||
|
||
// WithSpecifyModel specify ChatGPT model | ||
func WithSpecifyModel(model string) Option { | ||
return func(gpt *ChatGPT) { | ||
gpt.model = model | ||
} | ||
} | ||
|
||
// WithSpecifyBaseURL specify OPENAI API base url | ||
func WithSpecifyBaseURL(url string) Option { | ||
return func(gpt *ChatGPT) { | ||
gpt.baseURL = url | ||
} | ||
} | ||
|
||
type chunk struct { | ||
Choices []struct { | ||
Delta struct { | ||
Content string `json:"content"` | ||
} `json:"delta"` | ||
} `json:"choices"` | ||
} | ||
|
||
func (gpt *ChatGPT) Chat(query string) chan Result { | ||
out := make(chan Result) | ||
|
||
go func() { | ||
req := utils.NewRequest(gpt.url) | ||
req.SetData(map[string]interface{}{ | ||
"model": gpt.model, | ||
"stream": true, | ||
"messages": []map[string]string{ | ||
{ | ||
"role": "user", | ||
"content": query, | ||
}, | ||
}, | ||
}) | ||
req.SetHeader("Authorization", "Bearer "+gpt.apiKey) | ||
req.SetHeader("Content-Type", "application/json") | ||
|
||
// response | ||
resp, err := req.POST() | ||
if err != nil { | ||
log.Fatalln("openai request failed:", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// read response stream data | ||
buf := make([]byte, 4096) | ||
for { | ||
n, err := resp.Body.Read(buf) | ||
if err == io.EOF { | ||
out <- Result{Type: TypeDone} | ||
return | ||
} | ||
if err != nil { | ||
out <- Result{Type: TypeError, Content: err.Error()} | ||
return | ||
} | ||
|
||
chunks := string(buf[:n]) | ||
data := new(chunk) | ||
for _, chunk := range strings.Split(chunks, "\n\n") { | ||
if !strings.HasPrefix(chunk, "data: ") { | ||
continue | ||
} | ||
chunk = strings.TrimPrefix(chunk, "data: ") | ||
|
||
// done | ||
if chunk == "[DONE]" { | ||
out <- Result{Type: TypeDone} | ||
return | ||
} | ||
|
||
err := json.Unmarshal([]byte(chunk), data) | ||
if err != nil { | ||
out <- Result{Type: TypeError, Content: err.Error()} | ||
return | ||
} | ||
out <- Result{Type: TypeData, Content: data.Choices[0].Delta.Content} | ||
} | ||
} | ||
}() | ||
|
||
return out | ||
} |
Oops, something went wrong.