Skip to content

Commit

Permalink
feat: supports console capture and analysis of error info
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaostudy committed Dec 23, 2023
0 parents commit f85c592
Show file tree
Hide file tree
Showing 18 changed files with 767 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
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)`.
-->
14 changes: 14 additions & 0 deletions .github/workflows/license.yml
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
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.vscode

example/dev.env
41 changes: 41 additions & 0 deletions .licenserc
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
37 changes: 37 additions & 0 deletions bigmodel/bigmodel.go
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
)
141 changes: 141 additions & 0 deletions bigmodel/chatgpt.go
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
}
Loading

0 comments on commit f85c592

Please sign in to comment.