Sign inSign up

anotia/cogpt

By anotia

Updated almost 2 years ago

Get access to Claude 3.5 Sonnet, GPT-4o & o1 via copilot.

Image
0

4.3K

anotia/cogpt repository overview

CoGPT

中文教程见 原作者的博客

Updates:

  • 2024.08.02 更新了 VS Code 和 Copilot Chat 版本,并添加了 GPT-4o 模型。
  • 2024.10.30 更新了 VS Code 和 Copilot Chat 版本,并添加了 gpt-4o, gpt-4o-mini, gpt-4o-2024-08-06, o1-previewo1-mini 模型。
  • 2024.11.08 更新了 VS Code,并添加了 claude-3.5-sonnet 模型(GitHub 文档)。注意:必须把项目部署在除大陆/香港以外的地方,才能使用这个模型

Only for network programming learning purposes.

Features

API 用法和 OpenAI 几乎一样,不过 token 换成 Copilot 的。Copilot 的所有模型都可以使用。

使用方法

GitHub 会封代码仓库,因此暂时只能用 Docker 部署。

获得 token

运行下面的命令:

docker run --rm anotia/cogpt:apptoken-latest

在浏览器中打开 https://github.com/login/device ,输入脚本提供的 8 位代码(xxxx-xxxx),确认登录后,脚本会输出 token(以 ghu_ 开头)。

部署 API 服务

创建一个文件夹:

mkdir CoGPT && cd CoGPT

然后创建一个 docker-compose.yml 文件,内容如下:

version: '3'
services:
  cogpt-api:
    image: anotia/cogpt:latest
    environment:
      - HOST=0.0.0.0
    ports:
      - 8080:8080
    volumes:
      - ./db:/app/db
      - ./log:/app/log
    restart: unless-stopped
    container_name: cogpt-api

如果你想使用开发版本,将 anotia/cogpt:latest 替换为 anotia/cogpt:dev

默认情况下,服务会监听 8080 端口。如果你想更改端口,可以编辑 ports。例如,要监听 80 端口的话,把 8080:8080 改为 80:8080

然后运行 docker compose up -d 来启动服务。

调用 API

基本上用法和 OpenAI API 一样,只是 API key 用上面获取的就行。

  • GET /
    • Returns Hi, I'm CoGPT.
  • GET /health
    • Returns {"status":"OK"}
  • GET /v1/models
    • return available models
  • POST /v1/chat/completions
    • for chat api
  • POST /v1/embeddings
    • for embeddings api
      Pay attention that this api is not totally compatible with OpenAI API.
      For input field, OpenAI API accepts following types:

      • string: The string that will be turned into an embedding.
      • array: The array of strings that will be turned into an embedding.
      • array: The array of integers that will be turned into an embedding.
      • array: The array of arrays containing integers that will be turned into an embedding.

      Unfortunately, this service only accepts the first 2 types as well as the array of arrays containing strings.

Deploy
Warning

This service is not designed to be deployed on public network.
The best way to use is to deploy it on your own computer or local network. Or you can deploy it on public network, but only for yourself.
DO NOT share your token with others. If a token is accessed from many different IPs, it will be banned. And if too many tokens are requested from one IP, something bad may happen.
So again, ONLY for yourself.

Best approach
  1. Deploy locally on your own computer
  2. Deploy on local network for personal use or share in small group
  3. Deploy on your own server for personal use
Bad approach
  1. Provide public interface for everyone to use
    In this way, many tokens will be requested from one IP, which will cause problems.
  2. Provide public integrade (web) apps (such as ChatGPT-Next-Web)
    Makint too many requests with one token will cause problems.
  3. Deploy on with serverless services (such as Vercel)
    Serverless services will change IP frequently, and they have short lifetime.
  4. Any abuse of the service

DO NOT try any of theses approaches.

Deploy without Docker

Download the latest release for your platform. Unzip it and run cogpt-api (or cogpt-api.exe on Windows).

By default, the service will listen on localhost:8080. For configuration, see Config.

Run as a service
Linux

For Linux based on systemd, you can follow the steps below.

First, download the latest release for Linux. Unzip it and move cogpt-api to /opt/cogpt/ and grant it executable permission.

Then copy the content of cogpt-api.service to /etc/systemd/system/cogpt-api.service.

If you need to change the config, you should edit /opt/cogpt/.env file.

Finally, run following commands to enable and start the service.

sudo systemctl enable cogpt-api
sudo systemctl start cogpt-api

Run sudo systemctl stop cogpt-api to stop the service.

Run sudo systemctl disable cogpt-api to disable the service.

MacOS

For MacOS, services are based on launchd.

First, download the latest release for MacOS. Unzip it and move cogpt-api to /opt/cogpt/ and grant it executable permission.

Then copy the content of com.cogpt-api.plist to /Library/LaunchDaemons/com.cogpt-api.plist.

If you need to change the config, you should edit /opt/cogpt/.env file.

Finally, run sudo launchctl load /Library/LaunchDaemons/com.cogpt-api.plist to start the service.

Run sudo launchctl unload /Library/LaunchDaemons/com.cogpt-api.plist to stop the service.

Windows

For Windows, we can use scheduled tasks. You can follow the steps below.

First, download the latest release for Windows. Unzip it to a directory. Let's say C:\CoGPT\.

Then create a file cogpt-api-service.ps1 in C:\CoGPT\ with copy content of cogpt-api-service.ps1 to it.

Start a PowerShell with administrator permission and run following commands.

cd C:\CoGPT\
./cogpt-api-service.ps1 enable

Here are all commands you can use. All commands should be run in PowerShell with administrator permission.

./cogpt-api-service.ps1 enable  # enable and start the service
./copgt-api-service.ps1 disable # stop and disable the service
./cogpt-api-service.ps1 start   # start the service
./cogpt-api-service.ps1 stop    # stop the service
./cogpt-api-service.ps1 restart # restart the service
./cogpt-api-service.ps1 status  # check the status of the service
Share Token

If you want to share this service with your friends, it's not safe to directly share your GitHub app token. This feature is designed for this situation. You can create map that maps so-called share token to real GitHub app token.

The first way is to set environment variable or modify .env environment variable file. You should set SHARE_TOKEN to a string like share-xxxxxxx1:ghu_xxxxxxx1,share-xxxxxxx2:ghu_xxxxxxx2. The format is share-token:real-token,share-token:real-token. You can add as many pairs as you want.

The other way is to use command line argument. You can run ./cogpt-api -share-token share-xxxxxxx1:ghu_xxxxxxx1,share-xxxxxxx2:ghu_xxxxxxx2 to start the service. You can add as many pairs as you want.

If you set as above, when you make a request with a token that starts with share-, the service will use the real token mapped to the share token. If you make a request with a token that starts with ghu_, the service will use the token directly.

Note that share tokens must start with share-. Maps that don't start with share- will be ignored.

To generate a random share token, you can download the latest release for your platform. Unzip it and run ./gen-share-token.

Config

Edit .env or set environment variables or command line arguments.

Here are the config options and their default values (.env or environment variables):

keysdefaultdescription
HOSTlocalhostHost to listen on
PORT8080Port to listen on
CACHEtrueWhether to cache tokens in sqlite database. If false, tokens will be cached in memory
CACHE_PATHdb/cache.sqlite3Path to sqlite database. Only used if CACHE is true
DEBUGfalseWhether to enable debug mode. If true, the service will print debug info
LOG_LEVELinfoLog level.
SHARE_TOKEN""Maps of share-token and real token. For example, SHARE_TOKEN=share-xxxxxxx1:ghu_xxxxxxx1,share-xxxxxxx2:ghu_xxxxxxx2.

For command line arguments, run ./cogpt-api -h to see the help message.

Precedence: command line arguments > environment variables > .env.

Proxy

Environment variables for proxy are also supported. They are ALL_PROXY, HTTPS_PROXY and HTTP_PROXY. You can also use command line arguments to set proxy. Run ./cogpt-api -h to see the help message. Precedence: command line arguments > environment variables (ALL_PROXY > HTTPS_PROXY > HTTP_PROXY).

Credits

License

MPL-2.0

Star History

Star History Chart

Tag summary

Content type

Image

Digest

sha256:198e2f587

Size

10.3 MB

Last updated

almost 2 years ago

docker pull anotia/cogpt