├── ks.png ├── qwen.png ├── user.png ├── README.md ├── ksbanner.png ├── run.bat ├── llama.log ├── autoQwen.py ├── Qwen0.5-stChat_API_Black.py └── KS-AI_chat.py /ks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/KS-AI/main/ks.png -------------------------------------------------------------------------------- /qwen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/KS-AI/main/qwen.png -------------------------------------------------------------------------------- /user.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/KS-AI/main/user.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KS-AI 2 | test full portable llamafile Qwen05bchat with streamlit 3 | -------------------------------------------------------------------------------- /ksbanner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/KS-AI/main/ksbanner.png -------------------------------------------------------------------------------- /run.bat: -------------------------------------------------------------------------------- 1 | start cmd.exe /k .\qwen1_5-0_5b-chat-q8.exe --nobrowser 2 | .\python-3.10.11\python.exe -m streamlit run .\KS-AI_chat.py 3 | PAUSE -------------------------------------------------------------------------------- /llama.log: -------------------------------------------------------------------------------- 1 | warming up the model with an empty run 2 | 3 | llama server listening at http://127.0.0.1:8080 4 | llama server listening at http://10.88.200.75:8080 5 | 6 | warning: this OS doesn't support pledge() security 7 | -------------------------------------------------------------------------------- /autoQwen.py: -------------------------------------------------------------------------------- 1 | # Chat with an intelligent assistant in your terminal 2 | from openai import OpenAI 3 | import sys 4 | from time import sleep 5 | 6 | 7 | print("\033[95;3;6m") 8 | print("1. Waiting 10 seconds for the API to load...") 9 | sleep(10) 10 | print("2. Connecting to the LlamaFile API...") 11 | print("\033[0m") #reset all 12 | 13 | # Point to the local server 14 | client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed") 15 | history = [ 16 | {"role": "system", "content": "You are QWEN05, an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful. Always reply in the language of the instructions."}, 17 | {"role": "user", "content": "Hello, introduce yourself to someone opening this program for the first time. Be concise."}, 18 | ] 19 | print("\033[92;1m") 20 | while True: 21 | userinput = "" 22 | completion = client.chat.completions.create( 23 | model="local-model", # this field is currently unused 24 | messages=history, 25 | temperature=0.3, 26 | frequency_penalty = 1.6, 27 | max_tokens = 600, 28 | stream=True, 29 | ) 30 | 31 | new_message = {"role": "assistant", "content": ""} 32 | 33 | for chunk in completion: 34 | if chunk.choices[0].delta.content: 35 | print(chunk.choices[0].delta.content, end="", flush=True) 36 | new_message["content"] += chunk.choices[0].delta.content 37 | 38 | history.append(new_message) 39 | 40 | print("\033[1;30m") #dark grey 41 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 42 | print("\033[91;1m") #red 43 | lines = sys.stdin.readlines() 44 | for line in lines: 45 | userinput += line + "\n" 46 | if "quit!" in lines[0].lower(): 47 | print("\033[0mBYE BYE!") 48 | break 49 | history = [ 50 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 51 | ] 52 | history.append({"role": "user", "content": userinput}) 53 | print("\033[92;1m") -------------------------------------------------------------------------------- /Qwen0.5-stChat_API_Black.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | # Chat with an intelligent assistant in your terminal 3 | from openai import OpenAI 4 | from time import sleep 5 | import datetime 6 | 7 | def writehistory(filename,text): 8 | with open(filename, 'a', encoding='utf-8') as f: 9 | f.write(text) 10 | f.write('\n') 11 | f.close() 12 | 13 | #AVATARS 👷🐦 🥶🌀 14 | av_us = './user.png' #"🦖" #A single emoji, e.g. "🧑💻", "🤖", "🦖". Shortcodes are not supported. 15 | av_ass = './ks.png' 16 | 17 | # Set the webpage title 18 | st.set_page_config( 19 | page_title="Your LocalGPT with 🌀 Qwen-0.5", 20 | page_icon="🌀", 21 | layout="wide") 22 | 23 | # Create a header element 24 | mytitle = '
Your own LocalGPT with 🌀 Key Solution AI
' 25 | st.markdown(mytitle, unsafe_allow_html=True) 26 | #st.header("Your own LocalGPT with 🌀 h2o-danube-1.8b-chat") 27 | subtitle = 'Powerwed by Qwen, the best 0.5B chat model?
' 28 | st.markdown(subtitle, unsafe_allow_html=True) 29 | 30 | 31 | # create THE SESSIoN STATES 32 | if "logfilename" not in st.session_state: 33 | ## Logger file 34 | tstamp = datetime.datetime.now() 35 | tstamp = str(tstamp).replace(' ','_') 36 | tstamp = str(tstamp).replace(':','_') 37 | logfile = f'{tstamp[:-7]}_log.txt' 38 | st.session_state.logfilename = logfile 39 | sleep(2) 40 | #Write in the history the first 2 sessions 41 | writehistory(st.session_state.logfilename,f'Your own LocalGPT with 🌀 Qwen-0.5b-chat\n---\n🧠🫡: You are a helpful assistant.') 42 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?') 43 | 44 | if "len_context" not in st.session_state: 45 | st.session_state.len_context = 0 46 | 47 | if "limiter" not in st.session_state: 48 | st.session_state.limiter = 0 49 | 50 | if "bufstatus" not in st.session_state: 51 | st.session_state.bufstatus = "**:green[Good]**" 52 | 53 | if "temperature" not in st.session_state: 54 | st.session_state.temperature = 0.1 55 | 56 | if "maxlength" not in st.session_state: 57 | st.session_state.maxlength = 500 58 | 59 | # Point to the local server 60 | # Change localhost with the IP ADDRESS of the computer acting as a server 61 | # itmay be something like "http://192.168.1.52:8000/v1" 62 | client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed") 63 | 64 | # CREATE THE SIDEBAR 65 | with st.sidebar: 66 | st.image('./ksbanner.png', width=160) 67 | st.session_state.temperature = st.slider('Temperature:', min_value=0.0, max_value=1.0, value=0.1, step=0.02) 68 | st.session_state.limiter = st.slider('Turns:', min_value=7, max_value=17, value=12, step=1) 69 | st.session_state.maxlength = st.slider('Length reply:', min_value=150, max_value=1000, 70 | value=500, step=50) 71 | mytokens = st.markdown(f"""**Context turns** {st.session_state.len_context}""") 72 | st.markdown(f"Buffer status: {st.session_state.bufstatus}") 73 | st.markdown(f"**Logfile**: {st.session_state.logfilename}") 74 | btnClear = st.button("Clear History",type="primary", use_container_width=True) 75 | 76 | # We store the conversation in the session state. 77 | # This will be used to render the chat conversation. 78 | # We initialize it with the first message we want to be greeted with. 79 | if "messages" not in st.session_state: 80 | st.session_state.messages = [ 81 | {"role": "system", "content": "You are QWEN-KS, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",}, 82 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 83 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 84 | ] 85 | 86 | def clearHistory(): 87 | st.session_state.messages = [ 88 | {"role": "system", "content": "You are QWEN-KS, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",}, 89 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 90 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 91 | ] 92 | st.session_state.len_context = len(st.session_state.messages) 93 | if btnClear: 94 | clearHistory() 95 | st.session_state.len_context = len(st.session_state.messages) 96 | 97 | # We loop through each message in the session state and render it as 98 | # a chat message. 99 | for message in st.session_state.messages[1:]: 100 | if message["role"] == "user": 101 | with st.chat_message(message["role"],avatar=av_us): 102 | st.markdown(message["content"]) 103 | else: 104 | with st.chat_message(message["role"],avatar=av_ass): 105 | st.markdown(message["content"]) 106 | 107 | # We take questions/instructions from the chat input to pass to the LLM 108 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"): 109 | 110 | # Add our input to the session state 111 | st.session_state.messages.append( 112 | {"role": "user", "content": user_prompt} 113 | ) 114 | 115 | # Add our input to the chat window 116 | with st.chat_message("user", avatar=av_us): 117 | st.markdown(user_prompt) 118 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}') 119 | 120 | 121 | with st.chat_message("assistant",avatar=av_ass): 122 | with st.spinner("Thinking..."): 123 | response = '' 124 | conv_messages = [] 125 | st.session_state.len_context = len(st.session_state.messages) 126 | # Checking context window for the LLM, not for the chat history to be displayed 127 | if st.session_state.len_context > st.session_state.limiter: 128 | st.session_state.bufstatus = "**:red[Overflow]**" 129 | # this will keep 5 full turns into consideration 130 | x=st.session_state.limiter-5 131 | conv_messages.append(st.session_state.messages[0]) 132 | for i in range(0,x): 133 | conv_messages.append(st.session_state.messages[-x+i]) 134 | print(len(conv_messages)) 135 | completion = client.chat.completions.create( 136 | model="local-model", # this field is currently unused 137 | messages=conv_messages, 138 | temperature=st.session_state.temperature, 139 | frequency_penalty = 1.6, 140 | stop=['<|im_end|>','',"Your own LocalGPT with 🟠 Key Solution AI
' 25 | st.markdown(mytitle, unsafe_allow_html=True) 26 | #st.header("Your own LocalGPT with 🌀 h2o-danube-1.8b-chat") 27 | subtitle = 'Powerwed by Qwen, the best 0.5B chat model?
' 28 | st.markdown(subtitle, unsafe_allow_html=True) 29 | 30 | 31 | # create THE SESSIoN STATES 32 | if "logfilename" not in st.session_state: 33 | ## Logger file 34 | tstamp = datetime.datetime.now() 35 | tstamp = str(tstamp).replace(' ','_') 36 | tstamp = str(tstamp).replace(':','_') 37 | logfile = f'{tstamp[:-7]}_log.txt' 38 | st.session_state.logfilename = logfile 39 | sleep(2) 40 | #Write in the history the first 2 sessions 41 | writehistory(st.session_state.logfilename,f'Your own LocalGPT with 🌀 Qwen-0.5b-chat\n---\n🧠🫡: You are a helpful assistant.') 42 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?') 43 | 44 | if "len_context" not in st.session_state: 45 | st.session_state.len_context = 0 46 | 47 | if "limiter" not in st.session_state: 48 | st.session_state.limiter = 0 49 | 50 | if "bufstatus" not in st.session_state: 51 | st.session_state.bufstatus = "**:green[Good]**" 52 | 53 | if "temperature" not in st.session_state: 54 | st.session_state.temperature = 0.1 55 | 56 | if "maxlength" not in st.session_state: 57 | st.session_state.maxlength = 500 58 | 59 | # Point to the local server 60 | # Change localhost with the IP ADDRESS of the computer acting as a server 61 | # itmay be something like "http://192.168.1.52:8000/v1" 62 | client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed") 63 | 64 | # CREATE THE SIDEBAR 65 | with st.sidebar: 66 | st.image('./ksbanner.png', width=160) 67 | st.session_state.temperature = st.slider('Temperature:', min_value=0.0, max_value=1.0, value=0.1, step=0.02) 68 | st.session_state.limiter = st.slider('Turns:', min_value=7, max_value=17, value=12, step=1) 69 | st.session_state.maxlength = st.slider('Length reply:', min_value=150, max_value=1000, 70 | value=500, step=50) 71 | mytokens = st.markdown(f"""**Context turns** {st.session_state.len_context}""") 72 | st.markdown(f"Buffer status: {st.session_state.bufstatus}") 73 | st.markdown(f"**Logfile**: {st.session_state.logfilename}") 74 | btnClear = st.button("Clear History",type="primary", use_container_width=True) 75 | 76 | # We store the conversation in the session state. 77 | # This will be used to render the chat conversation. 78 | # We initialize it with the first message we want to be greeted with. 79 | if "messages" not in st.session_state: 80 | st.session_state.messages = [ 81 | {"role": "system", "content": "You are QWEN-KS, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",}, 82 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 83 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 84 | ] 85 | 86 | def clearHistory(): 87 | st.session_state.messages = [ 88 | {"role": "system", "content": "You are QWEN-KS, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",}, 89 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 90 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 91 | ] 92 | st.session_state.len_context = len(st.session_state.messages) 93 | if btnClear: 94 | clearHistory() 95 | st.session_state.len_context = len(st.session_state.messages) 96 | 97 | # We loop through each message in the session state and render it as 98 | # a chat message. 99 | for message in st.session_state.messages[1:]: 100 | if message["role"] == "user": 101 | with st.chat_message(message["role"],avatar=av_us): 102 | st.markdown(message["content"]) 103 | else: 104 | with st.chat_message(message["role"],avatar=av_ass): 105 | st.markdown(message["content"]) 106 | 107 | # We take questions/instructions from the chat input to pass to the LLM 108 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"): 109 | 110 | # Add our input to the session state 111 | st.session_state.messages.append( 112 | {"role": "user", "content": user_prompt} 113 | ) 114 | 115 | # Add our input to the chat window 116 | with st.chat_message("user", avatar=av_us): 117 | st.markdown(user_prompt) 118 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}') 119 | 120 | 121 | with st.chat_message("assistant",avatar=av_ass): 122 | message_placeholder = st.empty() 123 | with st.spinner("Thinking..."): 124 | response = '' 125 | conv_messages = [] 126 | st.session_state.len_context = len(st.session_state.messages) 127 | # Checking context window for the LLM, not for the chat history to be displayed 128 | if st.session_state.len_context > st.session_state.limiter: 129 | st.session_state.bufstatus = "**:red[Overflow]**" 130 | # this will keep 5 full turns into consideration 131 | x=st.session_state.limiter-5 132 | conv_messages.append(st.session_state.messages[0]) 133 | for i in range(0,x): 134 | conv_messages.append(st.session_state.messages[-x+i]) 135 | print(len(conv_messages)) 136 | full_response = "" 137 | completion = client.chat.completions.create( 138 | model="local-model", # this field is currently unused 139 | messages=conv_messages, 140 | temperature=st.session_state.temperature, 141 | frequency_penalty = 1.6, 142 | stop=['<|im_end|>','',"