Skip to content

Commit

Permalink
Add chat history exmaple
Browse files Browse the repository at this point in the history
Co-authored-by: fujitatomoya [email protected]
  • Loading branch information
ParthSareen committed Nov 21, 2024
1 parent 5258495 commit 679ebf9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ python3 <example>.py
- [chat.py](chat.py)
- [async-chat.py](async-chat.py)
- [chat-stream.py](chat-stream.py) - Streamed outputs
- [chat-with-history.py](chat-with-history.py) - Chat with model and maintain history of the conversation


### Generate - Generate text with a model
Expand Down
37 changes: 37 additions & 0 deletions examples/chat-with-history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from ollama import chat


messages = [
{
'role': 'user',
'content': 'Why is the sky blue?',
},
{
'role': 'assistant',
'content': "The sky is blue because of the way the Earth's atmosphere scatters sunlight.",
},
{
'role': 'user',
'content': 'What is the weather in Tokyo?',
},
{
'role': 'assistant',
'content': 'The weather in Tokyo is typically warm and humid during the summer months, with temperatures often exceeding 30°C (86°F). The city experiences a rainy season from June to September, with heavy rainfall and occasional typhoons. Winter is mild, with temperatures rarely dropping below freezing. The city is known for its high-tech and vibrant culture, with many popular tourist attractions such as the Tokyo Tower, Senso-ji Temple, and the bustling Shibuya district.',
},
]

while True:
user_input = input('Chat with history: ')
response = chat(
'llama3.1',
messages=messages
+ [
{'role': 'user', 'content': user_input},
],
)

# Add the response to the messages to maintain the history
messages.append(
{'role': 'assistant', 'content': response.message.content},
)
print(response.message.content + '\n')

0 comments on commit 679ebf9

Please sign in to comment.