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

Add support for AI APIs #10493

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

npolshakova
Copy link
Contributor

@npolshakova npolshakova commented Jan 23, 2025

Description

Adds support for AI APIs: #10494

API changes

Introduces AI APIs in kgateway. This is based on the Gloo AI APIs.

The HTTPRoute selects an Upstream via a backendRef:

apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: route-to-upstream
  namespace: gwtest
spec:
  parentRefs:
    - name: http-gw-for-test
  hostnames:
    - "test"
  rules:
    - matches:
        - path:
            type: Exact
            value: /v1/chat/completions
    - backendRefs:
        - name: openai
          kind: Upstream
          group: gateway.kgateway.dev

Where an Upstream specifies a provider and auth type:

apiVersion: gateway.kgateway.dev/v1alpha1
kind: Upstream
metadata:
  labels:
    app: kgateway
  name: anthropic
  namespace: gwtest
spec:
  ai:
    llm:
      provider:
        anthropic:
          authToken:
            kind: "Passthrough"

To select a RoutePolicy you can use extensionRef on the AI Upstream route:

apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: route-to-upstream
  namespace: gwtest
spec:
  parentRefs:
    - name: http-gw-for-test
  hostnames:
    - "test"
  rules:
    - matches:
        - path:
            type: Exact
            value: /v1/chat/completions
    - backendRefs:
        - name: openai
          kind: Upstream
          group: gateway.kgateway.dev
      filters:
      - type: ExtensionRef
        extensionRef:
           group: gateway.kgateway.dev/v1alpha1
           kind: RoutePolicy
           name: open-ai-opt
---
apiVersion: gateway.kgateway.dev/v1alpha1
kind: RoutePolicy
metadata:
  name: route-test
  namespace: gwtest
spec:
  ai:
    promptEnrichment:
      prepend:
        - role: SYSTEM
          content: "respond to all questions in French"
        - role: SYSTEM
          content: "Say hello before each response"
    promptGuard:
      request:
        customResponse:
          message: "Rejected due to inappropriate content"
          statusCode: 400
        regex:
          matches:
          - pattern: "credit card"
      response:
        regex:
          builtins:
            - PHONE_NUMBER
            - EMAIL
            - SSN
            - CREDIT_CARD

The APIs follow the required vs. optional API conventions defined in https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#optional-vs-required:

Optional fields:

  • Have omitempty set
  • Are pointer type in the Go definition (e.g. AwesomeFlag *SomeFlag) or have a built-in nil value (e.g. maps and slices).

Required fields:

  • Do not have omitempty set
  • They are not a pointer type in the Go definition (e.g. AnotherFlag SomeFlag).

Code changes

Plugin and extauth changes will be implemented in a follow up. This PR only introduces the AI APIs.

CI changes

NONE

Docs changes

NONE

Context

Enhancement Proposal: #10495

Testing steps

Manually validated CR validation by install helm chart and applying example configs.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

Copy link
Contributor

@EItanya EItanya left a comment

Choose a reason for hiding this comment

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

Overall this make sense to me. I have some nits about structure, but my big takeaways are:

  1. What's the decision process between pointer and non-pointer types throughout the API, it's not clear to me.
  2. Why are we using iota for enums in some place, and string aliases in others?
  3. The AIRouteOptions need more validation akin to the upstream.

@npolshakova npolshakova force-pushed the port-ai-apis-to-kgateway branch 2 times, most recently from 31cda6a to 3510255 Compare February 11, 2025 13:57
@npolshakova npolshakova force-pushed the port-ai-apis-to-kgateway branch 2 times, most recently from 184f600 to ad175ad Compare February 12, 2025 23:48
@npolshakova npolshakova changed the title [WIP] Add support for AI APIs Add support for AI APIs Feb 13, 2025
@npolshakova npolshakova force-pushed the port-ai-apis-to-kgateway branch from 63f82c9 to 930afa2 Compare February 13, 2025 22:28
@npolshakova npolshakova marked this pull request as ready for review February 13, 2025 22:34
This was referenced Feb 14, 2025
@npolshakova npolshakova requested a review from EItanya February 17, 2025 19:53
@npolshakova npolshakova force-pushed the port-ai-apis-to-kgateway branch from f5d16d3 to 37538fd Compare February 19, 2025 16:22
@lgadban lgadban linked an issue Feb 24, 2025 that may be closed by this pull request
@npolshakova npolshakova requested review from andy-fong and removed request for andy-fong February 24, 2025 19:22
@npolshakova npolshakova requested a review from lgadban February 24, 2025 19:22
@artberger artberger mentioned this pull request Feb 18, 2025
3 tasks
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.

Add AI Gateway APIs
4 participants