1. 下载ollama,配置下载的模型保存路径
在电脑环境变量中配置如下:
2. 安装好ollama.exe后,Win+r输入cmd进入终端,输入ollama,输出如下信息:
C:\Users>ollama
Usage:
ollama [flags]
ollama [command]
Available Commands:
serve Start ollama
create Create a model from a Modelfile
show Show information for a model
run Run a model
pull Pull a model from a registry
push Push a model to a registry
list List models
ps List running models
cp Copy a model
rm Remove a model
help Help about any command
Flags:
-h, --help help for ollama
-v, --version Show version information
Use "ollama [command] --help" for more information about a command.
3.拉需要的模型。
例如:ollama run llama3.1
4.启动模型
例如:ollama run llama3.1
让lamma3.1写一个贪吃蛇小游戏,一次run成功。
import random
import tkinter as tk
class SnakeGame:
def __init__(self, width=800, height=600):
self.width = width
self.height = height
self.root = tk.Tk()
self.canvas = tk.Canvas(self.root, width=self.width, height=self.height)
self.canvas.pack()
self.snake = [(200, 200), (220, 200), (240, 200)]
self.direction = "right"
self.apple = (400, 300)
self.draw_snake()
self.update_score(0)
def draw_snake(self):
self.canvas.delete("all")
for x, y in self.snake:
self.canvas.create_rectangle(x-10, y-10, x+10, y+10, fill="#33CC33")
self.canvas.create_oval(self.apple[0]-10, self.apple[1]-10, self.apple[0]+10, self.apple[1]+10,
fill="#FF0000")
def update_score(self, score):
# Update the score label
self.canvas.delete("score_label")
self.canvas.create_text(10, 10, text=f"Score: {score}", font="Arial 12", anchor="nw")
def reset_game(self):
global direction
global snake
global apple
direction = "right"
snake = [(200, 200), (220, 200), (240, 200)]
apple = (random.randint(10, self.width-10) // 10 * 10, random.randint(10, self.height-10) // 10 * 10)
self.draw_snake()
self.update_score(0)
def update_game(self):
head = self.snake[-1]
if self.direction == "right":
new_head = (head[0] + 10, head[1])
elif self.direction == "left":
new_head = (head[0] - 10, head[1])
elif self.direction == "up":
new_head = (head[0], head[1] - 10)
else:
new_head = (head[0], head[1] + 10)
if (new_head[0] < 0 or new_head[0] >= self.width
or new_head[1] < 0 or new_head[1] >= self.height
or new_head in self.snake[:-1]):
print("Game Over!")
return
self.snake.append(new_head)
if new_head == self.apple:
self.update_score(1)
self.apple = (random.randint(10, self.width-10) // 10 * 10, random.randint(10, self.height-10) // 10 *
10)
else:
self.snake.pop(0)
self.draw_snake()
self.canvas.after(100, self.update_game) # Call update_game every 100ms
def key_press(self, event):
if event.keysym == "Right" and self.direction != "left":
self.direction = "right"
elif event.keysym == "Left" and self.direction != "right":
self.direction = "left"
elif event.keysym == "Up" and self.direction != "down":
self.direction = "up"
elif event.keysym == "Down" and self.direction != "up":
self.direction = "down"
def run(self):
self.root.bind("<Key>", self.key_press)
self.update_game()
self.root.mainloop()
game = SnakeGame()
game.run()
5. 选择导入自己微调量化后的模型
创建Modelfile文件,填写内容如下所示(需要换为自己文件路径):
FROM D:\\AI\\qwen-7b-output\\quantize_myqwen7b.gguf
从模型保存的目录进入终端输入命令:
ollama create 自定义模型名字 -f Modelfile