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 baichuan llm support #979

Merged
merged 7 commits into from
May 22, 2024

Conversation

lizzy-0323
Copy link
Contributor

@lizzy-0323 lizzy-0323 commented May 20, 2024

Ⅰ. Describe what this PR did

Support baichuan ai llm, api documentation (https://platform.baichuan-ai.com/docs/api)

Ⅱ. Does this pull request fix one issue?

fix #952

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

Ⅳ. Describe how to verify it

envoy.yaml:

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: baichuan
                            timeout: 300s
                http_filters:
                  - name: baichuan
                    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: baichuan
                          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": "baichuan",
                                  "apiTokens": [
                                    "sk-xxxxxx"
                                  ],
                                  "withSearchEnhance":false
                                }
                              }

                  - name: envoy.filters.http.router
  clusters:
    - name: baichuan
      connect_timeout: 30s
      type: LOGICAL_DNS
      dns_lookup_family: V4_ONLY
      lb_policy: ROUND_ROBIN
      load_assignment:
        cluster_name: baichuan
        endpoints:
          - lb_endpoints:
              - endpoint:
                  address:
                    socket_address:
                      address: api.baichuan-ai.com
                      port_value: 443
      transport_socket:
        name: envoy.transport_sockets.tls
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
          "sni": "api.baichuan-ai.com"

Request:

curl "http://localhost:10000/v1/chat/completions"  -H "Content-Type: application/json" -d '{
  "model": "Baichuan2-Turbo",            
  "messages": [                                                     
    {                        
      "role": "user",                
      "content": "你好,你是谁?"
    }                                                                                 
  ]                                                 
}'  

Response:

{
   "id":"chatcmpl-Mcdc9015KFRHoSV",
   "object":"chat.completion",
   "created":1716190037,
   "model":"Baichuan2-Turbo",
   "choices":[
      {
         "index":0,
         "message":{
            "role":"assistant",
            "content":"你好!我是百川大模型,是由百川智能的工程师们创造的大语言模型,我可以和人类进行自然交流、解答问题、协助创作,帮助大众轻松、普惠的获得世界知识和专业服务。如果你有任何问题,可以随时向我提问。"
         },
         "finish_reason":"stop"
      }
   ],
   "usage":{
      "prompt_tokens":6,
      "completion_tokens":52,
      "total_tokens":58
   }
}

Ⅴ. Special notes for reviews

在baichuan ai的api文档中,我发现有一个api parameter是with_search_enhance, 这是一个bool类型的参数,每次打开需要消耗额外的token,我认为该参数只需要从context json中获取即可,故没有添加到文档和代码中。

@CLAassistant
Copy link

CLAassistant commented May 20, 2024

CLA assistant check
All committers have signed the CLA.

@johnlanni johnlanni requested a review from CH3CHO May 20, 2024 09:22
@johnlanni
Copy link
Collaborator

cc @CH3CHO

@lizzy-0323 lizzy-0323 changed the title feature: add baichuan llm support feat: add baichuan llm support May 20, 2024
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.

由于先合并了另外一个模型的 provider,代码有冲突了。麻烦和上面的评论一并处理一下。谢谢!

plugins/wasm-go/extensions/ai-proxy/README.md Outdated Show resolved Hide resolved
@lizzy-0323 lizzy-0323 requested a review from CH3CHO May 21, 2024 12:00
Updated comments in provider.go, add "yi"
@lizzy-0323 lizzy-0323 requested a review from CH3CHO May 21, 2024 13:48
@CH3CHO CH3CHO merged commit fc6a6aa into alibaba:main May 22, 2024
11 checks passed
@Suchun-sv
Copy link
Contributor

Suchun-sv commented May 27, 2024

Ⅰ. Describe what this PR did

Support baichuan ai llm, api documentation (https://platform.baichuan-ai.com/docs/api)

Ⅱ. Does this pull request fix one issue?

fix #952

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

Ⅳ. Describe how to verify it

envoy.yaml:

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: baichuan
                            timeout: 300s
                http_filters:
                  - name: baichuan
                    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: baichuan
                          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": "baichuan",
                                  "apiTokens": [
                                    "sk-xxxxxx"
                                  ],
                                  "withSearchEnhance":false
                                }
                              }

                  - name: envoy.filters.http.router
  clusters:
    - name: baichuan
      connect_timeout: 30s
      type: LOGICAL_DNS
      dns_lookup_family: V4_ONLY
      lb_policy: ROUND_ROBIN
      load_assignment:
        cluster_name: baichuan
        endpoints:
          - lb_endpoints:
              - endpoint:
                  address:
                    socket_address:
                      address: api.baichuan-ai.com
                      port_value: 443
      transport_socket:
        name: envoy.transport_sockets.tls
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
          "sni": "api.baichuan-ai.com"

Request:

curl "http://localhost:10000/v1/chat/completions"  -H "Content-Type: application/json" -d '{
  "model": "Baichuan2-Turbo",            
  "messages": [                                                     
    {                        
      "role": "user",                
      "content": "你好,你是谁?"
    }                                                                                 
  ]                                                 
}'  

Response:

{
   "id":"chatcmpl-Mcdc9015KFRHoSV",
   "object":"chat.completion",
   "created":1716190037,
   "model":"Baichuan2-Turbo",
   "choices":[
      {
         "index":0,
         "message":{
            "role":"assistant",
            "content":"你好!我是百川大模型,是由百川智能的工程师们创造的大语言模型,我可以和人类进行自然交流、解答问题、协助创作,帮助大众轻松、普惠的获得世界知识和专业服务。如果你有任何问题,可以随时向我提问。"
         },
         "finish_reason":"stop"
      }
   ],
   "usage":{
      "prompt_tokens":6,
      "completion_tokens":52,
      "total_tokens":58
   }
}

Ⅴ. Special notes for reviews

在baichuan ai的api文档中,我发现有一个api parameter是with_search_enhance, 这是一个bool类型的参数,每次打开需要消耗额外的token,我认为该参数只需要从context json中获取即可,故没有添加到文档和代码中。

疑似api token泄露

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.

AI 代理 Wasm 插件对接百川大模型
5 participants