基于Ollama-Python的Ollama聊天窗口界面
By
the100s
at 2024-04-12 • 0人收藏 • 976人看过
本工具只是一个ollama的简易聊天窗口界面,
您需要自行安装ollama程序,请前往
https://ollama.com/
下载并安装.之后通过命令行窗口运行ollama serve命令保持后台程序待命.
再在另一个命令行窗口运行运行ollama run gemma:2b
以便下载一些模型(下载完后可关闭基于命令行的聊天窗口),
最后本工具才派上用场.




开发本聊天窗口的一个原因是,
基于命令行打字令人感到不快速.
另一个原因是aardio足以强大和胜任
致谢(不分排名):
Ollama
Ollama-python
Arrdio
远期(很远)计划目标:
一键懒人包
自带和自动运行ollama
可以增删模型
异步模式
废话少说,上代码和工程文件包(含编译程序,在dist目录里)
import win.ui;
import web.form;
/*DSG{{*/
mainForm = win.form(text="ollama aardio UI";right=741;bottom=898)
mainForm.add(
button={cls="button";text="发送(回车)";left=503;top=313;right=718;bottom=347;z=3};
combobox={cls="combobox";left=428;top=209;right=706;bottom=235;edge=1;items={};mode="dropdown";z=6};
edit={cls="edit";left=31;top=20;right=723;bottom=198;edge=1;hscroll=1;multiline=1;readonly=1;vscroll=1;z=1};
edit2={cls="edit";text="写一个js获取wmi printer get name的范例";left=31;top=249;right=722;bottom=294;edge=1;multiline=1;z=2};
edit3={cls="edit";left=323;top=304;right=475;bottom=353;edge=1;hide=1;hscroll=1;multiline=1;vscroll=1;z=5};
lbBrowser={cls="custom";text="自定义控件";left=28;top=366;right=718;bottom=861;z=4};
static={cls="static";text="输入区:";left=35;top=222;right=104;bottom=242;transparent=1;z=7};
static2={cls="static";text="选择模型:";left=361;top=215;right=427;bottom=233;transparent=1;z=8}
)
/*}}*/
//markdown form
var wb = web.form( mainForm.lbBrowser , , ,,true);
wb.noScriptErr = true;
wb.external={
saveHtmlFile = function(){
winform.btnSaveHtml.oncommand();
}
}
wb.html = /**
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<body>
<h3>AI回复内容显示区<h3>
</body>
</html>
**/
//记忆选项
import fsys.config;
config = fsys.config("/config/");
mainForm.bindConfig( config.mainForm,{
combobox = 1;
} );
import process.python;
var res=""
//list ollama installed models
var python = process.python.exec(`
import sys
import ollama
import json
response =ollama.list()
print(response)
`);
var info,err = python.json();
if(info=null){
win.msgboxErr("警告:ollama后台服务可能未运行 ; warnning: ollama serve may not be running.")
mainForm.close()
}
mainForm.edit.print(info || err);
//mainForm.edit.printf(type(info))//table
//mainForm.edit.printf(info["models"][1]["name"]) //codegemma:latest
var models_count=#info["models"]
if(models_count>0){
for(i=1;models_count;1){
mainForm.combobox.add(info["models"][i]["name"])
}
//mainForm.edit.printf(mainForm.combobox.selIndex)
if(config.mainForm.combobox){
mainForm.combobox.selIndex=config.mainForm.combobox
}
}else {
//no ollama serve running case
mainForm.edit.printf("警告:ollama后台服务可能未运行 ; warnning: ollama serve may not be running.")
}
mainForm.button.oncommand = function(id,event){
mainForm.button.disabled=true
mainForm.button.text="请稍后..."
var ldcode=string.load("pycode.txt")//方便编辑代码
ldcode=string.replace(ldcode,"@<1>",mainForm.combobox.selText)
ldcode=string.replace(ldcode,"@<2>",mainForm.edit2.text)
mainForm.edit.printf("======== 即将执行代码: ========")
mainForm.edit.printf(ldcode)
var pycode=ldcode
var python = process.python.exec(pycode)
mainForm.edit.printf("======== 请稍后,正在执行... ========")
mainForm.edit3.text=""
python.logResponse(mainForm.edit3)
python.onResponseEnd = function(){
import string.markdown;
var markdown = string.markdown();
wb.body.innerHTML = markdown.render( mainForm.edit3.text || "" );
mainForm.edit.printf("======== 执行结束,输出内容: ========")
mainForm.edit.printf(mainForm.edit3.text)
mainForm.button.disabled=false
mainForm.button.text="发送"
}
}
mainForm.edit2.onOk = function(){
mainForm.button.oncommand()
return true;
}
mainForm.saveConfigBeforeDestroy = function(){
config.mainForm.combobox=mainForm.combobox.selIndex
}
mainForm.show();
return win.loopMessage();另一个放在编译程序同级目录下的pycode.txt内容如下:
import ollama
response = ollama.chat(model='<1>', messages=[
{
'role': 'system',
'content': '<2>',
},
])
print(response['message']['content'])Ollama aardio chat UI 工程文件含编译程序.rar
https://www.123pan.com/s/mtEDVv-mtkhH.html提取码:100s
//上面的代码为何不是使用<?= mainform.edit2.text ?>这样的模板,因为我尝试过未如意
//本篇代码和工程文件代码使用开源MIT协议,任何人均可拿去能改进和主导继续开发.
7 个回复 | 最后更新于 2024-04-19
登录后方可回帖




AI是未来