Add example for chat with history
Open
Beginner friendly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Description
Chat with history is perhaps the most common use case. In fact ollama run works like that. An example with that use case will be great for the newcomers. Here's a sample code:
import ollama
messages = []
def send(chat):
messages.append(
{
'role': 'user',
'content': chat,
}
)
stream = ollama.chat(model='mistral:instruct',
messages=messages,
stream=True,
)
response = ""
for chunk in stream:
part = chunk['message']['content']
print(part, end='', flush=True)
response = response + part
messages.append(
{
'role': 'assistant',
'content': response,
}
)
print("")
while True:
chat = input(">>> ")
if chat == "/exit":
break
elif len(chat) > 0:
send(chat)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing documentation or examples in the ollama-python repository and review how current chat examples are organized. Add a Python example based on the supplied chat-with-history flow, and consider the work done when newcomers can see how messages persist across turns and how to exit the interaction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100