Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the ZhipuAI (ChatGLM) provider to the ai-proxy wasm plugin #950 #1007

Merged
merged 2 commits into from
May 28, 2024

Conversation

Suchun-sv
Copy link
Contributor

Ⅰ. Describe what this PR did

Add the ZhipuAI (ChatGLM) provider to the ai-proxy wasm plugin.

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

0. cd repo (ai-proxy)

1. build main.wasm

tinygo build -o main.wasm -scheduler=none -target=wasi -gc=custom -tags="custommalloc nottinygc_finalizer" ./

2. docker-compose up -d

docker-compose.yaml

version: '3.7'
services:
  envoy:
    image: higress-registry.cn-hangzhou.cr.aliyuncs.com/higress/gateway:v1.4.0-rc.1
    entrypoint: /usr/local/bin/envoy
    # 注意这里对wasm开启了debug级别日志,正式部署时则默认info级别
    command: -c /etc/envoy/envoy.yaml --component-log-level wasm:debug
    depends_on:
    - httpbin
    networks:
    - wasmtest
    ports:
    - "10000:10000"
    volumes:
    - ./envoy.yaml:/etc/envoy/envoy.yaml
    - ./main.wasm:/etc/envoy/plugin.wasm
    - ./main.wasm:/etc/envoy/main.wasm

  httpbin:
    image: kennethreitz/httpbin:latest
    networks:
    - wasmtest
    ports:
    - "12345:80"

networks:
  wasmtest: {}

envoy.yaml

# File generated by hgctl. Modify as required.

admin:
  address:
    socket_address:
      protocol: TCP
      address: 0.0.0.0
      port_value: 9901
static_resources:
  listeners:
    - name: listener_0
      address:
        socket_address:
          protocol: TCP
          address: 0.0.0.0
          port_value: 10000
      filter_chains:
        - filters:
            - name: envoy.filters.network.http_connection_manager
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                scheme_header_transformation:
                  scheme_to_overwrite: https
                stat_prefix: ingress_http
                # Output envoy logs to stdout
                access_log:
                  - name: envoy.access_loggers.stdout
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
                # Modify as required
                route_config:
                  name: local_route
                  virtual_hosts:
                    - name: local_service
                      domains: [ "*" ]
                      routes:
                        - match:
                            prefix: "/"
                          route:
                            cluster: zhipuai
                            timeout: 300s
                http_filters:
                  - name: wasmtest
                    typed_config:
                      "@type": type.googleapis.com/udpa.type.v1.TypedStruct
                      type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
                      value:
                        config:
                          name: wasmtest
                          vm_config:
                            runtime: envoy.wasm.runtime.v8
                            code:
                              local:
                                filename: /etc/envoy/main.wasm
                          configuration:
                            "@type": "type.googleapis.com/google.protobuf.StringValue"
                            value: |
                              {
                                "provider": {
                                  "type": "zhipuai",
                                  "domain": "open.bigmodel.cn",
                                  "apiTokens": [
                                    "*"
                                  ],
                                  "timeout": 1200000,
                                  "modelMapping": {
                                    "gpt-3": "glm-3-turbo",
                                    "gpt-35-turbo": "glm-4v",
                                    "gpt-4-turbo": "glm-4",
                                    "*": "glm-3-turbo"
                                  }
                                }
                              }
                  - name: envoy.filters.http.router
  clusters:
    - name: zhipuai
      connect_timeout: 30s
      type: LOGICAL_DNS
      dns_lookup_family: V4_ONLY
      lb_policy: ROUND_ROBIN
      load_assignment:
        cluster_name: zhipuai
        endpoints:
          - lb_endpoints:
              - endpoint:
                  address:
                    socket_address:
                      address: open.bigmodel.cn
                      port_value: 443
      transport_socket:
        name: envoy.transport_sockets.tls
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
          "sni": "open.bigmodel.cn"

#### 3. Q&A
```bash
curl "http://localhost:10000/v1/chat/completions"  -H "Content-Type: application/json" -d '{
  "model": "glm-4",            
  "messages": [                                                     
    {                        
      "role": "user",                
      "content": "你好,你是哪个公司的?"
    }                                                                                 
  ]                                                 
}'  
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "你好,我是人工智能助手,不属于任何公司,\n我是由清华大学 KEG 实验室和智谱 AI 共同开发的。\n我的目的是为了帮助用户解决问题,提供信息和建议。\n有什么我可以帮到你的吗?",
        "role": "assistant"
      }
    }
  ],
  "created": 1716851431,
  "id": "*",
  "model": "glm-4",
  "request_id": "*",
  "usage": {
    "completion_tokens": 47,
    "prompt_tokens": 11,
    "total_tokens": 58
  }
}

Ⅴ. Special notes for reviews

Copy link
Collaborator

@CH3CHO CH3CHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@CH3CHO CH3CHO merged commit 51b9d9e into alibaba:main May 28, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants