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!: use ollama python library instead of calling the API with requests #1059

Merged
merged 10 commits into from
Sep 7, 2024

Conversation

lbux
Copy link
Contributor

@lbux lbux commented Sep 6, 2024

Related Issues

Proposed Changes:

I got the idea to rewrite the library to use the Python library when I ran into tools issues. For some reason, it worked directly through through the api (using CURL) and using Ollama's Python library, but it did not work through Haystack's implementation of the api calls. After some troubleshooting, I rewrote the generator that I needed to use the Python library as a backend, and it worked.

If Ollama already handles the requests for us, why try to reinvent it? I believe using the Python library will allow for easier additions in the future and make the code easier to understand for those who want to modify and extend the generators/embedders.

How did you test it?

I slightly modified the tests due to a nuance with how the urls are handled (Ollama just wants the url and port as each generator/embedder calls its respective endpoint), so it is not 1:1 but pretty close. I also removed tests for components that we no longer need.

Notes for the reviewer

This is likely a breaking change due to the changes of url.

Some type checking tests will fail while I work out a workaround.

Checklist

@CLAassistant
Copy link

CLAassistant commented Sep 6, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added integration:ollama type:documentation Improvements or additions to documentation labels Sep 6, 2024
@anakin87
Copy link
Member

anakin87 commented Sep 7, 2024

This is a good idea!

I wanted to do the same thing...

The only reason we don't use Ollama python library is that it didn't exist when the integration was created.

@anakin87 anakin87 changed the title refactor: use ollama python library instead of api refactor!: use ollama python library instead of calling the API with requests Sep 7, 2024
@anakin87 anakin87 marked this pull request as ready for review September 7, 2024 18:51
@anakin87 anakin87 requested a review from a team as a code owner September 7, 2024 18:51
@anakin87 anakin87 requested review from davidsbatista and anakin87 and removed request for a team and davidsbatista September 7, 2024 18:51
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

I took the liberty to push some minor changes.

Thank you for your work!

@anakin87 anakin87 merged commit 79217e8 into deepset-ai:main Sep 7, 2024
8 checks passed
@lbux
Copy link
Contributor Author

lbux commented Sep 7, 2024

Awesome! Nice to see everything was wrapped up by the time I woke up

@lbux lbux deleted the ollama_rewrite branch September 7, 2024 20:47
@jaimevalero
Copy link

I do not know if this helps anyone, but in my app, I had to change:

# From this
pipe.add_component("llm_specialist", OllamaGenerator(model='llama3.1:8b', url='http://127.0.0.1:11434/api/generate'))
# To this
pipe.add_component("llm_specialist", OllamaGenerator(model='llama3.1:8b', url='http://127.0.0.1:11434'))

After updating the package from ollama-haystack-0.0.6 to ollama-haystack-1.0.0, to keep things working.

@lbux
Copy link
Contributor Author

lbux commented Sep 9, 2024

I do not know if this helps anyone, but in my app, I had to change:

# From this
pipe.add_component("llm_specialist", OllamaGenerator(model='llama3.1:8b', url='http://127.0.0.1:11434/api/generate'))
# To this
pipe.add_component("llm_specialist", OllamaGenerator(model='llama3.1:8b', url='http://127.0.0.1:11434'))

After updating the package from ollama-haystack-0.0.6 to ollama-haystack-1.0.0, to keep things working.

Yes, this is what I meant by breaking change. This should be the only thing impacted (provided that everything else was written correctly). I'm sure there is a way to "catch" these types of errors and if needed, I can try and implement it, but I want to know:

Did you find it easy to debug the error and correct the code?

@anakin87
Copy link
Member

anakin87 commented Sep 9, 2024

Thanks!
I updated the docs to reflect this change.

@jaimevalero
Copy link

Did you find it easy to debug the error and correct the code?

At first sight I tought it could be related to the new haystack pipeline logic because the exception was during the pipeline run.

Also, I discarded the ollama service by doing:

curl -s 127.0.0.1:11434/api/generate -d '{"model":"llama3.1:8b","prompt":"why is the sky blue?","stream":false}' | jq -r .response

As it was returning data, that discarded the ollama service itself.

Then, I started to "freeze", each package of the requirements.txt file in my app, one by one . Finally, I realized it was the ollama-haystack package and not the haystack-ai.

The issue was very well documented (big thank you).

Amnah199 pushed a commit that referenced this pull request Oct 2, 2024
…`requests` (#1059)

* switch from api to python library

* minor fixes, remove template as not applicable

* expect proper error

* fix typing issues

* client as internal attr

* lint

* remove requests from deps

* impr readme

---------

Co-authored-by: anakin87 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:ollama type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants