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

Allow users to govern the ollama token context size #121803

Closed
wants to merge 5 commits into from

Conversation

Rudd-O
Copy link

@Rudd-O Rudd-O commented Jul 11, 2024

Proposed change

Allows the user of Ollama to specify the context token size, to ensure that models with
small default num_ctx can still be made to work with Home Assistant assist.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @synesthesiam, mind taking a look at this pull request as it has been labeled with an integration (ollama) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of ollama can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign ollama Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Rudd-O added a commit to Rudd-O/home-assistant.io that referenced this pull request Jul 11, 2024
@MartinHjelmare MartinHjelmare changed the title Allow users to govern the token context size. Allow users to govern the ollama token context size Jul 12, 2024
The default 2048 is useless with even a modest smart home — it causes the system prompt to be completely ignored.
@@ -81,6 +81,11 @@
CONF_MAX_HISTORY = "max_history"
DEFAULT_MAX_HISTORY = 20

CONF_NUM_CTX = "num_ctx"
DEFAULT_NUM_CTX = 2048
MAX_NUM_CTX = 65536

Choose a reason for hiding this comment

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

Newer model seem to support up to 128K, might be a good idea to change max to an even higher number (unless I'm misunderstanding something).

Copy link
Author

Choose a reason for hiding this comment

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

Perhaps. Mind suggesting a diff / patch in this PR? I would happily increase it.

Choose a reason for hiding this comment

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

Suggested change
MAX_NUM_CTX = 65536
MAX_NUM_CTX = 131072

@@ -251,6 +253,11 @@ async def async_process(
{"messages": message_history.messages},
)

options: ollama.Options | None = None
num_ctx = settings.get(CONF_NUM_CTX, DEFAULT_NUM_CTX)
if num_ctx != DEFAULT_NUM_CTX:
Copy link
Contributor

Choose a reason for hiding this comment

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

The default size in ollama is 2048. So when DEFAULT_NUM_CTX is set to 4096 here it doesn't actually set a num_ctx so the 4096 is ignored and it still uses 2048.

One option is:

  • Don't set DEFAULT_NUM_CTX at all so that the default is None and only pass the context size when its not None
  • If you're trying to also increase the default context size, then this needs a different approach

},
"data_description": {
"prompt": "Instruct how the LLM should respond. This can be a template.",
"keep_alive": "Duration in seconds for Ollama to keep model in memory. -1 = indefinite, 0 = never."
"keep_alive": "Duration in seconds for Ollama to keep model in memory. -1 = indefinite, 0 = never.",
"num_ctx": "Increase this if you have a complex smart home, or the LLM seems to ignore knowingly exposed devices."

Choose a reason for hiding this comment

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

Suggested change
"num_ctx": "Increase this if you have a complex smart home, or the LLM seems to ignore knowingly exposed devices."
"num_ctx": "Number of tokens a model can process. Higher values allow to handle a larger number of devices."

@frenck
Copy link
Member

frenck commented Aug 8, 2024

I've marked this PR, as changes are requested that need to be processed.
Please un-draft it once it is ready for review again by clicking the "Ready for review" button.

Thanks! 👍

../Frenck

Learn more about our pull request process.

@frenck frenck marked this pull request as draft August 8, 2024 16:56
@tannisroot
Copy link

@Rudd-O will you be able to implement the suggested changes to the PR? Thanks

@allenporter
Copy link
Contributor

Sent #124555 given this seems to have gone un responsive and i think it needs a prompt fix given there is somewhat of a regression here. Respectfully closing to handle in the other PR.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ollama context token size must be configurable
5 participants