joe, Yesterday, we looked at how to write a JavaScript app that uses Ollama. Recently, we started to look at Python on this site and I figured that we better follow it up with how to write a Python app that uses Ollama. Just like with JavaScript, Ollama offers a Python library, so we are going to be using that for our examples. Also just like we did with the JavaScript demo, I am going to be using the generate endpoint instead of the chat endpoint. That keeps things simpler but I am going to explore the chat endpoint also at some point.
Install the Ollama Library
The first step is to run
pip3 install ollama
from the terminal. First, you need to create a virtual environment to isolate your project’s libraries from the global Python libraries.Basic CLI example
At this point, we can start writing code. When we used the web service earlier this week, we used the generate endpoint and provided
model
,prompt
, andstream
as parameters. We set thestream
parameter to false so that it would return a single response object instead of a stream of objects. When using the python library, thestream
parameter isn’t necessary because it returns a single response object by default. We still provide it with amodel
and aprompt
, though.If you run it from the terminal, the response will look familiar.
If you replace
print(output)
withprint(output['response'])
, you can more clearly see the important bits.Basic Web Application Example
The output is very similar to the node-fetch example from earlier this week. Last week, when we looked at how to dockerize a node app, we output an array as an unordered list. Let’s see if we can replicate that result using the output from Ollama.
If you
pip install flask
to install flask, you can host a simple HTTP page at port 8080 and with the magic of json.loads() and a for loop, you can build your unordered list.So, what does the output look like?
Every time you load the page, it makes a server-side API call to Ollama, gets a list of large cities in Wisconsin, and displays them on the website. The list is never the same (because of hallucinations) but that is another issue.
Have any questions, comments, etc? Please feel free to drop a comment, below.
https://jws.news/2024/how-to-write-a-python-app-that-uses-ollama/
Add comment