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

refactor(*): change ai analytics #12583

Merged
merged 21 commits into from
Apr 15, 2024
Merged

refactor(*): change ai analytics #12583

merged 21 commits into from
Apr 15, 2024

Conversation

AntoineJac
Copy link
Contributor

@AntoineJac AntoineJac commented Feb 19, 2024

Summary

This will allow to add a provide layer in the analytics in case several plugins are used

Current model is:

  "ai": {
    "meta": {
      "response_model": "mistral-tiny",
      "provider_name": "mistral",
      "request_model": "mistral-tiny"
    },
    "usage": {
      "prompt_tokens": 259,
      "completion_tokens": 274,
      "total_tokens": 533
    }
  }

New model if using several plugins:

  "ai": {
    "mistral": {
      "request_total_tokens": 648,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "mistral-tiny",
            "plugin_id": "1c783122-0223-4a03-9f1d-c71d2baab01e",
            "response_model": "mistral-tiny",
            "provider_name": "mistral"
          },
          "usage": {
            "prompt_token": 392,
            "total_tokens": 648,
            "completion_token": 256
          }
        }
      ],
      "request_prompt_tokens": 392,
      "request_completion_tokens": 256
    },
    "azure": {
      "request_total_tokens": 145,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "gpt-35-turbo",
            "plugin_id": "5df193be-47a3-4f1b-8c37-37e31af0568b",
            "response_model": "gpt-35-turbo",
            "provider_name": "azure"
          },
          "usage": {
            "prompt_token": 89,
            "total_tokens": 145,
            "completion_token": 56
          }
        }
      ],
      "request_prompt_tokens": 89,
      "request_completion_tokens": 56
    },
    "cohere": {
      "request_total_tokens": 149,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "command",
            "plugin_id": "30a63ab9-03c6-447a-9b25-195eb0acaeb2",
            "response_model": "command",
            "provider_name": "cohere"
          },
          "usage": {
            "prompt_token": 78,
            "total_tokens": 149,
            "completion_token": 71
          }
        }
      ],
      "request_prompt_tokens": 78,
      "request_completion_tokens": 71
    }
  }

Checklist

Issue reference

Fix #[issue number]

KAG-3759
KAG-4124

EDIT: format JSON snippets

@CLAassistant
Copy link

CLAassistant commented Feb 19, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@AntoineJac
Copy link
Contributor Author

AntoineJac commented Mar 1, 2024

By using the AI request and response transformers you will get following logs:

When using same AI provider:

"ai": {
    "cohere": {
      "instances": [
        {
          "meta": {
            "response_model": "command",
            "provider_name": "cohere",
            "request_model": "command"
          },
          "usage": {
            "completion_tokens": 33,
            "prompt_tokens": 129,
            "total_tokens": 162
          }
        },
        {
          "meta": {
            "response_model": "command",
            "provider_name": "cohere",
            "request_model": "command"
          },
          "usage": {
            "completion_tokens": 25,
            "prompt_tokens": 412,
            "total_tokens": 437
          }
        }
      ],
      "request_prompt_tokens": 541,
      "request_completion_tokens": 58,
      "request_total_tokens": 599,
      "number_of_instances": 2
    }
  }

When using different AI providers:

  "ai": {
    "cohere": {
      "instances": [
        {
          "meta": {
            "response_model": "command",
            "provider_name": "cohere",
            "request_model": "command"
          },
          "usage": {
            "completion_tokens": 130,
            "prompt_tokens": 129,
            "total_tokens": 259
          }
        }
      ],
      "request_prompt_tokens": 129,
      "request_completion_tokens": 130,
      "request_total_tokens": 259,
      "number_of_instances": 1
    },
    "mistral": {
      "instances": [
        {
          "meta": {
            "response_model": "mistral-tiny",
            "provider_name": "mistral",
            "request_model": "mistral-tiny"
          },
          "usage": {
            "completion_tokens": 171,
            "prompt_tokens": 562,
            "total_tokens": 733
          }
        }
      ],
      "request_prompt_tokens": 562,
      "request_completion_tokens": 171,
      "request_total_tokens": 733,
      "number_of_instances": 1
    }
  }

EDIT: JSON formatting

@locao locao requested a review from gszr March 19, 2024 20:36
@subnetmarco
Copy link
Member

Would it be possible to also store the plugin_id for each instance so that the user can point back to where it's being used?

@github-actions github-actions bot added the cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee label Mar 28, 2024
@RobSerafini RobSerafini added this to the 3.7.0 milestone Apr 8, 2024
@tysoekong tysoekong force-pushed the refactor/ai-analytics branch 2 times, most recently from 935038c to bbfe32e Compare April 14, 2024 20:20
kong/llm/drivers/shared.lua Outdated Show resolved Hide resolved
kong/llm/drivers/shared.lua Show resolved Hide resolved
kong/llm/drivers/shared.lua Show resolved Hide resolved
kong/llm/drivers/shared.lua Outdated Show resolved Hide resolved
kong/plugins/ai-proxy/handler.lua Outdated Show resolved Hide resolved
spec/03-plugins/38-ai-proxy/02-openai_integration_spec.lua Outdated Show resolved Hide resolved
Copy link
Contributor

@tysoekong tysoekong left a comment

Choose a reason for hiding this comment

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

I've rebase'd and added a test - this is good to go now!

@AntoineJac
Copy link
Contributor Author

It seems we have resolved all the points. Are we ok to push this PR? @jschmid1
Thanks

@jschmid1 jschmid1 self-requested a review April 15, 2024 15:08
@jschmid1 jschmid1 merged commit 0db4b62 into master Apr 15, 2024
25 checks passed
@jschmid1 jschmid1 deleted the refactor/ai-analytics branch April 15, 2024 15:10
@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-12583-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-12583-to-master-to-upstream
git checkout -b cherry-pick-12583-to-master-to-upstream
ancref=$(git merge-base 1b920ca144aed6669b2b1dbe5003683b4fcd18d3 9f3ab14f104e81d317aa6f2cc2d8337f5e72178a)
git cherry-pick -x $ancref..9f3ab14f104e81d317aa6f2cc2d8337f5e72178a

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-12583-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-12583-to-master-to-upstream
git checkout -b cherry-pick-12583-to-master-to-upstream
ancref=$(git merge-base 1b920ca144aed6669b2b1dbe5003683b4fcd18d3 9f3ab14f104e81d317aa6f2cc2d8337f5e72178a)
git cherry-pick -x $ancref..9f3ab14f104e81d317aa6f2cc2d8337f5e72178a

@jschmid1 jschmid1 changed the title change ai analytics refactor: change ai analytics Apr 15, 2024
@AndyZhang0707 AndyZhang0707 added the incomplete-cherry-pick A cherry-pick was incomplete and needs manual intervention label Apr 16, 2024
@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-12583-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-12583-to-master-to-upstream
git checkout -b cherry-pick-12583-to-master-to-upstream
ancref=$(git merge-base 1b920ca144aed6669b2b1dbe5003683b4fcd18d3 9f3ab14f104e81d317aa6f2cc2d8337f5e72178a)
git cherry-pick -x $ancref..9f3ab14f104e81d317aa6f2cc2d8337f5e72178a

@AntoineJac
Copy link
Contributor Author

@jschmid1 thanks could you please cherry pick and merge to EE? Thanks

@dndx
Copy link
Member

dndx commented Apr 16, 2024

@jschmid1 This squashed commit is missing the component in the scope, please watch out next time :)

@AntoineJac AntoineJac mentioned this pull request Apr 19, 2024
3 tasks
@locao locao changed the title refactor: change ai analytics refactor(*): change ai analytics Apr 19, 2024
@kikito kikito removed the incomplete-cherry-pick A cherry-pick was incomplete and needs manual intervention label Apr 23, 2024
gruceo pushed a commit that referenced this pull request Apr 23, 2024
This is a fixed of #12583
We need to refactor as many observability providers do not simplify ingest array. Here is a new nested format:

Current model is:
```json
  "ai": {
    "mistral": {
      "request_total_tokens": 648,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "mistral-tiny",
            "plugin_id": "1c783122-0223-4a03-9f1d-c71d2baab01e",
            "response_model": "mistral-tiny",
            "provider_name": "mistral"
          },
          "usage": {
            "prompt_token": 392,
            "total_tokens": 648,
            "completion_token": 256
          }
        }
      ],
      "request_prompt_tokens": 392,
      "request_completion_tokens": 256
    },
    "azure": {
      "request_total_tokens": 145,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "gpt-35-turbo",
            "plugin_id": "5df193be-47a3-4f1b-8c37-37e31af0568b",
            "response_model": "gpt-35-turbo",
            "provider_name": "azure"
          },
          "usage": {
            "prompt_token": 89,
            "total_tokens": 145,
            "completion_token": 56
          }
        }
      ],
      "request_prompt_tokens": 89,
      "request_completion_tokens": 56
    },
    "cohere": {
      "request_total_tokens": 149,
      "number_of_instances": 1,
      "instances": [
        {
          "meta": {
            "request_model": "command",
            "plugin_id": "30a63ab9-03c6-447a-9b25-195eb0acaeb2",
            "response_model": "command",
            "provider_name": "cohere"
          },
          "usage": {
            "prompt_token": 78,
            "total_tokens": 149,
            "completion_token": 71
          }
        }
      ],
      "request_prompt_tokens": 78,
      "request_completion_tokens": 71
    }
  }
```


New model if using several plugins:

```json
  "ai": {
    "ai-request-transformer": {
      "meta": {
        "request_model": "gpt-35-turbo",
        "provider_name": "azure",
        "response_model": "gpt-35-turbo",
        "plugin_id": "5df193be-47a3-4f1b-8c37-37e31af0568b"
      },
      "payload": {},
      "usage": {
        "prompt_token": 89,
        "completion_token": 56,
        "total_tokens": 145
      }
    },
    "ai-response-transformer": {
      "meta": {
        "request_model": "mistral-tiny",
        "provider_name": "mistral",
        "response_model": "mistral-tiny",
        "plugin_id": "1c783122-0223-4a03-9f1d-c71d2baab01e"
      },
      "payload": {},
      "usage": {
        "prompt_token": 168,
        "completion_token": 180,
        "total_tokens": 348
      }
    },
    "ai-proxy": {
      "meta": {
        "request_model": "gpt-35-turbo",
        "provider_name": "azure",
        "response_model": "gpt-35-turbo",
        "plugin_id": "546c3856-24b3-469a-bd6c-f6083babd2cd"
      },
      "payload": {},
      "usage": {
        "prompt_token": 28,
        "completion_token": 14,
        "total_tokens": 42
      }
    }
  },
```

* fix analytics with new format

* changelog

* fix

* fix json

* fix test

* fix test

* fix test

* fix(ai analytics): edit the format of the ai anayltics

* fix(ai analytics): edit the format of the ai anayltics

* fix: add testcase

* fix: add testcase

* fix: add testcase

* fix lint

* fix(ai-transformer): integration tests wrong response status

---------

Co-authored-by: Jack Tysoe <jack.tysoe@konghq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants