1 頁 (共 1 頁)

DeepSeek + Python

發表於 : 2025-02-06, 19:24
dtchang
參考:
https://medium.com/@Mert.A/install-deep ... 59de3c160d

安裝 ollama

代碼: 選擇全部

pip install ollama
ollama run deepseek-r1:1.5b
範例程式:

代碼: 選擇全部

import ollama

model = "deepseek-r1:1.5b"
messages = []
while True:

    user_input = input("You: ")
    # Exit Condition
    if user_input.lower() == "exit":
        break
    # Response Initialization
    response = [
        {'role': 'user',
         'content': user_input},
        {'role': 'assistant',
         'content': ""},
    ]
    # Generating AI Responses
    for part in ollama.chat(model=model, messages=messages + [
        {"role": "user", "content": user_input}
    ], stream=True):
        # Processing AI Response
        print(part["message"]["content"], end='', flush=True)
        response[1]["content"] += part.message.content

    print()
    # Add the response to the messages to maintain the history
    messages += response