Skip to content

Commit

Permalink
add embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
ljnchn committed Jan 10, 2024
1 parent 26db248 commit c4d5927
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
copilot2gpt
.env
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ release:
rm -rf ${DIST_DIR}/*.gz
# Build for mac
GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-mac64-${VERSION}.tar.gz ./${BINARY} .env
tar czvf ${DIST_DIR}/${BINARY}-mac64-${VERSION}.tar.gz ./${BINARY} .env.example
# Build for arm
go clean
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-linux-arm64-${VERSION}.tar.gz ./${BINARY} .env
tar czvf ${DIST_DIR}/${BINARY}-linux-arm64-${VERSION}.tar.gz ./${BINARY} .env.example
# Build for linux386
go clean
CGO_ENABLED=0 GOOS=linux GOARCH=386 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-linux-386-${VERSION}.tar.gz ./${BINARY} .env
tar czvf ${DIST_DIR}/${BINARY}-linux-386-${VERSION}.tar.gz ./${BINARY} .env.example
# Build for linux
go clean
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-linux-64-${VERSION}.tar.gz ./${BINARY} .env
tar czvf ${DIST_DIR}/${BINARY}-linux-64-${VERSION}.tar.gz ./${BINARY} .env.example
# Build for win386
go clean
CGO_ENABLED=0 GOOS=windows GOARCH=386 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-win-386-${VERSION}.tar.gz ./${BINARY}.exe .env
tar czvf ${DIST_DIR}/${BINARY}-win-386-${VERSION}.tar.gz ./${BINARY}.exe .env.example
# Build for win
go clean
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}"
tar czvf ${DIST_DIR}/${BINARY}-win-64-${VERSION}.tar.gz ./${BINARY}.exe .env
tar czvf ${DIST_DIR}/${BINARY}-win-64-${VERSION}.tar.gz ./${BINARY}.exe .env.example
go clean
# Cleans our projects: deletes binaries
clean:
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

## 运行程序

`cp .env.example .env`

`./copilot2gpt`

默认监听端口为 8081,可以在 .env 中修改
Expand All @@ -43,6 +45,16 @@ curl --location 'http://127.0.0.1:8081/v1/chat/completions' \
}'
```

``` bash
curl --location 'http://127.0.0.1:8081/v1/embeddings' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ghu_xxx' \
--data '{
"input":["Your text string goes here"],
"model":"text-embedding-ada-002"
}'
```



## 感谢以下项目,灵感来自于VV佬
Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (

const tokenUrl = "https://api.github.com/copilot_internal/v2/token"
const completionsUrl = "https://api.githubcopilot.com/chat/completions"
const embeddingsUrl = "https://api.githubcopilot.com/embeddings"

var requestUrl = ""

type Model struct {
ID string `json:"id"`
Expand All @@ -39,7 +42,7 @@ type ModelList struct {
Data []Model `json:"data"`
}

var version = "v0.2"
var version = "v0.3"
var port = "8081"

func main() {
Expand Down Expand Up @@ -92,6 +95,15 @@ func main() {
c.Header("Cache-Control", "no-cache, must-revalidate")
c.Header("Connection", "keep-alive")

requestUrl = completionsUrl
forwardRequest(c)
})

r.POST("/v1/embeddings", func(c *gin.Context) {
c.Header("Cache-Control", "no-cache, must-revalidate")
c.Header("Connection", "keep-alive")

requestUrl = embeddingsUrl
forwardRequest(c)
})

Expand Down Expand Up @@ -139,7 +151,7 @@ func forwardRequest(c *gin.Context) {

isStream := gjson.GetBytes(jsonData, "stream").String() == "true"

req, err := http.NewRequest("POST", completionsUrl, bytes.NewBuffer(jsonData))
req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(jsonData))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
Expand All @@ -163,7 +175,7 @@ func forwardRequest(c *gin.Context) {
log.Printf("对话失败:%d, %s ", resp.StatusCode, bodyString)
cache := cache.New(5*time.Minute, 10*time.Minute)
cache.Delete(ghuToken)
c.AbortWithError(resp.StatusCode, err)
c.AbortWithError(resp.StatusCode, fmt.Errorf(bodyString))
return
}

Expand Down

0 comments on commit c4d5927

Please sign in to comment.