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

Improve token counting for messages with package #1577

Merged
merged 8 commits into from
May 21, 2024

Conversation

pamelafox
Copy link
Collaborator

Purpose

As I needed our token counting and message building functionality for another sample, I moved it to a separate package. That also gave me the opportunity to increase test coverage and receive community contributions for improving it, so it is more accurate than what's currently in this repo (notably, it can now count tools, which it was ignoring before).

You can see the interface for the other package here:
https://pypi.org/project/openai-messages-token-helper/

This PR deletes the code/tests from this repo and replaces them with equivalent functionality in that package.

Does this introduce a breaking change?

When developers merge from main and run the server, azd up, or azd deploy, will this produce an error?
If you're not sure, try it out on an old environment.

[ ] Yes
[X] No

Does this require changes to learn.microsoft.com docs?

This repository is referenced by this tutorial
which includes deployment, settings and usage instructions. If text or screenshot need to change in the tutorial,
check the box below and notify the tutorial author. A Microsoft employee can do this for you if you're an external contributor.

[ ] Yes
[X] No

Type of change

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[X] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

Code quality checklist

See CONTRIBUTING.md for more details.

  • The current tests all pass (python -m pytest).
  • I added tests that prove my fix is effective or that my feature works: There is already a test, test_chat_with_long_history, that effectively uses the functionality.
  • I ran python -m pytest --cov to verify 100% coverage of added lines
  • I ran python -m mypy to check for type errors
  • I either used the pre-commit hooks or ran ruff and black manually on my code.

@@ -254,6 +255,10 @@ async def compute_image_embedding(self, q: str):
return VectorizedQuery(vector=image_query_vector, k_nearest_neighbors=50, fields="imageEmbedding")

async def run(
self, messages: list[dict], stream: bool = False, session_state: Any = None, context: dict[str, Any] = {}
self,
messages: list[ChatCompletionMessageParam],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I updated types from dict to the more precise ChatCompletionMessageParam, otherwise we get errors, since a dict can have many more properties than that particular TypedDict.

{"role": ASSISTANT, "content": "Summarize Cryptocurrency Market Dynamics from last year"},
{"role": USER, "content": "What are my health plans?"},
{"role": ASSISTANT, "content": "Show available health plans"},
query_prompt_few_shots: list[ChatCompletionMessageParam] = [
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I also added types to our few shots

@pamelafox
Copy link
Collaborator Author

I've merged from main, this is ready for review.

Copy link
Collaborator

@mattgotteiner mattgotteiner left a comment

Choose a reason for hiding this comment

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

May want to add link to the source of the new package in the README

@pamelafox pamelafox merged commit 77303da into Azure-Samples:main May 21, 2024
9 checks passed
@pamelafox pamelafox deleted the llmhelper branch May 21, 2024 23:26
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