Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ollama Model Interaction

Ollama Model Interaction

Ollama provides multiple ways to interact with models, the most common of which is performing inference through the command line.

1. Command-line interaction

Interacting directly with a model through the command line is the simplest way.

Run a model

Use the ollama run command to start a model and enter interactive mode:

ollama run 

For example, we download the qwen3.5 model:

Example

ollama run qwen3.5

After starting, you can directly enter questions or instructions, and the model generates responses in real time.

>>> 你好,你能帮我写一段代码吗?

当然可以。但是首先我想知道您希望在哪种编程语言中实现这个功能(例如Python、JavaScript等)和要解决什么问题或者完成的任务是什么样的例子呢?这样我们可以为您提供更准确的

内容,同时也方便我帮助你写出最适合您的代码片段。





>>> 写一段 python hello world

Of course! Here is a simple "Hello, World!" program:



```python

print("Hello, World!")

This script outputs Hello, World!, and prints it to the console. This is the most basic Python Hello world example; Python is an interpreted, general-purpose programming language known for its simplicity and readability. It also allows users to insert variables and expressions into code to create complex behavior.



### Exit interactive mode



在交互模式下,输入 /bye 或按下 Ctrl+d 退出。



---



## 2. Single command interaction



如果您只需要模型生成一次响应,可以直接在命令行中传递输入。



### 使用管道输入



Pipe input to the model:



## 实例



echo



"你是谁?"



|



ollama run qwen3.5



  

输出结果如下:



![](https://www.runoob.com/wp-content/uploads/2025/02/runoob_1787975744569.png)



### 使用命令行参数



Pass input directly in the command line:



ollama run qwen3.5 "Python hello world code?"



输出结果如下:



In Python, "Hello World!" is usually this simple script:

print("Hello World!")

When you run this program, it outputs Hello, World. This is because the print() function prints the string "Hello, World" to the standard output device (stdout) - the information displayed on your screen (in this case, when running a Python script in a command-line terminal or similar tool, it writes directly to the console.



---



## 3. Multi-turn conversation



Ollama 支持多轮对话,模型可以记住上下文。



## 实例



>>>



你好,你能帮我写一段 Python 代码吗?



  

当然可以!请告诉我你需要实现什么功能。



  

  

>>>



我想写一个计算斐波那契数列的函数。



  

Okay, here is a simple Python function:



  

def



fibonacci



(



n



)



:



  

   



if



n



<=



1



:



  

       



return



n



  

   



else



:



  

       



return



fibonacci



(



n-



1



)



+ fibonacci



(



n-



2



)



  

---



## 4. 文件输入



可以将文件内容作为输入传递给模型。



假设 input.txt 文件内容为:



Python hello world code?



Use the content of input.txt as input:



ollama run qwen3.5 < input.txt



---



## 5. Custom Prompts



通过 Modelfile 定义自定义提示词或系统指令,使模型在交互中遵循特定规则。



### Create custom model



编写一个 Modelfile:



## 实例



FROM qwen3.5



  

SYSTEM



"你是一个编程助手,专门帮助用户编写代码。"



  

Then create a custom model:



ollama create runoob-coder -f ./Modelfile



Run custom model:



ollama run runoob-coder



---



## 6. Interaction Log



Ollama 会记录交互日志,方便调试和分析。



View logs:



ollama logs