4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Copilotのチャットで非対応LLMを使う方法

Posted at

GitHub Copilotのチャット機能ですが、複数のモデルから選択が可能です。

image.png

GitHub Copilotでは、「Bring your own language model key」という機能が用意されており、APIキーを持ち込んで利用することができます。

image.png

ここにあるモデルであればキーをセットするだけで使えるのですが、ここにないモデルも使いたいんです。

一筋縄ではいかなかったのですが、ひねりにひねって非対応モデルでも動作させられましたので紹介します。

もちろん、自己責任でご利用ください。

チャットのモデルとコード補完のモデルは別々の設定があるようです。今回扱うのはチャットで使うモデルです。

作戦

Bring your own language model keyで選べるOllamaを選択したうえで、テキスト生成の/chat/completionsAPIだけをLiteLLMを使って別のモデルにルーティングします。

設定のタイミングで/chat/completions以外のAPIが呼び出され、ここを突破しないと使えなかったのでこんな事になりました。

+-------------------------------------------------------+
|                  docker-compose.yml                    |
+-------------------------------------------------------+
|                                                       |
|  +---------------------+                              |
|  |       Nginx         |                              |
|  | +---------------+   |                              |
|  | |   Container   |   |                              |
|  | +---------------+   |                              |
|  |                     |                              |
|  | - image: nginx      |                              |
|  | - ports: "11434:80" |                              |
|  | - volumes:          |                              |
|  |   - ./nginx.conf    |                              |
|  +---------------------+                              |
|            |                                          |
|            |                                          |
|            v                                          |
|  +---------------------+     +---------------------+  |
|  |  nginx.conf         |     |       LiteLLM       |  |
|  |                     |     | +---------------+   |  |
|  | location /v1/chat/  |---->| |   Container   |   |  |
|  |   completions {     |     | +---------------+   |  |
|  |   proxy_pass        |     |                     |  |
|  |   litellm:4000;     |     | - image: litellm    |  |
|  |   }                 |     | - port: 4000        |  |
|  |                     |     | - config.yaml       |  |
|  | location / {        |     +---------------------+  |
|  |   proxy_pass        |                              |
|  |   ollama:11434;     |                              |
|  |   }                 |                              |
|  +---------------------+                              |
|            |                                          |
|            v                                          |
|  +---------------------+                              |
|  |       Ollama        |                              |
|  | +---------------+   |                              |
|  | |   Container   |   |                              |
|  | +---------------+   |                              |
|  |                     |                              |
|  | - image: ollama     |                              |
|  | - volumes: ./ollama |                              |
|  +---------------------+                              |
|                                                       |
+-------------------------------------------------------+

## 手順

以下のコードを準備します。

  • docker-compose.yml

    こちら
    docker-compose.yml
    services:
      ollama:
        image: ollama/ollama:latest
        container_name: ollama
        volumes:
          - ./ollama:/root/.ollama
        networks:
          - app_network
        restart: unless-stopped
    
      litellm:
        image: ghcr.io/berriai/litellm:main-latest
        container_name: litellm
        volumes:
          - ./litellm_config.yaml:/app/config.yaml
          - ~/.aws:/root/.aws
        networks:
          - app_network
        restart: unless-stopped
        depends_on:
          - ollama
        command: [ "--config", "/app/config.yaml", "--port", "4000", "--num_workers", "8" ]
    
      nginx:
        image: nginx:alpine
        container_name: nginx_proxy
        ports:
          - "11434:80"
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf:ro
        networks:
          - app_network
        restart: unless-stopped
        depends_on:
          - ollama
          - litellm
    
    networks:
      app_network:
        driver: bridge
    
  • nginx.conf

    こちら
    nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log warn;
    pid /var/run/nginx.pid;
    
    events {
        worker_connections 1024;
    }
    
    http {
        include /etc/nginx/mime.types;
        default_type application/json;
    
        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log /var/log/nginx/access.log main;
        sendfile on;
        keepalive_timeout 65;
    
        server {
            listen 80;
            
            # /v1/chat/completions のみ litellm にルーティング
            location /v1/chat/completions {
                proxy_pass http://litellm:4000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
            
            # その他のAPIは ollama にルーティング
            location / {
                proxy_pass http://ollama:11434;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }
    }
    
  • litellm_config.yaml

    こちら
    litellm_config.yaml
    model_list:
      - model_name: nova-premier
        litellm_params:
          model: bedrock/us.amazon.nova-premier-v1:0
          aws_region_name: us-east-1
    

準備ができたら起動します。

docker compose up

それでは設定を行っていきます。

  1. チャット欄にあるモデル名(この図だとClaude 3.5 Sonnet)をクリックし、「Manage models...」をクリックします

    image.png

  2. 「Ollama」を選択します

    image.png

  3. モデル名を入力します(nova-premierの部分)。ここはlitellm_config.yamlmodel_nameにセットした値です

    image.png

  4. Configure advances settngsは、「Yes」を選択します。(Noだと設定が失敗しちゃいます)

    image.png

  5. Enter a friendly name ...は空欄のままでOKです
    image.png

  6. maximum input tokensmaximum output tokensもデフォルトのまま行ってみましょう
    image.png

    image.png

  7. Does this model support visionは画像のインプットに対応しているかどうかです。Amazon Nova Proで有効にしてみましたが、うまく動きませんでしたので「No」でいきましょう
    image.png

これで設定完了です。

image.png

モデルの一覧に追加されました。

image.png

チャットしてみる

チャットができました!

image.png

設定が終わったあとはOllamaは不要でLiteLLMだけでOKのようです。

以上、誰得情報でした。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?