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

implement get_num_tokens to use google's count_tokens function #10565

Merged
merged 8 commits into from
Oct 30, 2023

Conversation

hsuyuming
Copy link
Contributor

can get the correct token count instead of using gpt-2 model

Description:
Implement get_num_tokens within VertexLLM to use google's count_tokens function. (https://cloud.google.com/vertex-ai/docs/generative-ai/get-token-count). So we don't need to download gpt-2 model from huggingface, also when we do the mapreduce chain we can get correct token count.

Tag maintainer:
@lkuligin
Twitter handle:
My twitter: @abehsu1992626

can get the correct token count instead of using gpt-2 model
@vercel
Copy link

vercel bot commented Sep 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 30, 2023 8:27pm

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:improvement Medium size change to existing code to handle new use-cases labels Sep 14, 2023
@hsuyuming
Copy link
Contributor Author

hsuyuming commented Sep 14, 2023

@lkuligin @eyurtsev @hwchase17
Could you help me review this PR when you get time? Thank you.

@hsuyuming
Copy link
Contributor Author

@holtskinner can you help me review this PR, please?

@hsuyuming
Copy link
Contributor Author

@baskaryan Can you help me review this?

@hsuyuming
Copy link
Contributor Author

@lkuligin @eyurtsev @hwchase17 @holtskinner
Can you help me review this PR? Since this one have opened for few weeks.

@lkuligin
Copy link
Collaborator

lkuligin commented Oct 4, 2023

I don't think there's a need to add additional client and deal with it. You can use count_tokens method instead that is available for a TextModel itself (i.e., self.client.count_tokens for the Langchain implementation).

@hsuyuming
Copy link
Contributor Author

@lkuligin Please correct me if i am wrong.

it looks like TextModel didn't have count_tokens function.

>>> llm = VertexAI()
>>> "count_tokens" in dir(llm.client)
False
>>> llm.client.couont_tokens
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_PreviewTextGenerationModel' object has no attribute 'couont_tokens'

@hsuyuming
Copy link
Contributor Author

hsuyuming commented Oct 6, 2023

ohh, this function is release by google-cloud-aiplatform v1.34.0 version, which release on Oct6
https://github.com/googleapis/python-aiplatform/releases/tag/v1.34.0

@hsuyuming
Copy link
Contributor Author

@lkuligin I did some modifications base on your suggestions. Please help us review it again when you are available. Thank you. :)

@hsuyuming
Copy link
Contributor Author

Hi @lkuligin :
Can you help me review again? Thank you

@hsuyuming
Copy link
Contributor Author

@lkuligin Sorry to bother you. Can you review this change? please

@hsuyuming
Copy link
Contributor Author

@lkuligin @eyurtsev @hwchase17 @holtskinner
Can you help me review this PR? Since this one have opened for few weeks.

@holtskinner
Copy link
Contributor

I can't approve/merge this PR, but LGTM.

@@ -97,3 +100,26 @@ async def test_model_garden_agenerate() -> None:
output = await llm.agenerate(["What is the meaning of life?", "How much is 2+2"])
assert isinstance(output, LLMResult)
assert len(output.generations) == 2


def test_vertex_call_trigger_count_tokens(mocker) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nits: I'm not sure this qualifies as an integration test. Probably, for a integration test we should rather actually invoke a call to VertexAI and check that it returns results successfully, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @lkuligin:
For this testcase (test_vertex_call_trigger_count_tokens), i modify it, so it will call the underlying function directory, I also create the other testcase called "test_get_num_tokens_be_called_when_using_mapreduce_chain", because the token usage didn't be included from chain output. So it is hard to me to make sure if this function(count_tokens) will be called when user are using mapreduce method or not. This is why i decide to use marker to test this logic.

Please let me know your thoughts, or any suggestions i can make it more better. :)

@baskaryan baskaryan added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Oct 30, 2023
@baskaryan baskaryan merged commit 630ae24 into langchain-ai:master Oct 30, 2023
xieqihui pushed a commit to xieqihui/langchain that referenced this pull request Nov 21, 2023
…hain-ai#10565)

can get the correct token count instead of using gpt-2 model

**Description:** 
Implement get_num_tokens within VertexLLM to use google's count_tokens
function.
(https://cloud.google.com/vertex-ai/docs/generative-ai/get-token-count).
So we don't need to download gpt-2 model from huggingface, also when we
do the mapreduce chain we can get correct token count.

**Tag maintainer:** 
@lkuligin 
**Twitter handle:** 
My twitter: @abehsu1992626

---------

Co-authored-by: Bagatur <[email protected]>
hoanq1811 pushed a commit to hoanq1811/langchain that referenced this pull request Feb 2, 2024
…hain-ai#10565)

can get the correct token count instead of using gpt-2 model

**Description:** 
Implement get_num_tokens within VertexLLM to use google's count_tokens
function.
(https://cloud.google.com/vertex-ai/docs/generative-ai/get-token-count).
So we don't need to download gpt-2 model from huggingface, also when we
do the mapreduce chain we can get correct token count.

**Tag maintainer:** 
@lkuligin 
**Twitter handle:** 
My twitter: @abehsu1992626

---------

Co-authored-by: Bagatur <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. Ɑ: models Related to LLMs or chat model modules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants