├── testcpp ├── testingLoadCPP-smallModels.txt ├── smol-llama-banner.png ├── details.md ├── details.txt ├── 99.st-ChatLlamaCPP_streamSEL.py └── testCPP.py ├── DDGicon.png ├── logoDDG.png ├── searchddglogo.png ├── #JGYI0_searchDB.ddg ├── #JUHIS_searchDB.ddg ├── 03.st-SearchDDG2-text_requirements.txt ├── 99.testleadpickleDDG.py ├── 00.Gemma1-1-LCPP_stream.py ├── 00.consoleAPI_stream.py ├── 00.consoleLCPP_stream.py ├── SearchDDG2-text.py ├── 01.st-API-openAI_stream.py ├── 03.st-SearchDDG2-text.py ├── 03.st-FixOLD-DDG2_0.2.0.py ├── 01.st-ChatLlamaCPP_stream.py ├── 04.st-LoadDDG2_0.2.0.py ├── 03.st-SearchDDG2_0.2.0.py ├── what_is_NVIDIA_Deep_Learning_Institute.txt ├── 00.testSLM_stream.py ├── README.md └── JUHIS-log.txt /testcpp/testingLoadCPP-smallModels.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DDGicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/DDGicon.png -------------------------------------------------------------------------------- /logoDDG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/logoDDG.png -------------------------------------------------------------------------------- /searchddglogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/searchddglogo.png -------------------------------------------------------------------------------- /#JGYI0_searchDB.ddg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/#JGYI0_searchDB.ddg -------------------------------------------------------------------------------- /#JUHIS_searchDB.ddg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/#JUHIS_searchDB.ddg -------------------------------------------------------------------------------- /testcpp/smol-llama-banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/testcpp/smol-llama-banner.png -------------------------------------------------------------------------------- /03.st-SearchDDG2-text_requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/-LLM-Studies/main/03.st-SearchDDG2-text_requirements.txt -------------------------------------------------------------------------------- /99.testleadpickleDDG.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240627 Shanghai Time 08:00 3 | # adding streamlit interface to browse web documents 4 | # add 'search_query' : q, in the metadata 5 | # ⚠️ Segregating display datastructure from saved db 🗃️ datastructure 6 | # REV02 - fix old type of DDG 7 | import streamlit as st 8 | import os 9 | from rich.markdown import Markdown 10 | import warnings 11 | warnings.filterwarnings(action='ignore') 12 | from rich.console import Console 13 | from newspaper import Article 14 | import pickle 15 | from langchain.schema.document import Document 16 | console = Console(width=90) 17 | from io import StringIO 18 | from io import BytesIO 19 | import random 20 | import string 21 | import datetime 22 | import base64 23 | 24 | 25 | st.set_page_config(page_title="AI powered web serach", layout="wide",page_icon='📱') 26 | 27 | if "keyDDGfile" not in st.session_state: 28 | st.session_state.keyDDGfile = 1 29 | 30 | 31 | st.title("AI powered Web Document Search") 32 | st.write('Using Newspaper3k and DuckDuckGo LangChain wrapper') 33 | st.divider() 34 | # Upload the audio file 35 | file1 = st.sidebar.file_uploader("Upload a text document", 36 | type=["ddg"],accept_multiple_files=False, 37 | key=st.session_state.keyDDGfile) 38 | st.divider() 39 | if file1: 40 | console.print(f'[blink2 orange1]Loading...') 41 | pkl_file = open(file1.name, 'rb') 42 | st.write(type(pkl_file)) 43 | data_docs = pickle.load(pkl_file) 44 | st.write(type(data_docs)) 45 | pkl_file.close() 46 | st.write(data_docs) -------------------------------------------------------------------------------- /00.Gemma1-1-LCPP_stream.py: -------------------------------------------------------------------------------- 1 | # Chat with an intelligent assistant in your terminal BARTOWSKI\gemma-1.1-2b-it-Q8_0.gguf 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 | from llama_cpp import Llama 10 | llm = Llama( 11 | model_path='models/gemma-1.1-2b-it-Q4_K_M.gguf', 12 | n_gpu_layers=0, 13 | temperature=0.1, 14 | top_p = 0.5, 15 | n_ctx=8192, 16 | max_tokens=600, 17 | repeat_penalty=1.7, 18 | stop=["''","<|im_end|>","Instruction:","### Instruction:","###",""], 19 | verbose=True, 20 | ) 21 | print("2. Model BARTOWSKI\gemma-1.1-2b-it-Q4_K_M.gguf loaded with LlamaCPP...") 22 | print("\033[0m") #reset all 23 | 24 | history = [] 25 | print("\033[92;1m") 26 | counter = 1 27 | while True: 28 | if counter > 5: 29 | history = [] 30 | userinput = "" 31 | print("\033[1;30m") #dark grey 32 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 33 | print("\033[91;1m") #red 34 | lines = sys.stdin.readlines() 35 | for line in lines: 36 | userinput += line + "\n" 37 | if "quit!" in lines[0].lower(): 38 | print("\033[0mBYE BYE!") 39 | break 40 | history.append({"role": "user", "content": userinput}) 41 | print("\033[92;1m") 42 | 43 | new_message = {"role": "assistant", "content": ""} 44 | 45 | full_response = "" 46 | fisrtround = 0 47 | for chunk in llm.create_chat_completion( 48 | messages=history, 49 | temperature=0.25, 50 | repeat_penalty= 1.6, 51 | stop=['<|im_end|>','',""], 52 | max_tokens=600, 53 | stream=True,): 54 | try: 55 | if chunk["choices"][0]["delta"]["content"]: 56 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 57 | full_response += chunk["choices"][0]["delta"]["content"] 58 | except: 59 | pass 60 | new_message["content"] = full_response 61 | history.append(new_message) 62 | counter += 1 -------------------------------------------------------------------------------- /00.consoleAPI_stream.py: -------------------------------------------------------------------------------- 1 | # Chat with an intelligent assistant in your terminal model/stablelm-2-zephyr-1_6b-Q8_0.gguf 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 KS-AI, 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": "Hi there!"}, 18 | {"role": "assistant", "content": "Hi, I am KS-AI your intelligent assistant. How can I help you today?"} 19 | ] 20 | print("\033[92;1m") 21 | counter = 1 22 | while True: 23 | if counter > 5: 24 | history = [ 25 | {"role": "system", "content": "You are KS-AI, an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful. Always reply in the language of the instructions."}, 26 | {"role": "user", "content": "Hi there!"}, 27 | {"role": "assistant", "content": "Hi, I am KS-AI your intelligent assistant. How can I help you today?"} 28 | ] 29 | userinput = "" 30 | print("\033[1;30m") #dark grey 31 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 32 | print("\033[91;1m") #red 33 | lines = sys.stdin.readlines() 34 | for line in lines: 35 | userinput += line + "\n" 36 | if "quit!" in lines[0].lower(): 37 | print("\033[0mBYE BYE!") 38 | break 39 | history.append({"role": "user", "content": userinput}) 40 | print("\033[92;1m") 41 | 42 | completion = client.chat.completions.create( 43 | model="local-model", # this field is currently unused 44 | messages=history, 45 | temperature=0.3, 46 | frequency_penalty = 1.6, 47 | max_tokens = 600, 48 | stream=True, 49 | stop=['<|endoftext|>',''] 50 | ) 51 | 52 | new_message = {"role": "assistant", "content": ""} 53 | 54 | for chunk in completion: 55 | if chunk.choices[0].delta.content: 56 | print(chunk.choices[0].delta.content, end="", flush=True) 57 | new_message["content"] += chunk.choices[0].delta.content 58 | history.append(new_message) 59 | counter += 1 -------------------------------------------------------------------------------- /00.consoleLCPP_stream.py: -------------------------------------------------------------------------------- 1 | # Chat with an intelligent assistant in your terminal model/stablelm-2-zephyr-1_6b-Q8_0.gguf 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 | from llama_cpp import Llama 10 | llm = Llama( 11 | model_path='models/dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf', 12 | n_gpu_layers=0, 13 | temperature=0.1, 14 | top_p = 0.5, 15 | n_ctx=8192, 16 | max_tokens=600, 17 | repeat_penalty=1.7, 18 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 19 | verbose=False, 20 | ) 21 | print("2. Model dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf loaded with LlamaCPP...") 22 | print("\033[0m") #reset all 23 | 24 | history = [ 25 | {"role": "system", "content": "You are KS-AI, an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful. Always reply in the language of the instructions."}, 26 | {"role": "user", "content": "Hi there!"}, 27 | {"role": "assistant", "content": "Hi, I am KS-AI your intelligent assistant. How can I help you today?"} 28 | ] 29 | print("\033[92;1m") 30 | counter = 1 31 | while True: 32 | if counter > 5: 33 | history = [ 34 | {"role": "system", "content": "You are KS-AI, an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful. Always reply in the language of the instructions."}, 35 | {"role": "user", "content": "Hi there!"}, 36 | {"role": "assistant", "content": "Hi, I am KS-AI your intelligent assistant. How can I help you today?"} 37 | ] 38 | userinput = "" 39 | print("\033[1;30m") #dark grey 40 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 41 | print("\033[91;1m") #red 42 | lines = sys.stdin.readlines() 43 | for line in lines: 44 | userinput += line + "\n" 45 | if "quit!" in lines[0].lower(): 46 | print("\033[0mBYE BYE!") 47 | break 48 | history.append({"role": "user", "content": userinput}) 49 | print("\033[92;1m") 50 | 51 | new_message = {"role": "assistant", "content": ""} 52 | 53 | full_response = "" 54 | fisrtround = 0 55 | for chunk in llm.create_chat_completion( 56 | messages=history, 57 | temperature=0.25, 58 | repeat_penalty= 1.6, 59 | stop=['<|im_end|>','',""], 60 | max_tokens=600, 61 | stream=True,): 62 | try: 63 | if chunk["choices"][0]["delta"]["content"]: 64 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 65 | full_response += chunk["choices"][0]["delta"]["content"] 66 | except: 67 | pass 68 | new_message["content"] = full_response 69 | history.append(new_message) 70 | counter += 1 -------------------------------------------------------------------------------- /SearchDDG2-text.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240619 Shanghai Time 14:00 3 | 4 | from rich.markdown import Markdown 5 | import warnings 6 | warnings.filterwarnings(action='ignore') 7 | import datetime 8 | from rich.console import Console 9 | from newspaper import Article 10 | import pickle 11 | from langchain.schema.document import Document 12 | console = Console(width=90) 13 | 14 | def writehistory(filename,text): 15 | with open(filename, 'a', encoding='utf-8') as f: 16 | f.write(text) 17 | f.write('\n') 18 | f.close() 19 | 20 | 21 | 22 | import nltk 23 | nltk.download('punkt') 24 | 25 | import os 26 | #from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 27 | # https://pypi.org/project/duckduckgo-search 28 | from langchain_community.utilities import DuckDuckGoSearchAPIWrapper 29 | # https://api.python.langchain.com/en/latest/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html 30 | wrapper = DuckDuckGoSearchAPIWrapper(region='us-en', time="y", max_results=10) #time parameter Options: d, w, m, y 31 | 32 | console.print(f'[bold red1]What do you want to search?') 33 | research = console.input(f'[bright_green]> ') 34 | console.print(90*'=') 35 | rawdb = wrapper.results(research,max_results=5) 36 | 37 | finter = research.replace(' ','_') 38 | filename = f'{finter}.txt' 39 | 40 | docdocs = [] 41 | st = 1 42 | numofdocs = len(rawdb) 43 | for items in rawdb: 44 | url = items["link"] 45 | try: #useful if the url is no reachable 46 | article = Article(url) 47 | article.download() 48 | article.parse() 49 | article.nlp() 50 | kw = [] 51 | #we merge nltk keywords and meta webpage keywords 52 | for i in article.keywords+article.meta_keywords: 53 | if i == '': #no blck yeywords for us 54 | pass 55 | else: 56 | kw.append(i) 57 | if article.text == '': #sometimes there is no text to parse. so we use the snippet 58 | text = f"""======================================= 59 | {items['snippet']} 60 | --------------------- 61 | 'source': {items['link']}, 62 | 'title': {items['title']}, 63 | 'snippet': {items['snippet']}, 64 | 'author':{article.authors}, 65 | 'keywords':{kw}, 66 | 'meta_description':{article.meta_description}, 67 | 'meta_img':{article.meta_img}, 68 | 'top_image':{article.top_image}, 69 | 'publish_date':{article.publish_date}, 70 | 'summary':{article.summary} 71 | ---------------------------------------------------""" 72 | writehistory(filename,text) 73 | console.print(text) 74 | else: 75 | text = f"""======================================= 76 | {article.text} 77 | --------------------- 78 | 'source': {items['link']}, 79 | 'title': {items['title']}, 80 | 'snippet': {items['snippet']}, 81 | 'author':{article.authors}, 82 | 'keywords':{kw}, 83 | 'meta_description':{article.meta_description}, 84 | 'meta_img':{article.meta_img}, 85 | 'top_image':{article.top_image}, 86 | 'publish_date':{article.publish_date}, 87 | 'summary':{article.summary} 88 | ---------------------------------------------------""" 89 | writehistory(filename,text) 90 | console.print(text) 91 | console.print(f'Prepared Ducment n.{st} out of {numofdocs}') 92 | st +=1 93 | except: 94 | pass 95 | 96 | 97 | console.print(Markdown("> SEARCH COMPLETED...")) 98 | console.print(" - ") 99 | 100 | 101 | -------------------------------------------------------------------------------- /testcpp/details.md: -------------------------------------------------------------------------------- 1 | ### Details for small models 2 | 3 | ``` 4 | 5 | ──────────────────────────────────────── Pythia-31M-Chat-v1 ──────────────────────────────────────── 6 | Model File: Pythia-31M-Chat-v1.Q8_0.gguf 7 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 8 | Training Context Window: 2048 9 | Stop Tokens: ['<|endoftext|>'] 10 | This model has Chat Template? True 11 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 12 | PROMPT FORMAT: 13 | 14 | 15 | <|im_start|>system 16 | {system_message}<|im_end|> 17 | <|im_start|>user 18 | {user_message}<|im_end|> 19 | <|im_start|>assistant 20 | 21 | 22 | Recommended inference parameters 23 | penalty_alpha: 0.5 24 | top_k: 2 25 | repetition_penalty: 1.0016 26 | 27 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 28 | Additional info: 29 | 30 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Pythia-31M-Chat-v1/tree/main 31 | Original Repo: https://huggingface.co/Felladrin/Pythia-31M-Chat-v1 32 | Model architecture: gptneox 33 | Number of parameters: 31M 34 | 35 | ==================================================================================================== 36 | 37 | 38 | 39 | ───────────────────────────────────────── Aira-2-124M-DPO ────────────────────────────────────────── 40 | Model File: Aira-2-124M-DPO.Q8_0.gguf 41 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 42 | Training Context Window: 1024 43 | Stop Tokens: ['<|endofcompletion|>'] 44 | This model has Chat Template? False 45 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 46 | PROMPT FORMAT: 47 | 48 | 49 | <|startofinstruction|>What is a language model?<|endofinstruction|> 50 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 51 | 52 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 53 | Additional info: 54 | 55 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Aira-2-124M-DPO 56 | Original Repo: https://huggingface.co/nicholasKluge/Aira-2-124M-DPO 57 | Model architecture: gpt2 58 | Number of parameters: 124M 59 | 60 | ==================================================================================================== 61 | 62 | 63 | 64 | ───────────────────────────────────── smol-llama-101m-chat-v1 ────────────────────────────────────── 65 | Model File: smol-llama-101m-chat-v1.q8_0.gguf 66 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 67 | Training Context Window: 1024 68 | Stop Tokens: [''] 69 | This model has Chat Template? True 70 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 71 | PROMPT FORMAT: 72 | 73 | 74 | <|im_start|>system 75 | {system_message}<|im_end|> 76 | <|im_start|>user 77 | {user_message}<|im_end|> 78 | <|im_start|>assistant 79 | 80 | Recommended inference parameters 81 | penalty_alpha: 0.5 82 | top_k: 4 83 | repetition_penalty: 1.105 84 | 85 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 86 | Additional info: 87 | 88 | GGUF Repo: https://huggingface.co/afrideva/Smol-Llama-101M-Chat-v1-GGUF 89 | Original Repo: https://huggingface.co/Felladrin/Smol-Llama-101M-Chat-v1 90 | Model architecture: llama 91 | Number of parameters: 101M 92 | 93 | ==================================================================================================== 94 | 95 | 96 | 97 | 98 | ─────────────────────────────────────────── Aira-2-355M ──────────────────────────────────────────── 99 | Model File: Aira-2-355M.Q6_K.gguf 100 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 101 | Training Context Window: 1024 102 | Stop Tokens: ['<|endoftext|>'] 103 | This model has Chat Template? False 104 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 105 | PROMPT FORMAT: 106 | 107 | 108 | <|startofinstruction|>What is a language model?<|endofinstruction|> 109 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 110 | 111 | 112 | Recommended inference parameters 113 | penalty_alpha: 0.5 114 | top_k: 2 115 | repetition_penalty: 1.0016 116 | 117 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 118 | Additional info: 119 | 120 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Aira-2-355M/tree/main 121 | Original Repo: https://huggingface.co/nicholasKluge/Aira-2-355M 122 | Model architecture: gpt2 123 | Number of parameters: 355M 124 | 125 | ==================================================================================================== 126 | 127 | 128 | 129 | 130 | ────────────────────────────────────── stablelm-2-zephyr-1_6b ────────────────────────────────────── 131 | Model File: stablelm-2-zephyr-1_6b-Q5_K_M.gguf 132 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 133 | Training Context Window: 4096 134 | Stop Tokens: ['<|endoftext|>'] 135 | This model has Chat Template? True 136 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 137 | PROMPT FORMAT: 138 | 139 | 140 | <|user|> 141 | {prompt}<|endoftext|> 142 | <|assistant|> 143 | 144 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 145 | Additional info: 146 | 147 | GGUF Repo: https://huggingface.co/second-state/stablelm-2-zephyr-1.6b-GGUF/tree/main 148 | Original Repo: https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b 149 | Model architecture: stablelm 150 | Number of parameters: 1.6b 151 | 152 | ==================================================================================================== 153 | 154 | 155 | 156 | ────────────────────────────────────── h2o-danube2-1.8b-chat ─────────────────────────────────────── 157 | Model File: h2o-danube2-1.8b-chat-Q5_K_M.gguf 158 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 159 | Training Context Window: 8192 160 | Stop Tokens: [''] 161 | This model has Chat Template? True 162 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 163 | PROMPT FORMAT: 164 | 165 | 166 | <|prompt|>Why is drinking water so healthy?<|answer|> 167 | 168 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 169 | Additional info: 170 | 171 | GGUF Repo: https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF 172 | Original Repo: https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF 173 | Model architecture: llama 174 | Number of parameters: 1.8b 175 | 176 | ==================================================================================================== 177 | 178 | 179 | 180 | 181 | 182 | ``` 183 | -------------------------------------------------------------------------------- /testcpp/details.txt: -------------------------------------------------------------------------------- 1 | 2 | ──────────────────────────────────────── Pythia-31M-Chat-v1 ──────────────────────────────────────── 3 | Model File: Pythia-31M-Chat-v1.Q8_0.gguf 4 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 5 | Training Context Window: 2048 6 | Stop Tokens: ['<|endoftext|>'] 7 | This model has Chat Template? True 8 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 9 | PROMPT FORMAT: 10 | 11 | 12 | <|im_start|>system 13 | {system_message}<|im_end|> 14 | <|im_start|>user 15 | {user_message}<|im_end|> 16 | <|im_start|>assistant 17 | 18 | 19 | Recommended inference parameters 20 | penalty_alpha: 0.5 21 | top_k: 2 22 | repetition_penalty: 1.0016 23 | 24 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 25 | Additional info: 26 | 27 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Pythia-31M-Chat-v1/tree/main 28 | Original Repo: https://huggingface.co/Felladrin/Pythia-31M-Chat-v1 29 | Model architecture: gptneox 30 | Number of parameters: 31M 31 | 32 | ==================================================================================================== 33 | 34 | 35 | 36 | ───────────────────────────────────────── Aira-2-124M-DPO ────────────────────────────────────────── 37 | Model File: Aira-2-124M-DPO.Q8_0.gguf 38 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 39 | Training Context Window: 1024 40 | Stop Tokens: ['<|endofcompletion|>'] 41 | This model has Chat Template? False 42 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 43 | PROMPT FORMAT: 44 | 45 | 46 | <|startofinstruction|>What is a language model?<|endofinstruction|> 47 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 48 | 49 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 50 | Additional info: 51 | 52 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Aira-2-124M-DPO 53 | Original Repo: https://huggingface.co/nicholasKluge/Aira-2-124M-DPO 54 | Model architecture: gpt2 55 | Number of parameters: 124M 56 | 57 | ==================================================================================================== 58 | 59 | 60 | 61 | ───────────────────────────────────── smol-llama-101m-chat-v1 ────────────────────────────────────── 62 | Model File: smol-llama-101m-chat-v1.q8_0.gguf 63 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 64 | Training Context Window: 1024 65 | Stop Tokens: [''] 66 | This model has Chat Template? True 67 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 68 | PROMPT FORMAT: 69 | 70 | 71 | <|im_start|>system 72 | {system_message}<|im_end|> 73 | <|im_start|>user 74 | {user_message}<|im_end|> 75 | <|im_start|>assistant 76 | 77 | Recommended inference parameters 78 | penalty_alpha: 0.5 79 | top_k: 4 80 | repetition_penalty: 1.105 81 | 82 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 83 | Additional info: 84 | 85 | GGUF Repo: https://huggingface.co/afrideva/Smol-Llama-101M-Chat-v1-GGUF 86 | Original Repo: https://huggingface.co/Felladrin/Smol-Llama-101M-Chat-v1 87 | Model architecture: llama 88 | Number of parameters: 101M 89 | 90 | ==================================================================================================== 91 | 92 | 93 | 94 | 95 | ─────────────────────────────────────────── Aira-2-355M ──────────────────────────────────────────── 96 | Model File: Aira-2-355M.Q6_K.gguf 97 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 98 | Training Context Window: 1024 99 | Stop Tokens: ['<|endoftext|>'] 100 | This model has Chat Template? False 101 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 102 | PROMPT FORMAT: 103 | 104 | 105 | <|startofinstruction|>What is a language model?<|endofinstruction|> 106 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 107 | 108 | 109 | Recommended inference parameters 110 | penalty_alpha: 0.5 111 | top_k: 2 112 | repetition_penalty: 1.0016 113 | 114 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 115 | Additional info: 116 | 117 | GGUF Repo: https://huggingface.co/Felladrin/gguf-Aira-2-355M/tree/main 118 | Original Repo: https://huggingface.co/nicholasKluge/Aira-2-355M 119 | Model architecture: gpt2 120 | Number of parameters: 355M 121 | 122 | ==================================================================================================== 123 | 124 | 125 | 126 | 127 | ────────────────────────────────────── stablelm-2-zephyr-1_6b ────────────────────────────────────── 128 | Model File: stablelm-2-zephyr-1_6b-Q5_K_M.gguf 129 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 130 | Training Context Window: 4096 131 | Stop Tokens: ['<|endoftext|>'] 132 | This model has Chat Template? True 133 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 134 | PROMPT FORMAT: 135 | 136 | 137 | <|user|> 138 | {prompt}<|endoftext|> 139 | <|assistant|> 140 | 141 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 142 | Additional info: 143 | 144 | GGUF Repo: https://huggingface.co/second-state/stablelm-2-zephyr-1.6b-GGUF/tree/main 145 | Original Repo: https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b 146 | Model architecture: stablelm 147 | Number of parameters: 1.6b 148 | 149 | ==================================================================================================== 150 | 151 | 152 | 153 | ────────────────────────────────────── h2o-danube2-1.8b-chat ─────────────────────────────────────── 154 | Model File: h2o-danube2-1.8b-chat-Q5_K_M.gguf 155 | ───────────────────────────────────────────── Details ────────────────────────────────────────────── 156 | Training Context Window: 8192 157 | Stop Tokens: [''] 158 | This model has Chat Template? True 159 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 160 | PROMPT FORMAT: 161 | 162 | 163 | <|prompt|>Why is drinking water so healthy?<|answer|> 164 | 165 | ──────────────────────────────────────────────────────────────────────────────────────────────────── 166 | Additional info: 167 | 168 | GGUF Repo: https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF 169 | Original Repo: https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF 170 | Model architecture: llama 171 | Number of parameters: 1.8b 172 | 173 | ==================================================================================================== 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /01.st-API-openAI_stream.py: -------------------------------------------------------------------------------- 1 | # https://github.com/fabiomatricardi/Gemma2B-ChatAssistant 2 | import streamlit as st 3 | # Chat with an intelligent assistant in your terminal 4 | from openai import OpenAI 5 | from time import sleep 6 | import datetime 7 | import random 8 | import string 9 | 10 | def writehistory(filename,text): 11 | with open(filename, 'a', encoding='utf-8') as f: 12 | f.write(text) 13 | f.write('\n') 14 | f.close() 15 | 16 | #AVATARS 👷🐦 🥶🌀 17 | av_us = '🧑‍💻' #"🦖" #A single emoji, e.g. "🧑‍💻", "🤖", "🦖". Shortcodes are not supported. 18 | av_ass = '🤖' 19 | 20 | # Set the webpage title 21 | st.set_page_config( 22 | page_title="Your LocalGPT with 🟠 Qwen-0.5", 23 | page_icon="🟠", 24 | layout="wide") 25 | 26 | # Create a header element 27 | mytitle = '

Your own LocalGPT with 🟠 Key Solution AI

' 28 | st.markdown(mytitle, unsafe_allow_html=True) 29 | #st.header("Your own LocalGPT with 🌀 h2o-danube-1.8b-chat") 30 | subtitle = '

Powerwed by Qwen, the best 0.5B chat model?

' 31 | st.markdown(subtitle, unsafe_allow_html=True) 32 | 33 | 34 | def genRANstring(n): 35 | """ 36 | n = int number of char to randomize 37 | """ 38 | N = n 39 | res = ''.join(random.choices(string.ascii_uppercase + 40 | string.digits, k=N)) 41 | return res 42 | 43 | # create THE SESSIoN STATES 44 | if "logfilename" not in st.session_state: 45 | ## Logger file 46 | logfile = f'{genRANstring(5)}_log.txt' 47 | st.session_state.logfilename = logfile 48 | #Write in the history the first 2 sessions 49 | writehistory(st.session_state.logfilename,f'{str(datetime.datetime.now())}\n\nYour own LocalGPT with 🌀 Qwen-0.5b-chat\n---\n🧠🫡: You are a helpful assistant.') 50 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?') 51 | 52 | if "len_context" not in st.session_state: 53 | st.session_state.len_context = 0 54 | 55 | if "limiter" not in st.session_state: 56 | st.session_state.limiter = 0 57 | 58 | if "bufstatus" not in st.session_state: 59 | st.session_state.bufstatus = "**:green[Good]**" 60 | 61 | if "temperature" not in st.session_state: 62 | st.session_state.temperature = 0.1 63 | 64 | if "maxlength" not in st.session_state: 65 | st.session_state.maxlength = 500 66 | 67 | # Point to the local server 68 | # Change localhost with the IP ADDRESS of the computer acting as a server 69 | # itmay be something like "http://192.168.1.52:8000/v1" 70 | client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed", organization='SelectedModel') 71 | 72 | # CREATE THE SIDEBAR 73 | with st.sidebar: 74 | st.image('logo.png', use_column_width=True) 75 | st.session_state.temperature = st.slider('Temperature:', min_value=0.0, max_value=1.0, value=0.1, step=0.02) 76 | st.session_state.limiter = st.slider('Turns:', min_value=7, max_value=17, value=12, step=1) 77 | st.session_state.maxlength = st.slider('Length reply:', min_value=150, max_value=1000, 78 | value=500, step=50) 79 | mytokens = st.markdown(f"""**Context turns** {st.session_state.len_context}""") 80 | st.markdown(f"Buffer status: {st.session_state.bufstatus}") 81 | st.markdown(f"**Logfile**: {st.session_state.logfilename}") 82 | btnClear = st.button("Clear History",type="primary", use_container_width=True) 83 | 84 | # We store the conversation in the session state. 85 | # This will be used to render the chat conversation. 86 | # We initialize it with the first message we want to be greeted with. 87 | if "messages" not in st.session_state: 88 | st.session_state.messages = [ 89 | {"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.",}, 90 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 91 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 92 | ] 93 | 94 | def clearHistory(): 95 | st.session_state.messages = [ 96 | {"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.",}, 97 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 98 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 99 | ] 100 | st.session_state.len_context = len(st.session_state.messages) 101 | if btnClear: 102 | clearHistory() 103 | st.session_state.len_context = len(st.session_state.messages) 104 | 105 | # We loop through each message in the session state and render it as 106 | # a chat message. 107 | for message in st.session_state.messages[1:]: 108 | if message["role"] == "user": 109 | with st.chat_message(message["role"],avatar=av_us): 110 | st.markdown(message["content"]) 111 | else: 112 | with st.chat_message(message["role"],avatar=av_ass): 113 | st.markdown(message["content"]) 114 | 115 | # We take questions/instructions from the chat input to pass to the LLM 116 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"): 117 | 118 | # Add our input to the session state 119 | st.session_state.messages.append( 120 | {"role": "user", "content": user_prompt} 121 | ) 122 | 123 | # Add our input to the chat window 124 | with st.chat_message("user", avatar=av_us): 125 | st.markdown(user_prompt) 126 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}') 127 | 128 | 129 | with st.chat_message("assistant",avatar=av_ass): 130 | message_placeholder = st.empty() 131 | with st.spinner("Thinking..."): 132 | response = '' 133 | conv_messages = [] 134 | st.session_state.len_context = len(st.session_state.messages) 135 | # Checking context window for the LLM, not for the chat history to be displayed 136 | if st.session_state.len_context > st.session_state.limiter: 137 | st.session_state.bufstatus = "**:red[Overflow]**" 138 | # this will keep 5 full turns into consideration 139 | x=st.session_state.limiter-5 140 | conv_messages.append(st.session_state.messages[0]) 141 | for i in range(0,x): 142 | conv_messages.append(st.session_state.messages[-x+i]) 143 | print(len(conv_messages)) 144 | full_response = "" 145 | completion = client.chat.completions.create( 146 | model="local-model", # this field is currently unused 147 | messages=conv_messages, 148 | temperature=st.session_state.temperature, 149 | frequency_penalty = 1.6, 150 | stop=['<|im_end|>','',""], 151 | max_tokens=st.session_state.maxlength, 152 | stream=True, 153 | ) 154 | for chunk in completion: 155 | if chunk.choices[0].delta.content: 156 | full_response += chunk.choices[0].delta.content 157 | message_placeholder.markdown(full_response + "🟠") 158 | message_placeholder.markdown(full_response) 159 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 160 | else: 161 | st.session_state.bufstatus = "**:green[Good]**" 162 | full_response = "" 163 | completion = client.chat.completions.create( 164 | model="local-model", # this field is currently unused 165 | messages=st.session_state.messages, 166 | temperature=st.session_state.temperature, 167 | frequency_penalty = 1.6, 168 | stop=['<|im_end|>','',""], 169 | max_tokens=st.session_state.maxlength, 170 | stream=True, 171 | ) 172 | for chunk in completion: 173 | if chunk.choices[0].delta.content: 174 | full_response += chunk.choices[0].delta.content 175 | message_placeholder.markdown(full_response + "🟠") 176 | message_placeholder.markdown(full_response) 177 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 178 | 179 | # Add the response to the session state 180 | st.session_state.messages.append( 181 | {"role": "assistant", "content": full_response} 182 | ) 183 | st.session_state.len_context = len(st.session_state.messages) 184 | -------------------------------------------------------------------------------- /03.st-SearchDDG2-text.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240627 Shanghai Time 08:00 3 | # adding streamlit interface to browse web documents 4 | import streamlit as st 5 | import os 6 | from rich.markdown import Markdown 7 | import warnings 8 | warnings.filterwarnings(action='ignore') 9 | from rich.console import Console 10 | from newspaper import Article 11 | import pickle 12 | from langchain.schema.document import Document 13 | console = Console(width=90) 14 | import random 15 | import string 16 | import datetime 17 | 18 | # to write out a log file PLAIN TXT 19 | def writehistory(filename,text): 20 | with open(filename, 'w', encoding='utf-8') as f: 21 | f.write(text) 22 | f.write('\n') 23 | f.close() 24 | 25 | # GENERATE RANDOM HASH of n charachters 26 | def genRANstring(n): 27 | """ 28 | n = int number of char to randomize 29 | """ 30 | N = n 31 | res = ''.join(random.choices(string.ascii_uppercase + 32 | string.digits, k=N)) 33 | return res 34 | 35 | # DEFINE FILENAMES 36 | if "sessionHASH" not in st.session_state: 37 | st.session_state.sessionHASH = genRANstring(5) 38 | if "sessionlogfilename" not in st.session_state: 39 | st.session_state.sessionlogfilename = f'{st.session_state.sessionHASH}-log.txt' 40 | if "sessionDBfilename" not in st.session_state: 41 | st.session_state.sessionDBfilename = f'{st.session_state.sessionHASH}_searchDB.ddg' 42 | 43 | # Initialize search history 44 | if "searches" not in st.session_state: 45 | st.session_state.searches = [] 46 | 47 | # Newsletter3k utility disctionary from NLTK 48 | @st.cache_resource 49 | def loadDictionary(): 50 | import nltk 51 | nltk.download('punkt') 52 | 53 | # Network Connector LangChain DuckDuckGo Wrapper 54 | #from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 55 | # https://pypi.org/project/duckduckgo-search 56 | # https://api.python.langchain.com/en/latest/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html 57 | @st.cache_resource 58 | def createWrapper(): 59 | from langchain_community.utilities import DuckDuckGoSearchAPIWrapper 60 | wrapper = DuckDuckGoSearchAPIWrapper(region='us-en', time="y", max_results=10) #time parameter Options: d, w, m, y 61 | return wrapper 62 | 63 | loadDictionary() 64 | if "wrapper" not in st.session_state: 65 | st.session_state.wrapper = createWrapper() 66 | if "searchquery" not in st.session_state: 67 | st.session_state.searchquery = '' 68 | if "limiter" not in st.session_state: 69 | st.session_state.limiter = 0 70 | 71 | def getWebDocuments(q,n): 72 | #wrapper = createWrapper() 73 | console.print(f'[bold red1]Searching for {q}') 74 | console.print(90*'=') 75 | rawdb = st.session_state.wrapper.results(q,max_results=n) 76 | docdocs = [] 77 | mt = 1 78 | numofdocs = len(rawdb) 79 | for items in rawdb: 80 | url = items["link"] 81 | try: #useful if the url is no reachable 82 | article = Article(url) 83 | article.download() 84 | article.parse() 85 | article.nlp() 86 | kw = [] 87 | #we merge nltk keywords and meta webpage keywords 88 | for i in article.keywords+article.meta_keywords: 89 | if i == '': #no blck yeywords for us 90 | pass 91 | else: 92 | kw.append(i) 93 | if article.text == '': #sometimes there is no text to parse. so we use the snippet 94 | docdocs.append(Document(page_content = items["snippet"], metadata = { 95 | 'source': items["link"], 96 | 'title': items["title"], 97 | 'snippet': items["snippet"], 98 | 'author':article.authors, 99 | 'keywords':kw, 100 | 'meta_description':article.meta_description, 101 | 'meta_img':article.meta_img, 102 | 'top_image':article.top_image, 103 | 'publish_date':article.publish_date, 104 | 'summary':article.summary})) 105 | else: 106 | docdocs.append(Document(page_content = article.text.replace('\n\n',''), metadata = { 107 | 'source': items["link"], 108 | 'title': items["title"], 109 | 'snippet': items["snippet"], 110 | 'author':article.authors, 111 | 'keywords':kw, 112 | 'meta_description':article.meta_description, 113 | 'meta_img':article.meta_img, 114 | 'top_image':article.top_image, 115 | 'publish_date':article.publish_date, 116 | 'summary':article.summary})) 117 | console.print(f'Prepared Ducment n.{mt} out of {numofdocs}') 118 | mt +=1 119 | except: 120 | pass 121 | st.session_state.searches.append({'role': 'results','results': docdocs}) 122 | 123 | 124 | 125 | st.title("AI powered Web Document Search") 126 | st.write('Using Newspaper3k and DuckDuckGo LangChain wrapper') 127 | 128 | 129 | def savelog(db): 130 | log = [] 131 | finaltext = '' 132 | for section in db: 133 | singlesearch = '' 134 | #console.print(section) 135 | if section['role'] == 'query': 136 | header1 = f"##### 🪄 Query: *{section['query']}*" 137 | singlesearch = singlesearch + header1 + '\n' 138 | else: 139 | header2 = '##### 🗃️ Web Documents' 140 | singlesearch = singlesearch + header2 + '\n' 141 | for items in section['results']: 142 | #console.print(section['results']) 143 | singlesearch = singlesearch + '📝 ' + f"{items.metadata['title']}\n" 144 | singlesearch = singlesearch + items.page_content + '\n--\n' 145 | singlesearch = singlesearch + f"""'source': {items.metadata['source']} 146 | 'title': {items.metadata['title']} 147 | 'snippet': {items.metadata['snippet']} 148 | 'author':{items.metadata['author']} 149 | 'keywords':{items.metadata['keywords']} 150 | 'meta_description':{items.metadata['meta_description']} 151 | 'meta_img':{items.metadata['meta_img']} 152 | 'top_image':{items.metadata['top_image']} 153 | 'publish_date':{items.metadata['publish_date']} 154 | 'summary':{items.metadata['summary']} 155 | ------------------------//---------------------------\n""" 156 | log.append(singlesearch) 157 | for i in log: 158 | finaltext = finaltext + i 159 | writehistory(st.session_state.sessionlogfilename,finaltext) 160 | 161 | def savedb(db): 162 | output = open(st.session_state.sessionDBfilename, 'wb') 163 | pickle.dump(db, output) 164 | output.close() 165 | 166 | 167 | # CREATE THE SIDEBAR 168 | with st.sidebar: 169 | st.image('logoDDG.png', use_column_width=True) 170 | st.session_state.limiter = st.slider('N.of Docs:', min_value=1, max_value=5, value=3, step=1) 171 | st.markdown(f"**_DB name**: {st.session_state.sessionDBfilename}") 172 | st.markdown(f"**___Logfile**: {st.session_state.sessionlogfilename}") 173 | logevents = st.empty() 174 | dbevents = st.empty() 175 | btnSAVE = st.button("Save DB",type="primary", use_container_width=True) 176 | 177 | # CREATE SEARCH FORM 178 | with st.form("my_form"): 179 | st.image('searchddglogo.png',) 180 | query = st.text_input( 181 | "What do you want to search?", 182 | placeholder="your query here", 183 | label_visibility='collapsed') 184 | # Every form must have a submit button. 185 | submitted = st.form_submit_button("Search") 186 | if submitted: 187 | with st.spinner(text="Search in progress..."): 188 | st.session_state.searchquery = query 189 | st.session_state.searches.append({'role' : 'query','query': query}) 190 | getWebDocuments(query,st.session_state.limiter) 191 | savelog(st.session_state.searches) 192 | #logevents.success('Search saved') 193 | st.toast('Search saved!', icon='🎉') 194 | 195 | st.markdown('### AI serach results') 196 | with st.container(height=500, border=True): 197 | if len(st.session_state.searches) >0: 198 | for section in st.session_state.searches: 199 | #console.print(section) 200 | if section['role'] == 'query': 201 | st.write(f"##### 🪄 Query: *{section['query']}*") 202 | else: 203 | with st.container(): 204 | st.markdown('##### 🗃️ Web Documents') 205 | for items in section['results']: 206 | #console.print(section['results']) 207 | with st.expander(label=items.metadata['title'], expanded=False, icon='📝'): 208 | st.write(items.page_content) 209 | st.divider() 210 | st.write(items.metadata) 211 | st.divider() 212 | if btnSAVE: 213 | savedb(st.session_state.searches) 214 | #dbevents.success('dB saved') 215 | st.toast('dB saved!', icon='🎉') 216 | 217 | console.print(Markdown("> SEARCH COMPLETED...")) 218 | console.print(" - ") 219 | 220 | 221 | -------------------------------------------------------------------------------- /03.st-FixOLD-DDG2_0.2.0.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240627 Shanghai Time 08:00 3 | # adding streamlit interface to browse web documents 4 | # add 'search_query' : q, in the metadata 5 | # ⚠️ Segregating display datastructure from saved db 🗃️ datastructure 6 | # REV02 - fix old type of DDG 7 | import streamlit as st 8 | import os 9 | from rich.markdown import Markdown 10 | import warnings 11 | warnings.filterwarnings(action='ignore') 12 | from rich.console import Console 13 | from newspaper import Article 14 | import pickle 15 | from langchain.schema.document import Document 16 | console = Console(width=90) 17 | import random 18 | import string 19 | import datetime 20 | 21 | st.set_page_config(page_title="AI powered web serach", layout="wide",page_icon='📱') 22 | 23 | # to write out a log file PLAIN TXT - this will rewrite every time the file 24 | def writehistory(filename,text): 25 | with open(filename, 'w', encoding='utf-8') as f: 26 | f.write(text) 27 | f.write('\n') 28 | f.close() 29 | 30 | # GENERATE RANDOM HASH of n charachters 31 | def genRANstring(n): 32 | """ 33 | n = int number of char to randomize 34 | """ 35 | N = n 36 | res = ''.join(random.choices(string.ascii_uppercase + 37 | string.digits, k=N)) 38 | return res 39 | 40 | # DEFINE FILENAMES 41 | if "sessionHASH" not in st.session_state: 42 | st.session_state.sessionHASH = genRANstring(5) 43 | if "sessionlogfilename" not in st.session_state: 44 | st.session_state.sessionlogfilename = f'{st.session_state.sessionHASH}-log.txt' 45 | if "sessionDBfilename" not in st.session_state: 46 | st.session_state.sessionDBfilename = f'{st.session_state.sessionHASH}_searchDB.ddg' 47 | 48 | # Initialize search history and dataDB / the one to be saved 49 | if "searches" not in st.session_state: 50 | st.session_state.searches = [] 51 | if "dataDB" not in st.session_state: 52 | st.session_state.dataDB = [] 53 | 54 | # Newsletter3k utility disctionary from NLTK 55 | @st.cache_resource 56 | def loadDictionary(): 57 | import nltk 58 | nltk.download('punkt') 59 | 60 | # Network Connector LangChain DuckDuckGo Wrapper 61 | #from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 62 | # https://pypi.org/project/duckduckgo-search 63 | # https://api.python.langchain.com/en/latest/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html 64 | @st.cache_resource 65 | def createWrapper(): 66 | from langchain_community.utilities import DuckDuckGoSearchAPIWrapper 67 | wrapper = DuckDuckGoSearchAPIWrapper(region='us-en', time="y", max_results=10) #time parameter Options: d, w, m, y 68 | return wrapper 69 | 70 | loadDictionary() 71 | if "wrapper" not in st.session_state: 72 | st.session_state.wrapper = createWrapper() 73 | if "searchquery" not in st.session_state: 74 | st.session_state.searchquery = '' 75 | if "limiter" not in st.session_state: 76 | st.session_state.limiter = 0 77 | if "chatdocs" not in st.session_state: 78 | st.session_state.chatdocs = 0 79 | if "uploadedDoc" not in st.session_state: 80 | st.session_state.uploadedDoc = [] 81 | if "uploadedText" not in st.session_state: 82 | st.session_state.uploadedText = '' 83 | if "fileloaded" not in st.session_state: 84 | st.session_state.fileloaded = 1 85 | 86 | def LoadDocuments(pickleDB): 87 | mt = 1 88 | numofdocs = len(pickleDB) 89 | for items in pickleDB: 90 | st.session_state.dataDB.append(Document(page_content = items.page_content, metadata = { 91 | 'search_query' : items.metadata['search_query'], 92 | 'source': items.metadata['source'], 93 | 'title': items.metadata['title'], 94 | 'snippet': items.metadata['snippet'], 95 | 'author':items.metadata['author'], 96 | 'keywords':items.metadata['keywords'], 97 | 'meta_description':items.metadata['meta_description'], 98 | 'meta_img':items.metadata['meta_img'], 99 | 'top_image':items.metadata['top_image'], 100 | 'publish_date':items.metadata['publish_date'], 101 | 'summary':items.metadata['summary']})) 102 | console.print(f'Prepared Document n.{mt} out of {numofdocs}') 103 | mt +=1 104 | 105 | 106 | if "keyDDGfile" not in st.session_state: 107 | st.session_state.keyDDGfile = 1 108 | 109 | def resetall(): 110 | st.session_state.keyimagefile += 1 111 | st.rerun() 112 | 113 | #################### MAIN STREaMLIT APP ################################## 114 | st.title("AI powered Web Document Search") 115 | st.write('Using Newspaper3k and DuckDuckGo LangChain wrapper') 116 | st.divider() 117 | # Upload the audio file 118 | file1 = st.sidebar.file_uploader("Upload a text document", 119 | type=["ddg"],accept_multiple_files=False, 120 | key=st.session_state.keyDDGfile) 121 | st.divider() 122 | # Save the searches into plain TXT file 123 | def savelog(db): 124 | log = [] 125 | finaltext = '' 126 | for section in db: 127 | singlesearch = '' 128 | #console.print(section) 129 | if section['role'] == 'query': 130 | header1 = f"##### 🪄 Query: *{section['query']}*" 131 | singlesearch = singlesearch + header1 + '\n' 132 | else: 133 | header2 = '##### 🗃️ Web Documents' 134 | singlesearch = singlesearch + header2 + '\n' 135 | for items in section['results']: 136 | #console.print(section['results']) 137 | singlesearch = singlesearch + '📝 ' + f"{items.metadata['title']}\n" 138 | singlesearch = singlesearch + items.page_content + '\n--\n' 139 | singlesearch = singlesearch + f"""'source': {items.metadata['source']} 140 | 'title': {items.metadata['title']} 141 | 'snippet': {items.metadata['snippet']} 142 | 'author':{items.metadata['author']} 143 | 'keywords':{items.metadata['keywords']} 144 | 'meta_description':{items.metadata['meta_description']} 145 | 'meta_img':{items.metadata['meta_img']} 146 | 'top_image':{items.metadata['top_image']} 147 | 'publish_date':{items.metadata['publish_date']} 148 | 'summary':{items.metadata['summary']} 149 | ------------------------//---------------------------\n""" 150 | log.append(singlesearch) 151 | for i in log: 152 | finaltext = finaltext + i 153 | writehistory(st.session_state.sessionlogfilename,finaltext) 154 | 155 | def load(fname): 156 | console.print(f'[blink2 orange1]Loading...') 157 | pkl_file = open(fname.name, 'rb') 158 | data_docs = pickle.load(pkl_file) 159 | pkl_file.close() 160 | st.session_state.dataDB = data_docs 161 | 162 | def savedb(db): 163 | output = open(st.session_state.sessionDBfilename, 'wb') 164 | pickle.dump(db, output) 165 | output.close() 166 | 167 | # CREATE THE SIDEBAR 168 | with st.sidebar: 169 | st.image('logoDDG.png', use_column_width=True) 170 | st.session_state.limiter = st.slider('N.of Docs:', min_value=1, max_value=5, value=3, step=1) 171 | st.markdown(f"**_DB name**: {st.session_state.sessionDBfilename}") 172 | st.markdown(f"**___Logfile**: {st.session_state.sessionlogfilename}") 173 | logevents = st.empty() 174 | dbevents = st.empty() 175 | btnLOAD = st.button("Load OLD DB",type="primary", use_container_width=True,disabled=st.session_state.keyDDGfile) 176 | st.divider() 177 | reset_btn = st.button('🧻✨ **Reset Document** ', type='primary') 178 | btnSAVE = st.button("Save DB",type="secondary", use_container_width=True, disabled=st.session_state.fileloaded) 179 | 180 | if reset_btn: 181 | resetall() 182 | try: 183 | st.session_state.uploadedDoc = [] 184 | st.session_state.uploadedText = '' 185 | st.session_state.fileloaded = 1 186 | st.session_state.keyDDGfile = 1 187 | except: 188 | pass 189 | 190 | if file1: 191 | st.session_state.keyDDGfile = 0 192 | 193 | if btnLOAD: 194 | load(file1) 195 | st.session_state.fileloaded = 0 196 | 197 | if btnSAVE: 198 | savelog() 199 | savedb(st.session_state.searches) 200 | #dbevents.success('dB saved') 201 | st.toast('dB saved!', icon='🎉') 202 | 203 | 204 | st.markdown('### AI serach results') 205 | with st.container(height=500, border=True): 206 | if len(st.session_state.searches) >0: 207 | for section in st.session_state.searches: 208 | #console.print(section) 209 | if section['role'] == 'query': 210 | st.write(f"##### 🪄 Query: *{section['query']}*") 211 | else: 212 | with st.container(): 213 | st.markdown('##### 🗃️ Web Documents') 214 | for items in section['results']: 215 | #console.print(section['results']) 216 | with st.expander(label=items.metadata['title'], expanded=False, icon='📝'): 217 | st.image(items.metadata['top_image'], width=350) 218 | st.write(f"source url: {items.metadata['source']}\n\nSummary: {items.metadata['summary']}") 219 | st.write(items.page_content) 220 | st.divider() 221 | st.write(items.metadata) 222 | st.divider() 223 | 224 | 225 | console.print(Markdown("> SEARCH COMPLETED...")) 226 | console.print(" - ") 227 | 228 | 229 | -------------------------------------------------------------------------------- /01.st-ChatLlamaCPP_stream.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import datetime 3 | import os 4 | from io import StringIO 5 | from rich.markdown import Markdown 6 | import warnings 7 | warnings.filterwarnings(action='ignore') 8 | import datetime 9 | from rich.console import Console 10 | console = Console(width=90) 11 | import tiktoken 12 | import random 13 | import string 14 | 15 | encoding = tiktoken.get_encoding("r50k_base") #context_count = len(encoding.encode(yourtext)) 16 | 17 | from llama_cpp import Llama 18 | 19 | #AVATARS 👷🐦 🥶🌀 20 | av_us = '🧑‍💻' #"🦖" #A single emoji, e.g. "🧑‍💻", "🤖", "🦖". Shortcodes are not supported. 21 | av_ass = '🤖' 22 | 23 | # Set the webpage title 24 | st.set_page_config( 25 | page_title="Your LocalGPT with 🟠 Qwen-0.5", 26 | page_icon="🟠", 27 | layout="wide") 28 | 29 | @st.cache_resource 30 | def create_chat(): 31 | # Set HF API token and HF repo 32 | from llama_cpp import Llama 33 | qwen05b = Llama( 34 | model_path='models/dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf', 35 | n_gpu_layers=0, 36 | temperature=0.1, 37 | top_p = 0.5, 38 | n_ctx=8192, 39 | max_tokens=600, 40 | repeat_penalty=1.7, 41 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 42 | verbose=False, 43 | ) 44 | print('loading qwen2-0_5b-instruct-q8_0.gguf with LlamaCPP...') 45 | return qwen05b 46 | 47 | def writehistory(filename,text): 48 | with open(filename, 'a', encoding='utf-8') as f: 49 | f.write(text) 50 | f.write('\n') 51 | f.close() 52 | 53 | 54 | 55 | # Create a header element 56 | mytitle = '

Your own LocalGPT with 🟠 Key Solution AI

' 57 | st.markdown(mytitle, unsafe_allow_html=True) 58 | #st.header("Your own LocalGPT with 🌀 h2o-danube-1.8b-chat") 59 | subtitle = '

Powerwed by Qwen, the best 0.5B chat model?

' 60 | st.markdown(subtitle, unsafe_allow_html=True) 61 | 62 | 63 | def genRANstring(n): 64 | """ 65 | n = int number of char to randomize 66 | """ 67 | N = n 68 | res = ''.join(random.choices(string.ascii_uppercase + 69 | string.digits, k=N)) 70 | return res 71 | 72 | # create THE SESSIoN STATES 73 | if "logfilename" not in st.session_state: 74 | ## Logger file 75 | logfile = f'{genRANstring(5)}_log.txt' 76 | st.session_state.logfilename = logfile 77 | #Write in the history the first 2 sessions 78 | writehistory(st.session_state.logfilename,f'{str(datetime.datetime.now())}\n\nYour own LocalGPT with 🌀 Qwen-0.5b-chat\n---\n🧠🫡: You are a helpful assistant.') 79 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?') 80 | 81 | if "len_context" not in st.session_state: 82 | st.session_state.len_context = 0 83 | 84 | if "limiter" not in st.session_state: 85 | st.session_state.limiter = 0 86 | 87 | if "bufstatus" not in st.session_state: 88 | st.session_state.bufstatus = "**:green[Good]**" 89 | 90 | if "temperature" not in st.session_state: 91 | st.session_state.temperature = 0.1 92 | 93 | if "maxlength" not in st.session_state: 94 | st.session_state.maxlength = 500 95 | 96 | # Point to the local server 97 | llm = create_chat() 98 | 99 | # CREATE THE SIDEBAR 100 | with st.sidebar: 101 | st.image('logo.png', use_column_width=True) 102 | st.session_state.temperature = st.slider('Temperature:', min_value=0.0, max_value=1.0, value=0.1, step=0.02) 103 | st.session_state.limiter = st.slider('Turns:', min_value=7, max_value=17, value=12, step=1) 104 | st.session_state.maxlength = st.slider('Length reply:', min_value=150, max_value=1000, 105 | value=500, step=50) 106 | mytokens = st.markdown(f"""**Context turns** {st.session_state.len_context}""") 107 | st.markdown(f"Buffer status: {st.session_state.bufstatus}") 108 | st.markdown(f"**Logfile**: {st.session_state.logfilename}") 109 | btnClear = st.button("Clear History",type="primary", use_container_width=True) 110 | 111 | # We store the conversation in the session state. 112 | # This will be used to render the chat conversation. 113 | # We initialize it with the first message we want to be greeted with. 114 | if "messages" not in st.session_state: 115 | st.session_state.messages = [ 116 | {"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.",}, 117 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 118 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 119 | ] 120 | 121 | def clearHistory(): 122 | st.session_state.messages = [ 123 | {"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.",}, 124 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 125 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 126 | ] 127 | st.session_state.len_context = len(st.session_state.messages) 128 | if btnClear: 129 | clearHistory() 130 | st.session_state.len_context = len(st.session_state.messages) 131 | 132 | # We loop through each message in the session state and render it as 133 | # a chat message. 134 | for message in st.session_state.messages[1:]: 135 | if message["role"] == "user": 136 | with st.chat_message(message["role"],avatar=av_us): 137 | st.markdown(message["content"]) 138 | else: 139 | with st.chat_message(message["role"],avatar=av_ass): 140 | st.markdown(message["content"]) 141 | 142 | # We take questions/instructions from the chat input to pass to the LLM 143 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"): 144 | 145 | # Add our input to the session state 146 | st.session_state.messages.append( 147 | {"role": "user", "content": user_prompt} 148 | ) 149 | 150 | # Add our input to the chat window 151 | with st.chat_message("user", avatar=av_us): 152 | st.markdown(user_prompt) 153 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}') 154 | 155 | 156 | with st.chat_message("assistant",avatar=av_ass): 157 | message_placeholder = st.empty() 158 | with st.spinner("Thinking..."): 159 | response = '' 160 | conv_messages = [] 161 | st.session_state.len_context = len(st.session_state.messages) 162 | # Checking context window for the LLM, not for the chat history to be displayed 163 | if st.session_state.len_context > st.session_state.limiter: 164 | st.session_state.bufstatus = "**:red[Overflow]**" 165 | # this will keep 5 full turns into consideration 166 | x=st.session_state.limiter-5 167 | conv_messages.append(st.session_state.messages[0]) 168 | for i in range(0,x): 169 | conv_messages.append(st.session_state.messages[-x+i]) 170 | print(len(conv_messages)) 171 | full_response = "" 172 | for chunk in llm.create_chat_completion( 173 | messages=conv_messages, 174 | temperature=st.session_state.temperature, 175 | repeat_penalty= 1.6, 176 | stop=['<|im_end|>','',""], 177 | max_tokens=st.session_state.maxlength, 178 | stream=True,): 179 | try: 180 | if chunk["choices"][0]["delta"]["content"]: 181 | full_response += chunk["choices"][0]["delta"]["content"] 182 | message_placeholder.markdown(full_response + "🟠") 183 | except: 184 | pass 185 | message_placeholder.markdown(full_response) 186 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 187 | else: 188 | st.session_state.bufstatus = "**:green[Good]**" 189 | full_response = "" 190 | for chunk in llm.create_chat_completion( 191 | messages=st.session_state.messages, 192 | temperature=st.session_state.temperature, 193 | repeat_penalty= 1.6, 194 | stop=['<|im_end|>','',""], 195 | max_tokens=st.session_state.maxlength, 196 | stream=True,): 197 | try: 198 | if chunk["choices"][0]["delta"]["content"]: 199 | full_response += chunk["choices"][0]["delta"]["content"] 200 | message_placeholder.markdown(full_response + "🟠") 201 | except: 202 | pass 203 | message_placeholder.markdown(full_response) 204 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 205 | 206 | # Add the response to the session state 207 | st.session_state.messages.append( 208 | {"role": "assistant", "content": full_response} 209 | ) 210 | st.session_state.len_context = len(st.session_state.messages) 211 | -------------------------------------------------------------------------------- /04.st-LoadDDG2_0.2.0.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240628 Shanghai Time 12:00 3 | # adding streamlit interface to browse web documents 4 | # - tool to merge all .ddg file 5 | # - tool to explore the web documents 6 | # - select documents to build a temporary vectorstore 7 | import streamlit as st 8 | from rich.markdown import Markdown 9 | from rich.console import Console 10 | console = Console(width=90) 11 | # DATA AND WARNING HANDLING 12 | import warnings 13 | warnings.filterwarnings(action='ignore') 14 | import pickle 15 | import os 16 | import random 17 | import string 18 | import datetime 19 | # LANGCHAIN IMPORTS 20 | from langchain.schema.document import Document 21 | from langchain.text_splitter import TokenTextSplitter 22 | from langchain_community.document_loaders import TextLoader 23 | from langchain_community.vectorstores import FAISS 24 | from langchain_community.embeddings import LlamaCppEmbeddings 25 | from langchain_text_splitters import CharacterTextSplitter 26 | # LLM AND NLP RELATED IMPORTS 27 | from llama_cpp import Llama 28 | from newspaper import Article 29 | import tiktoken 30 | 31 | 32 | # SET MAIN STREAMLIT CONFIGURATION 33 | st.set_page_config(page_title="AI powered web serach", layout="wide",page_icon='📱') 34 | 35 | # to write out a log file PLAIN TXT 36 | def writehistory(filename,text): 37 | with open(filename, 'w', encoding='utf-8') as f: 38 | f.write(text) 39 | f.write('\n') 40 | f.close() 41 | 42 | # GENERATE RANDOM HASH of n charachters 43 | def genRANstring(n): 44 | """ 45 | n = int number of char to randomize 46 | """ 47 | N = n 48 | res = ''.join(random.choices(string.ascii_uppercase + 49 | string.digits, k=N)) 50 | return res 51 | 52 | # DEFINE FILENAMES 53 | if "sessionHASH" not in st.session_state: 54 | st.session_state.sessionHASH = genRANstring(5) 55 | if "sessionlogfilename" not in st.session_state: 56 | st.session_state.sessionlogfilename = f'{st.session_state.sessionHASH}-log.txt' 57 | if "sessionDBfilename" not in st.session_state: 58 | st.session_state.sessionDBfilename = f'{st.session_state.sessionHASH}_searchDB.ddg' 59 | 60 | # Initialize search history 61 | if "searches" not in st.session_state: 62 | st.session_state.searches = [] 63 | 64 | # Newsletter3k utility disctionary from NLTK 65 | @st.cache_resource 66 | def loadDictionary(): 67 | import nltk 68 | nltk.download('punkt') 69 | # Create Tokenizer conunter 70 | @st.cache_resource 71 | def tokencounter(): 72 | import tiktoken 73 | #context_count = len(encoding.encode(yourtext)) 74 | encoding = tiktoken.get_encoding("r50k_base") 75 | return encoding 76 | 77 | # Network Connector LangChain DuckDuckGo Wrapper 78 | #from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 79 | # https://pypi.org/project/duckduckgo-search 80 | # https://api.python.langchain.com/en/latest/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html 81 | @st.cache_resource 82 | def createWrapper(): 83 | from langchain_community.utilities import DuckDuckGoSearchAPIWrapper 84 | wrapper = DuckDuckGoSearchAPIWrapper(region='us-en', time="y", max_results=10) #time parameter Options: d, w, m, y 85 | return wrapper 86 | 87 | loadDictionary() 88 | tokencounter = tokencounter() 89 | 90 | if "wrapper" not in st.session_state: 91 | st.session_state.wrapper = createWrapper() 92 | if "searchquery" not in st.session_state: 93 | st.session_state.searchquery = '' 94 | if "limiter" not in st.session_state: 95 | st.session_state.limiter = 0 96 | 97 | def getWebDocuments(q,n): 98 | #wrapper = createWrapper() 99 | console.print(f'[bold red1]Searching for {q}') 100 | console.print(90*'=') 101 | rawdb = st.session_state.wrapper.results(q,max_results=n) 102 | docdocs = [] 103 | mt = 1 104 | numofdocs = len(rawdb) 105 | for items in rawdb: 106 | url = items["link"] 107 | try: #useful if the url is no reachable 108 | article = Article(url) 109 | article.download() 110 | article.parse() 111 | article.nlp() 112 | kw = [] 113 | #we merge nltk keywords and meta webpage keywords 114 | for i in article.keywords+article.meta_keywords: 115 | if i == '': #no blck yeywords for us 116 | pass 117 | else: 118 | kw.append(i) 119 | if article.text == '': #sometimes there is no text to parse. so we use the snippet 120 | docdocs.append(Document(page_content = items["snippet"], metadata = { 121 | 'source': items["link"], 122 | 'title': items["title"], 123 | 'snippet': items["snippet"], 124 | 'author':article.authors, 125 | 'keywords':kw, 126 | 'meta_description':article.meta_description, 127 | 'meta_img':article.meta_img, 128 | 'top_image':article.top_image, 129 | 'publish_date':article.publish_date, 130 | 'summary':article.summary})) 131 | else: 132 | docdocs.append(Document(page_content = article.text.replace('\n\n',''), metadata = { 133 | 'source': items["link"], 134 | 'title': items["title"], 135 | 'snippet': items["snippet"], 136 | 'author':article.authors, 137 | 'keywords':kw, 138 | 'meta_description':article.meta_description, 139 | 'meta_img':article.meta_img, 140 | 'top_image':article.top_image, 141 | 'publish_date':article.publish_date, 142 | 'summary':article.summary})) 143 | console.print(f'Prepared Document n.{mt} out of {numofdocs}') 144 | mt +=1 145 | except: 146 | pass 147 | st.session_state.searches.append({'role': 'results','results': docdocs}) 148 | 149 | 150 | 151 | st.title("AI powered Web Document Search") 152 | st.write('Using Newspaper3k and DuckDuckGo LangChain wrapper') 153 | 154 | 155 | def savelog(db): 156 | log = [] 157 | finaltext = '' 158 | for section in db: 159 | singlesearch = '' 160 | #console.print(section) 161 | if section['role'] == 'query': 162 | header1 = f"##### 🪄 Query: *{section['query']}*" 163 | singlesearch = singlesearch + header1 + '\n' 164 | else: 165 | header2 = '##### 🗃️ Web Documents' 166 | singlesearch = singlesearch + header2 + '\n' 167 | for items in section['results']: 168 | #console.print(section['results']) 169 | singlesearch = singlesearch + '📝 ' + f"{items.metadata['title']}\n" 170 | singlesearch = singlesearch + items.page_content + '\n--\n' 171 | singlesearch = singlesearch + f"""'source': {items.metadata['source']} 172 | 'title': {items.metadata['title']} 173 | 'snippet': {items.metadata['snippet']} 174 | 'author':{items.metadata['author']} 175 | 'keywords':{items.metadata['keywords']} 176 | 'meta_description':{items.metadata['meta_description']} 177 | 'meta_img':{items.metadata['meta_img']} 178 | 'top_image':{items.metadata['top_image']} 179 | 'publish_date':{items.metadata['publish_date']} 180 | 'summary':{items.metadata['summary']} 181 | ------------------------//---------------------------\n""" 182 | log.append(singlesearch) 183 | for i in log: 184 | finaltext = finaltext + i 185 | writehistory(st.session_state.sessionlogfilename,finaltext) 186 | 187 | def savedb(db): 188 | output = open(st.session_state.sessionDBfilename, 'wb') 189 | pickle.dump(db, output) 190 | output.close() 191 | 192 | 193 | # CREATE THE SIDEBAR 194 | with st.sidebar: 195 | st.image('logoDDG.png', use_column_width=True) 196 | st.session_state.limiter = st.slider('N.of Docs:', min_value=1, max_value=5, value=3, step=1) 197 | st.markdown(f"**_DB name**: {st.session_state.sessionDBfilename}") 198 | st.markdown(f"**___Logfile**: {st.session_state.sessionlogfilename}") 199 | logevents = st.empty() 200 | dbevents = st.empty() 201 | btnSAVE = st.button("Save DB",type="primary", use_container_width=True) 202 | 203 | # CREATE SEARCH FORM 204 | with st.form("my_form"): 205 | st.image('searchddglogo.png',) 206 | query = st.text_input( 207 | "What do you want to search?", 208 | placeholder="your query here", 209 | label_visibility='collapsed') 210 | # Every form must have a submit button. 211 | submitted = st.form_submit_button("Search") 212 | if submitted: 213 | with st.spinner(text="Search in progress..."): 214 | st.session_state.searchquery = query 215 | st.session_state.searches.append({'role' : 'query','query': query}) 216 | getWebDocuments(query,st.session_state.limiter) 217 | savelog(st.session_state.searches) 218 | #logevents.success('Search saved') 219 | st.toast('Search saved!', icon='🎉') 220 | 221 | st.markdown('### AI serach results') 222 | with st.container(height=500, border=True): 223 | if len(st.session_state.searches) >0: 224 | for section in st.session_state.searches: 225 | #console.print(section) 226 | if section['role'] == 'query': 227 | st.write(f"##### 🪄 Query: *{section['query']}*") 228 | else: 229 | with st.container(): 230 | st.markdown('##### 🗃️ Web Documents') 231 | for items in section['results']: 232 | #console.print(section['results']) 233 | with st.expander(label=items.metadata['title'], expanded=False, icon='📝'): 234 | st.image(items.metadata['top_image'], width=350) 235 | st.write(f"source url: {items.metadata['source']}\n\nSummary: {items.metadata['summary']}") 236 | st.write(items.page_content) 237 | st.divider() 238 | st.write(items.metadata) 239 | st.divider() 240 | if btnSAVE: 241 | savedb(st.session_state.searches) 242 | #dbevents.success('dB saved') 243 | st.toast('dB saved!', icon='🎉') 244 | 245 | console.print(Markdown("> SEARCH COMPLETED...")) 246 | console.print(" - ") 247 | 248 | 249 | -------------------------------------------------------------------------------- /testcpp/99.st-ChatLlamaCPP_streamSEL.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import datetime 3 | import os 4 | from io import StringIO 5 | from rich.markdown import Markdown 6 | import warnings 7 | warnings.filterwarnings(action='ignore') 8 | import datetime 9 | from rich.console import Console 10 | console = Console(width=90) 11 | import tiktoken 12 | import random 13 | import string 14 | 15 | encoding = tiktoken.get_encoding("r50k_base") #context_count = len(encoding.encode(yourtext)) 16 | 17 | from llama_cpp import Llama 18 | 19 | #AVATARS 👷🐦 🥶🌀 20 | av_us = '🧑‍💻' #"🦖" #A single emoji, e.g. "🧑‍💻", "🤖", "🦖". Shortcodes are not supported. 21 | av_ass = '🤖' 22 | 23 | # Set the webpage title 24 | st.set_page_config( 25 | page_title="Your LocalGPT with 🟠 GGUF models", 26 | page_icon="🟠", 27 | layout="wide") 28 | 29 | if "model_list" not in st.session_state: 30 | st.session_state.model_list = ("Aira2-124M", "DolphinQwen2-0.5b", "Gemma1.1-2b", 31 | "H2O-Danube2-1.8","Pythia70M-Chatsalad","StableLMZepyhr-1.6b", 32 | "HerculesQwen2-0.5b","Qwen2-1.5b",) 33 | 34 | if "selected_model" not in st.session_state: 35 | st.session_state.selected_model = '' 36 | 37 | 38 | 39 | @st.cache_resource 40 | def create_chat(): 41 | # Set HF API token and HF repo 42 | from llama_cpp import Llama 43 | qwen05b = Llama( 44 | model_path='models/Aira-2-124M-DPO.Q8_0.gguf', 45 | n_gpu_layers=0, 46 | temperature=0.1, 47 | top_p = 0.5, 48 | n_ctx=2048, 49 | max_tokens=600, 50 | repeat_penalty=1.7, 51 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 52 | verbose=True, 53 | ) 54 | print('loading gemma-2b-zephyr-dpo.Q6_K.gguf with LlamaCPP...') 55 | return qwen05b 56 | 57 | def writehistory(filename,text): 58 | with open(filename, 'a', encoding='utf-8') as f: 59 | f.write(text) 60 | f.write('\n') 61 | f.close() 62 | 63 | 64 | 65 | # Create a header element 66 | mytitle = '

Your own LocalGPT with 🟠 Key Solution AI

' 67 | st.markdown(mytitle, unsafe_allow_html=True) 68 | #st.header("Your own LocalGPT with 🌀 h2o-danube-1.8b-chat") 69 | subtitle = '

Powerwed by Qwen, the best 0.5B chat model?

' 70 | st.markdown(subtitle, unsafe_allow_html=True) 71 | 72 | 73 | def genRANstring(n): 74 | """ 75 | n = int number of char to randomize 76 | """ 77 | N = n 78 | res = ''.join(random.choices(string.ascii_uppercase + 79 | string.digits, k=N)) 80 | return res 81 | 82 | # create THE SESSIoN STATES 83 | if "logfilename" not in st.session_state: 84 | ## Logger file 85 | logfile = f'{genRANstring(5)}_log.txt' 86 | st.session_state.logfilename = logfile 87 | #Write in the history the first 2 sessions 88 | writehistory(st.session_state.logfilename,f'{str(datetime.datetime.now())}\n\nYour own LocalGPT with 🌀 Qwen-0.5b-chat\n---\n🧠🫡: You are a helpful assistant.') 89 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?') 90 | 91 | if "len_context" not in st.session_state: 92 | st.session_state.len_context = 0 93 | 94 | if "limiter" not in st.session_state: 95 | st.session_state.limiter = 0 96 | 97 | if "bufstatus" not in st.session_state: 98 | st.session_state.bufstatus = "**:green[Good]**" 99 | 100 | if "temperature" not in st.session_state: 101 | st.session_state.temperature = 0.1 102 | 103 | if "maxlength" not in st.session_state: 104 | st.session_state.maxlength = 500 105 | 106 | # Point to the local server 107 | llm = create_chat() 108 | 109 | # CREATE THE SIDEBAR 110 | with st.sidebar: 111 | st.image('logo.png', use_column_width=True) 112 | st.session_state.selected_model = st.selectbox( 113 | "Select your model first", 114 | st.session_state.model_list, 115 | index=None, 116 | placeholder="Select contact method...", 117 | ) 118 | st.session_state.temperature = st.slider('Temperature:', min_value=0.0, max_value=1.0, value=0.1, step=0.02) 119 | st.session_state.limiter = st.slider('Turns:', min_value=7, max_value=17, value=12, step=1) 120 | st.session_state.maxlength = st.slider('Length reply:', min_value=150, max_value=1000, 121 | value=500, step=50) 122 | mytokens = st.markdown(f"""**Context turns** {st.session_state.len_context}""") 123 | st.markdown(f"Buffer status: {st.session_state.bufstatus}") 124 | st.markdown(f"Selected Model: **{st.session_state.selected_model}**") 125 | st.markdown(f"**Logfile**: {st.session_state.logfilename}") 126 | btnClear = st.button("Clear History",type="primary", use_container_width=True) 127 | 128 | # We store the conversation in the session state. 129 | # This will be used to render the chat conversation. 130 | # We initialize it with the first message we want to be greeted with. 131 | if "messages" not in st.session_state: 132 | st.session_state.messages = [ 133 | {"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.",}, 134 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 135 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 136 | ] 137 | 138 | def clearHistory(): 139 | st.session_state.messages = [ 140 | {"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.",}, 141 | {"role": "user", "content": "Hi, I am a Key Solution employee, a Company that works in the Oil and Gas sector."}, 142 | {"role": "assistant", "content": "Hi there, I am QWEN-KS, how may I help you today?"} 143 | ] 144 | st.session_state.len_context = len(st.session_state.messages) 145 | if btnClear: 146 | clearHistory() 147 | st.session_state.len_context = len(st.session_state.messages) 148 | 149 | # We loop through each message in the session state and render it as 150 | # a chat message. 151 | for message in st.session_state.messages[1:]: 152 | if message["role"] == "user": 153 | with st.chat_message(message["role"],avatar=av_us): 154 | st.markdown(message["content"]) 155 | else: 156 | with st.chat_message(message["role"],avatar=av_ass): 157 | st.markdown(message["content"]) 158 | 159 | # We take questions/instructions from the chat input to pass to the LLM 160 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"): 161 | 162 | # Add our input to the session state 163 | st.session_state.messages.append( 164 | {"role": "user", "content": user_prompt} 165 | ) 166 | 167 | # Add our input to the chat window 168 | with st.chat_message("user", avatar=av_us): 169 | st.markdown(user_prompt) 170 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}') 171 | 172 | 173 | with st.chat_message("assistant",avatar=av_ass): 174 | message_placeholder = st.empty() 175 | with st.spinner("Thinking..."): 176 | response = '' 177 | conv_messages = [] 178 | st.session_state.len_context = len(st.session_state.messages) 179 | # Checking context window for the LLM, not for the chat history to be displayed 180 | if st.session_state.len_context > st.session_state.limiter: 181 | st.session_state.bufstatus = "**:red[Overflow]**" 182 | # this will keep 5 full turns into consideration 183 | x=st.session_state.limiter-5 184 | conv_messages.append(st.session_state.messages[0]) 185 | for i in range(0,x): 186 | conv_messages.append(st.session_state.messages[-x+i]) 187 | print(len(conv_messages)) 188 | full_response = "" 189 | for chunk in llm.create_chat_completion( 190 | messages=conv_messages, 191 | temperature=st.session_state.temperature, 192 | repeat_penalty= 1.6, 193 | stop=['<|im_end|>','',""], 194 | max_tokens=st.session_state.maxlength, 195 | stream=True,): 196 | try: 197 | if chunk["choices"][0]["delta"]["content"]: 198 | full_response += chunk["choices"][0]["delta"]["content"] 199 | message_placeholder.markdown(full_response + "🟠") 200 | except: 201 | pass 202 | message_placeholder.markdown(full_response) 203 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 204 | else: 205 | st.session_state.bufstatus = "**:green[Good]**" 206 | full_response = "" 207 | for chunk in llm.create_chat_completion( 208 | messages=st.session_state.messages, 209 | temperature=st.session_state.temperature, 210 | repeat_penalty= 1.6, 211 | stop=['<|im_end|>','',""], 212 | max_tokens=st.session_state.maxlength, 213 | stream=True,): 214 | try: 215 | if chunk["choices"][0]["delta"]["content"]: 216 | full_response += chunk["choices"][0]["delta"]["content"] 217 | message_placeholder.markdown(full_response + "🟠") 218 | except: 219 | pass 220 | message_placeholder.markdown(full_response) 221 | writehistory(st.session_state.logfilename,f'🟠: {full_response}\n\n---\n\n') 222 | 223 | # Add the response to the session state 224 | st.session_state.messages.append( 225 | {"role": "assistant", "content": full_response} 226 | ) 227 | st.session_state.len_context = len(st.session_state.messages) 228 | -------------------------------------------------------------------------------- /03.st-SearchDDG2_0.2.0.py: -------------------------------------------------------------------------------- 1 | 2 | # Updated 20240627 Shanghai Time 08:00 3 | # adding streamlit interface to browse web documents 4 | # add 'search_query' : q, in the metadata 5 | # ⚠️ Segregating display datastructure from saved db 🗃️ datastructure 6 | # REV02 - adjusting display of the Document retrieved 7 | # - preparing multi page approach 8 | # - load .ddg 9 | # - merge into a single db 10 | # - build RAG pipeline with selection of the documents 11 | import streamlit as st 12 | import os 13 | from rich.markdown import Markdown 14 | import warnings 15 | warnings.filterwarnings(action='ignore') 16 | from rich.console import Console 17 | from newspaper import Article 18 | import pickle 19 | from langchain.schema.document import Document 20 | console = Console(width=90) 21 | import random 22 | import string 23 | import datetime 24 | 25 | st.set_page_config(page_title="AI powered web serach", layout="wide",page_icon='📱') 26 | 27 | # to write out a log file PLAIN TXT - this will rewrite every time the file 28 | def writehistory(filename,text): 29 | with open(filename, 'w', encoding='utf-8') as f: 30 | f.write(text) 31 | f.write('\n') 32 | f.close() 33 | 34 | # GENERATE RANDOM HASH of n charachters 35 | def genRANstring(n): 36 | """ 37 | n = int number of char to randomize 38 | """ 39 | N = n 40 | res = ''.join(random.choices(string.ascii_uppercase + 41 | string.digits, k=N)) 42 | return res 43 | 44 | # DEFINE FILENAMES 45 | if "sessionHASH" not in st.session_state: 46 | st.session_state.sessionHASH = genRANstring(5) 47 | if "sessionlogfilename" not in st.session_state: 48 | st.session_state.sessionlogfilename = f'{st.session_state.sessionHASH}-log.txt' 49 | if "sessionDBfilename" not in st.session_state: 50 | st.session_state.sessionDBfilename = f'{st.session_state.sessionHASH}_searchDB.ddg' 51 | 52 | # Initialize search history and dataDB / the one to be saved 53 | if "searches" not in st.session_state: 54 | st.session_state.searches = [] 55 | if "dataDB" not in st.session_state: 56 | st.session_state.dataDB = [] 57 | 58 | # Newsletter3k utility disctionary from NLTK 59 | @st.cache_resource 60 | def loadDictionary(): 61 | import nltk 62 | nltk.download('punkt') 63 | 64 | # Network Connector LangChain DuckDuckGo Wrapper 65 | #from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 66 | # https://pypi.org/project/duckduckgo-search 67 | # https://api.python.langchain.com/en/latest/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html 68 | @st.cache_resource 69 | def createWrapper(): 70 | from langchain_community.utilities import DuckDuckGoSearchAPIWrapper 71 | wrapper = DuckDuckGoSearchAPIWrapper(region='us-en', time="y", max_results=10) #time parameter Options: d, w, m, y 72 | return wrapper 73 | 74 | loadDictionary() 75 | if "wrapper" not in st.session_state: 76 | st.session_state.wrapper = createWrapper() 77 | if "searchquery" not in st.session_state: 78 | st.session_state.searchquery = '' 79 | if "limiter" not in st.session_state: 80 | st.session_state.limiter = 0 81 | 82 | def getWebDocuments(q,n): 83 | #wrapper = createWrapper() 84 | console.print(f'[bold red1]Searching for {q}') 85 | console.print(90*'=') 86 | rawdb = st.session_state.wrapper.results(q,max_results=n) 87 | docdocs = [] 88 | mt = 1 89 | numofdocs = len(rawdb) 90 | for items in rawdb: 91 | url = items["link"] 92 | try: #useful if the url is no reachable 93 | article = Article(url) 94 | article.download() 95 | article.parse() 96 | article.nlp() 97 | kw = [] 98 | #we merge nltk keywords and meta webpage keywords 99 | for i in article.keywords+article.meta_keywords: 100 | if i == '': #no blck yeywords for us 101 | pass 102 | else: 103 | kw.append(i) 104 | if article.text == '': #sometimes there is no text to parse. so we use the snippet 105 | docdocs.append(Document(page_content = items["snippet"], metadata = { 106 | 'search_query' : q, 107 | 'source': items["link"], 108 | 'title': items["title"], 109 | 'snippet': items["snippet"], 110 | 'author':article.authors, 111 | 'keywords':kw, 112 | 'meta_description':article.meta_description, 113 | 'meta_img':article.meta_img, 114 | 'top_image':article.top_image, 115 | 'publish_date':article.publish_date, 116 | 'summary':article.summary})) 117 | # Append only the LangchainDocument to the dataDB 118 | st.session_state.dataDB.append(Document(page_content = items["snippet"], metadata = { 119 | 'search_query' : q, 120 | 'source': items["link"], 121 | 'title': items["title"], 122 | 'snippet': items["snippet"], 123 | 'author':article.authors, 124 | 'keywords':kw, 125 | 'meta_description':article.meta_description, 126 | 'meta_img':article.meta_img, 127 | 'top_image':article.top_image, 128 | 'publish_date':article.publish_date, 129 | 'summary':article.summary})) 130 | else: 131 | docdocs.append(Document(page_content = article.text.replace('\n\n',''), metadata = { 132 | 'search_query' : q, 133 | 'source': items["link"], 134 | 'title': items["title"], 135 | 'snippet': items["snippet"], 136 | 'author':article.authors, 137 | 'keywords':kw, 138 | 'meta_description':article.meta_description, 139 | 'meta_img':article.meta_img, 140 | 'top_image':article.top_image, 141 | 'publish_date':article.publish_date, 142 | 'summary':article.summary})) 143 | # Append only the LangchainDocument to the dataDB 144 | st.session_state.dataDB.append(Document(page_content = article.text.replace('\n\n',''), metadata = { 145 | 'search_query' : q, 146 | 'source': items["link"], 147 | 'title': items["title"], 148 | 'snippet': items["snippet"], 149 | 'author':article.authors, 150 | 'keywords':kw, 151 | 'meta_description':article.meta_description, 152 | 'meta_img':article.meta_img, 153 | 'top_image':article.top_image, 154 | 'publish_date':article.publish_date, 155 | 'summary':article.summary})) 156 | console.print(f'Prepared Document n.{mt} out of {numofdocs}') 157 | mt +=1 158 | except: 159 | pass 160 | st.session_state.searches.append({'role': 'results','results': docdocs}) 161 | 162 | #################### MAIN STREaMLIT APP ################################## 163 | st.title("AI powered Web Document Search") 164 | st.write('Using Newspaper3k and DuckDuckGo LangChain wrapper') 165 | 166 | # Save the searches into plain TXT file 167 | def savelog(db): 168 | log = [] 169 | finaltext = '' 170 | for section in db: 171 | singlesearch = '' 172 | #console.print(section) 173 | if section['role'] == 'query': 174 | header1 = f"##### 🪄 Query: *{section['query']}*" 175 | singlesearch = singlesearch + header1 + '\n' 176 | else: 177 | header2 = '##### 🗃️ Web Documents' 178 | singlesearch = singlesearch + header2 + '\n' 179 | for items in section['results']: 180 | #console.print(section['results']) 181 | singlesearch = singlesearch + '📝 ' + f"{items.metadata['title']}\n" 182 | singlesearch = singlesearch + items.page_content + '\n--\n' 183 | singlesearch = singlesearch + f"""'source': {items.metadata['source']} 184 | 'title': {items.metadata['title']} 185 | 'snippet': {items.metadata['snippet']} 186 | 'author':{items.metadata['author']} 187 | 'keywords':{items.metadata['keywords']} 188 | 'meta_description':{items.metadata['meta_description']} 189 | 'meta_img':{items.metadata['meta_img']} 190 | 'top_image':{items.metadata['top_image']} 191 | 'publish_date':{items.metadata['publish_date']} 192 | 'summary':{items.metadata['summary']} 193 | ------------------------//---------------------------\n""" 194 | log.append(singlesearch) 195 | for i in log: 196 | finaltext = finaltext + i 197 | writehistory(st.session_state.sessionlogfilename,finaltext) 198 | 199 | def savedb(db): 200 | output = open(st.session_state.sessionDBfilename, 'wb') 201 | pickle.dump(db, output) 202 | output.close() 203 | 204 | 205 | # CREATE THE SIDEBAR 206 | with st.sidebar: 207 | st.image('logoDDG.png', use_column_width=True) 208 | st.session_state.limiter = st.slider('N.of Docs:', min_value=1, max_value=5, value=3, step=1) 209 | st.markdown(f"**_DB name**: {st.session_state.sessionDBfilename}") 210 | st.markdown(f"**___Logfile**: {st.session_state.sessionlogfilename}") 211 | logevents = st.empty() 212 | dbevents = st.empty() 213 | btnSAVE = st.button("Save DB",type="primary", use_container_width=True) 214 | 215 | # CREATE SEARCH FORM 216 | with st.form("my_form"): 217 | st.image('searchddglogo.png',) 218 | query = st.text_input( 219 | "What do you want to search?", 220 | placeholder="your query here", 221 | label_visibility='collapsed') 222 | # Every form must have a submit button. 223 | submitted = st.form_submit_button("Search") 224 | if submitted: 225 | with st.spinner(text="Search in progress..."): 226 | st.session_state.searchquery = query 227 | st.session_state.searches.append({'role' : 'query','query': query}) 228 | getWebDocuments(query,st.session_state.limiter) 229 | savelog(st.session_state.searches) 230 | #logevents.success('Search saved') 231 | st.toast('Search saved!', icon='🎉') 232 | 233 | st.markdown('### AI serach results') 234 | with st.container(height=500, border=True): 235 | if len(st.session_state.searches) >0: 236 | for section in st.session_state.searches: 237 | #console.print(section) 238 | if section['role'] == 'query': 239 | st.write(f"##### 🪄 Query: *{section['query']}*") 240 | else: 241 | with st.container(): 242 | st.markdown('##### 🗃️ Web Documents') 243 | for items in section['results']: 244 | #console.print(section['results']) 245 | with st.expander(label=items.metadata['title'], expanded=False, icon='📝'): 246 | st.image(items.metadata['top_image'], width=350) 247 | st.write(f"source url: {items.metadata['source']}\n\nSummary: {items.metadata['summary']}") 248 | st.write(items.page_content) 249 | st.divider() 250 | st.write(items.metadata) 251 | st.divider() 252 | if btnSAVE: 253 | savedb(st.session_state.searches) 254 | #dbevents.success('dB saved') 255 | st.toast('dB saved!', icon='🎉') 256 | 257 | console.print(Markdown("> SEARCH COMPLETED...")) 258 | console.print(" - ") 259 | 260 | 261 | -------------------------------------------------------------------------------- /testcpp/testCPP.py: -------------------------------------------------------------------------------- 1 | from rich.markdown import Markdown 2 | import warnings 3 | warnings.filterwarnings(action='ignore') 4 | from rich.console import Console 5 | console = Console(width=100) 6 | 7 | 8 | from llama_cpp import Llama 9 | modelfile = 'h2o-danube2-1.8b-chat-Q5_K_M.gguf' 10 | modelpath = 'models/'+modelfile 11 | modelname = 'h2o-danube2-1.8b-chat' 12 | model_nctx = 8192 13 | train_nctx = 8192 14 | stops = [''] 15 | chat_template = True 16 | repo = 'https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF' 17 | original_repo = 'https://huggingface.co/h2oai/h2o-danube2-1.8b-chat-GGUF' 18 | basemodel = 'llama' 19 | numOfParams = '1.8b' 20 | prompt_format = """ 21 | <|prompt|>Why is drinking water so healthy?<|answer|> 22 | """ 23 | llm = Llama( 24 | model_path=modelpath, 25 | n_gpu_layers=0, 26 | temperature=0.1, 27 | top_p = 0.5, 28 | n_ctx=model_nctx, 29 | max_tokens=600, 30 | repeat_penalty=1.44, 31 | stop=stops, 32 | verbose=True, 33 | ) 34 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 35 | console.rule(f"[bold turquoise2]{modelname}") 36 | #console.print(Markdown(f'## Model Name: {modelname}')) 37 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 38 | console.rule("[bold red2]Details") 39 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 40 | console.print(f'Stop Tokens: [red1]{stops}') 41 | console.print(f'This model has Chat Template? {chat_template}') 42 | console.rule() 43 | console.print(Markdown(f'### PROMPT FORMAT:')) 44 | promptmarkdown = f""" 45 | ``` 46 | {prompt_format} 47 | ``` 48 | """ 49 | console.print(Markdown(promptmarkdown)) 50 | console.rule() 51 | console.print(Markdown(f'### Additional info:')) 52 | promptmarkdown = f""" 53 | ``` 54 | GGUF Repo: {repo} 55 | Original Repo: {original_repo} 56 | Model architecture: {basemodel} 57 | Number of parameters: {numOfParams} 58 | ``` 59 | 60 | """ 61 | console.print(Markdown(promptmarkdown)) 62 | console.rule(characters='=') 63 | console.print('\n\n\n\n') 64 | 65 | a = input('press a key to exit...') 66 | del llm 67 | 68 | ######################################################################### 69 | 70 | ''' 71 | from llama_cpp import Llama 72 | modelfile = 'stablelm-2-zephyr-1_6b-Q5_K_M.gguf' 73 | modelpath = 'models/'+modelfile 74 | modelname = 'stablelm-2-zephyr-1_6b' 75 | model_nctx = 4096 76 | train_nctx = 4096 77 | stops = ['<|endoftext|>'] 78 | chat_template = True 79 | repo = 'https://huggingface.co/second-state/stablelm-2-zephyr-1.6b-GGUF/tree/main' 80 | original_repo = 'https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b' 81 | basemodel = 'stablelm' 82 | numOfParams = '1.6b' 83 | prompt_format = """ 84 | <|user|> 85 | {prompt}<|endoftext|> 86 | <|assistant|> 87 | """ 88 | llm = Llama( 89 | model_path=modelpath, 90 | n_gpu_layers=0, 91 | temperature=0.1, 92 | top_p = 0.5, 93 | n_ctx=model_nctx, 94 | max_tokens=600, 95 | repeat_penalty=1.44, 96 | stop=stops, 97 | verbose=True, 98 | ) 99 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 100 | console.rule(f"[bold turquoise2]{modelname}") 101 | #console.print(Markdown(f'## Model Name: {modelname}')) 102 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 103 | console.rule("[bold red2]Details") 104 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 105 | console.print(f'Stop Tokens: [red1]{stops}') 106 | console.print(f'This model has Chat Template? {chat_template}') 107 | console.rule() 108 | console.print(Markdown(f'### PROMPT FORMAT:')) 109 | promptmarkdown = f""" 110 | ``` 111 | {prompt_format} 112 | ``` 113 | """ 114 | console.print(Markdown(promptmarkdown)) 115 | console.rule() 116 | console.print(Markdown(f'### Additional info:')) 117 | promptmarkdown = f""" 118 | ``` 119 | GGUF Repo: {repo} 120 | Original Repo: {original_repo} 121 | Model architecture: {basemodel} 122 | Number of parameters: {numOfParams} 123 | ``` 124 | 125 | """ 126 | console.print(Markdown(promptmarkdown)) 127 | console.rule(characters='=') 128 | console.print('\n\n\n\n') 129 | 130 | a = input('press a key to exit...') 131 | del llm 132 | ''' 133 | 134 | ############################################################################ 135 | ''' 136 | from llama_cpp import Llama 137 | modelfile = 'stablelm-2-zephyr-1_6b-Q5_K_M.gguf' 138 | modelpath = 'models/'+modelfile 139 | modelname = 'stablelm-2-zephyr-1_6b' 140 | model_nctx = 4096 141 | train_nctx = 4096 142 | stops = ['<|endoftext|>'] 143 | chat_template = True 144 | repo = 'https://huggingface.co/second-state/stablelm-2-zephyr-1.6b-GGUF/tree/main' 145 | original_repo = 'https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b' 146 | basemodel = 'stablelm' 147 | numOfParams = '1.6b' 148 | prompt_format = """ 149 | <|user|> 150 | {prompt}<|endoftext|> 151 | <|assistant|> 152 | """ 153 | llm = Llama( 154 | model_path=modelpath, 155 | n_gpu_layers=0, 156 | temperature=0.1, 157 | top_p = 0.5, 158 | n_ctx=model_nctx, 159 | max_tokens=600, 160 | repeat_penalty=1.44, 161 | stop=stops, 162 | verbose=True, 163 | ) 164 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 165 | console.rule(f"[bold turquoise2]{modelname}") 166 | #console.print(Markdown(f'## Model Name: {modelname}')) 167 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 168 | console.rule("[bold red2]Details") 169 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 170 | console.print(f'Stop Tokens: [red1]{stops}') 171 | console.print(f'This model has Chat Template? {chat_template}') 172 | console.rule() 173 | console.print(Markdown(f'### PROMPT FORMAT:')) 174 | promptmarkdown = f""" 175 | ``` 176 | {prompt_format} 177 | ``` 178 | """ 179 | console.print(Markdown(promptmarkdown)) 180 | console.rule() 181 | console.print(Markdown(f'### Additional info:')) 182 | promptmarkdown = f""" 183 | ``` 184 | GGUF Repo: {repo} 185 | Original Repo: {original_repo} 186 | Model architecture: {basemodel} 187 | Number of parameters: {numOfParams} 188 | ``` 189 | 190 | """ 191 | console.print(Markdown(promptmarkdown)) 192 | console.rule(characters='=') 193 | console.print('\n\n\n\n') 194 | 195 | a = input('press a key to exit...') 196 | del llm 197 | 198 | ''' 199 | ''' 200 | from llama_cpp import Llama 201 | modelpath = 'models/Aira-2-355M.Q6_K.gguf' 202 | modelfile = 'Aira-2-355M.Q6_K.gguf' 203 | modelname = 'Aira-2-355M' 204 | model_nctx = 1024 205 | train_nctx = 1024 206 | stops = ['<|endoftext|>'] 207 | chat_template = False 208 | repo = 'https://huggingface.co/Felladrin/gguf-Aira-2-355M/tree/main' 209 | original_repo = 'https://huggingface.co/nicholasKluge/Aira-2-355M' 210 | basemodel = 'gpt2' 211 | numOfParams = '355M' 212 | prompt_format = """ 213 | <|startofinstruction|>What is a language model?<|endofinstruction|> 214 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 215 | 216 | 217 | Recommended inference parameters 218 | penalty_alpha: 0.5 219 | top_k: 2 220 | repetition_penalty: 1.0016 221 | """ 222 | llm = Llama( 223 | model_path=modelpath, 224 | n_gpu_layers=0, 225 | temperature=0.1, 226 | top_p = 0.5, 227 | n_ctx=model_nctx, 228 | max_tokens=600, 229 | repeat_penalty=1.44, 230 | stop=stops, 231 | verbose=True, 232 | ) 233 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 234 | console.rule(f"[bold turquoise2]{modelname}") 235 | #console.print(Markdown(f'## Model Name: {modelname}')) 236 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 237 | console.rule("[bold red2]Details") 238 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 239 | console.print(f'Stop Tokens: [red1]{stops}') 240 | console.print(f'This model has Chat Template? {chat_template}') 241 | console.rule() 242 | console.print(Markdown(f'### PROMPT FORMAT:')) 243 | promptmarkdown = f""" 244 | ``` 245 | {prompt_format} 246 | ``` 247 | """ 248 | console.print(Markdown(promptmarkdown)) 249 | console.rule() 250 | console.print(Markdown(f'### Additional info:')) 251 | promptmarkdown = f""" 252 | ``` 253 | GGUF Repo: {repo} 254 | Original Repo: {original_repo} 255 | Model architecture: {basemodel} 256 | Number of parameters: {numOfParams} 257 | ``` 258 | 259 | """ 260 | console.print(Markdown(promptmarkdown)) 261 | console.rule(characters='=') 262 | console.print('\n\n\n\n') 263 | 264 | a = input('press a key to exit...') 265 | del llm 266 | ''' 267 | 268 | 269 | ##################################################################################### 270 | ''' 271 | from llama_cpp import Llama 272 | modelpath = 'models/smol-llama-101m-chat-v1.q8_0.gguf' 273 | modelfile = 'smol-llama-101m-chat-v1.q8_0.gguf' 274 | modelname = 'smol-llama-101m-chat-v1' 275 | model_nctx = 1024 276 | train_nctx = 1024 277 | stops = [''] 278 | chat_template = True 279 | repo = 'https://huggingface.co/afrideva/Smol-Llama-101M-Chat-v1-GGUF' 280 | original_repo = 'https://huggingface.co/Felladrin/Smol-Llama-101M-Chat-v1' 281 | basemodel = 'llama' 282 | numOfParams = '101M' 283 | prompt_format = """ 284 | <|im_start|>system 285 | {system_message}<|im_end|> 286 | <|im_start|>user 287 | {user_message}<|im_end|> 288 | <|im_start|>assistant 289 | 290 | Recommended inference parameters 291 | penalty_alpha: 0.5 292 | top_k: 4 293 | repetition_penalty: 1.105 294 | """ 295 | llm = Llama( 296 | model_path=modelpath, 297 | n_gpu_layers=0, 298 | temperature=0.1, 299 | top_p = 0.5, 300 | n_ctx=model_nctx, 301 | max_tokens=350, 302 | repeat_penalty=1.44, 303 | stop=stops, 304 | verbose=True, 305 | ) 306 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 307 | console.rule(f"[bold turquoise2]{modelname}") 308 | #console.print(Markdown(f'## Model Name: {modelname}')) 309 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 310 | console.rule("[bold red2]Details") 311 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 312 | console.print(f'Stop Tokens: [red1]{stops}') 313 | console.print(f'This model has Chat Template? {chat_template}') 314 | console.rule() 315 | console.print(Markdown(f'### PROMPT FORMAT:')) 316 | promptmarkdown = f""" 317 | ``` 318 | {prompt_format} 319 | ``` 320 | """ 321 | console.print(Markdown(promptmarkdown)) 322 | console.rule() 323 | console.print(Markdown(f'### Additional info:')) 324 | promptmarkdown = f""" 325 | ``` 326 | GGUF Repo: {repo} 327 | Original Repo: {original_repo} 328 | Model architecture: {basemodel} 329 | Number of parameters: {numOfParams} 330 | ``` 331 | 332 | """ 333 | console.print(Markdown(promptmarkdown)) 334 | console.rule(characters='=') 335 | console.print('\n\n\n\n') 336 | 337 | a = input('press a key to exit...') 338 | del llm 339 | 340 | ''' 341 | #################################################################################### 342 | ''' 343 | from llama_cpp import Llama 344 | modelpath = 'models/Pythia-31M-Chat-v1.Q8_0.gguf' 345 | modelfile = 'Pythia-31M-Chat-v1.Q8_0.gguf' 346 | modelname = 'Pythia-31M-Chat-v1' 347 | model_nctx = 1024 348 | train_nctx = 2048 349 | stops = ['<|endoftext|>'] 350 | chat_template = True 351 | repo = 'https://huggingface.co/Felladrin/gguf-Pythia-31M-Chat-v1/tree/main' 352 | original_repo = 'https://huggingface.co/Felladrin/Pythia-31M-Chat-v1' 353 | basemodel = 'gptneox' 354 | numOfParams = '31M' 355 | prompt_format = """ 356 | <|im_start|>system 357 | {system_message}<|im_end|> 358 | <|im_start|>user 359 | {user_message}<|im_end|> 360 | <|im_start|>assistant 361 | 362 | 363 | Recommended inference parameters 364 | penalty_alpha: 0.5 365 | top_k: 2 366 | repetition_penalty: 1.0016 367 | """ 368 | llm = Llama( 369 | model_path=modelpath, 370 | n_gpu_layers=0, 371 | temperature=0.1, 372 | top_p = 0.5, 373 | n_ctx=model_nctx, 374 | max_tokens=600, 375 | repeat_penalty=1.44, 376 | stop=stops, 377 | verbose=True, 378 | ) 379 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 380 | console.rule(f"[bold turquoise2]{modelname}") 381 | #console.print(Markdown(f'## Model Name: {modelname}')) 382 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 383 | console.rule("[bold red2]Details") 384 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 385 | console.print(f'Stop Tokens: [red1]{stops}') 386 | console.print(f'This model has Chat Template? {chat_template}') 387 | console.rule() 388 | console.print(Markdown(f'### PROMPT FORMAT:')) 389 | promptmarkdown = f""" 390 | ``` 391 | {prompt_format} 392 | ``` 393 | """ 394 | console.print(Markdown(promptmarkdown)) 395 | console.rule() 396 | console.print(Markdown(f'### Additional info:')) 397 | promptmarkdown = f""" 398 | ``` 399 | GGUF Repo: {repo} 400 | Original Repo: {original_repo} 401 | Model architecture: {basemodel} 402 | Number of parameters: {numOfParams} 403 | ``` 404 | 405 | """ 406 | console.print(Markdown(promptmarkdown)) 407 | console.rule(characters='=') 408 | console.print('\n\n\n\n') 409 | 410 | a = input('press a key to exit...') 411 | del llm 412 | 413 | ###################################################################################### 414 | from llama_cpp import Llama 415 | modelpath = 'models/Aira-2-124M-DPO.Q8_0.gguf' 416 | modelfile = 'Aira-2-124M-DPO.Q8_0.gguf' 417 | modelname = 'Aira-2-124M-DPO' 418 | model_nctx = 1024 419 | train_nctx = 1024 420 | stops = ['<|endofcompletion|>'] 421 | chat_template = False 422 | repo = 'https://huggingface.co/Felladrin/gguf-Aira-2-124M-DPO' 423 | original_repo = 'https://huggingface.co/nicholasKluge/Aira-2-124M-DPO' 424 | prompt_format = """ 425 | <|startofinstruction|>What is a language model?<|endofinstruction|> 426 | A language model is a probability distribution over a vocabulary.<|endofcompletion|> 427 | """ 428 | basemodel = 'gpt2' 429 | numOfParams = '124M' 430 | 431 | llm = Llama( 432 | model_path=modelpath, 433 | n_gpu_layers=0, 434 | temperature=0.1, 435 | top_p = 0.5, 436 | n_ctx=model_nctx, 437 | max_tokens=600, 438 | repeat_penalty=1.44, 439 | stop=stops, 440 | verbose=True, 441 | ) 442 | console.print(f'loading {modelname} with LlamaCPP...\n\n\n\n\n') 443 | console.rule(f"[bold turquoise2]{modelname}") 444 | #console.print(Markdown(f'## Model Name: {modelname}')) 445 | console.print('[italic]Model File[/italic]: ' + f'[light_steel_blue1 bold]{modelfile}') 446 | console.rule("[bold red2]Details") 447 | console.print(f'[bold green1]Training Context Window: {train_nctx}') 448 | console.print(f'Stop Tokens: [red1]{stops}') 449 | console.print(f'This model has Chat Template? {chat_template}') 450 | console.rule() 451 | console.print(Markdown(f'### PROMPT FORMAT:')) 452 | promptmarkdown = f""" 453 | ``` 454 | {prompt_format} 455 | ``` 456 | """ 457 | console.print(Markdown(promptmarkdown)) 458 | console.rule() 459 | console.print(Markdown(f'### Additional info:')) 460 | promptmarkdown = f""" 461 | ``` 462 | GGUF Repo: {repo} 463 | Original Repo: {original_repo} 464 | Model architecture: {basemodel} 465 | Number of parameters: {numOfParams} 466 | ``` 467 | 468 | """ 469 | console.print(Markdown(promptmarkdown)) 470 | console.rule(characters='=') 471 | console.print('\n\n\n\n') 472 | 473 | a = input('press a key to exit...') 474 | del llm 475 | ###################################################################################### 476 | 477 | 478 | 479 | ''' -------------------------------------------------------------------------------- /what_is_NVIDIA_Deep_Learning_Institute.txt: -------------------------------------------------------------------------------- 1 | ======================================= 2 | AI is quickly becoming an integral part of diverse industries, from transportation and healthcare to manufacturing and finance. AI powers chatbots, recommender systems, computer vision applications, fraud prevention, and autonomous vehicles. It also has broad applications in engineering and science. 3 | Physics-informed machine learning (physics-ML) leverages knowledge of the physical world to train AI models. It is well suited for modeling real-world systems — some applications include: predicting extreme weather, data center cooling, turbulent flow over a car, and protein modeling. 4 | Figure 1. Physics-ML is used to model physical systems such as weather, ocean currents, and tidal activity 5 | Academic institutions play a pivotal role in nurturing emerging technologies and driving the innovation needed for their widespread adoption. There is no denying that today’s students who want to succeed in tomorrow’s workplace need to understand how AI can enable solutions. 6 | To support this work, NVIDIA is collaborating with pioneers at the intersection of science, engineering, and AI to create the first Deep Learning for Science and Engineering Teaching Kit for educators in academia worldwide. 7 | This new teaching kit will enable the next generation of engineers and scientists to leverage AI for innovation in the field of engineering and science. It was created with leading academics including George Karniadakis, professor of Applied Mathematics and Engineering at Brown University, and his team. 8 | “We designed this course with my collaborator, Dr. Raj Shukla, to address the urgent need for specific material for scientists and engineers,” said Karniadakis. “We focused on regression and mimicking the approximation theory and algorithms required in classical numerical analysis courses in the engineering curriculum.” 9 | Educators can get full, free access to the Deep Learning for Science and Engineering Teaching Kit (and many more) by joining the NVIDIA DLI Teaching Kit Program. Kits include lecture materials, labs, and sample problem sets to facilitate incorporating advanced technology into course curriculums. The entire lecture portion of the course is also available on NVIDIA On-Demand. 10 | Overview of the Science and Engineering Teaching Kit 11 | Educators in various fields will find this teaching kit useful, from mechanical, structural, and electrical engineering to atmospheric science, computational science, and more. Materials in the kit include the fundamentals of deep learning as well as advanced topics and hands-on exercises. It contains 15 lectures totaling about 30-35 hours, homework, and 20 projects across different fields. 12 | The Deep Learning for Science and Engineering Teaching Kit contains focused modules that combine theory, algorithms, programming, and examples. Highlights include: 13 | A primer on Python plus scientific and deep learning libraries 14 | Deep neural network architectures, training, and optimization 15 | Physics-informed neural networks 16 | Neural operators 17 | Data and uncertainty quantification 18 | High-performance computing (HPC) and the NVIDIA Modulus open-source framework 19 | This content is ideal for educators in engineering and science. The modular design enables instructors to develop their own custom version of the course to suit the needs of their students. 20 | The teaching kit includes dedicated modules for physics-ML, due to its potential to transform HPC simulation workflows across disciplines. These include multi-disciplinary physics including computational fluid dynamics, structural mechanics, and computational chemistry. Because of its broad applicability across science and engineering domains, physics-ML is well suited for modeling real-world multiphysics systems. 21 | AI surrogate models can help develop a wide range of solutions including weather forecasting, reducing power plant greenhouse gasses, and accelerating clean energy transitions. Such physically consistent surrogate models can underpin the deployment of large-scale digital twins of real-world systems. 22 | “To this end,” Karniadakis said, “the focus of the course is how to solve forward and inverse problems given sparse and noisy data, how to discover governing physical laws, how to construct proper surrogate models for digital twins, and how to quantify uncertainties associated with models and data.” 23 | The kit leverages the open-source NVIDIA Modulus framework with hands-on tutorials for project-based learning. Modulus enables engineering and scientific communities that may not have AI or programming expertise. With a Python-based interface, Modulus provides the right tools to combine the governing partial differential equations and other attributes of the problem like the physical geometry and boundary conditions with the training dataset in a simple way. 24 | It also provides a variety of reference applications as starting points, across domains (computational fluid dynamics, structures, thermal) applied to problems in different segments from manufacturing to healthcare. To learn more, see Physics-Informed Machine Learning Platform NVIDIA Modulus Is Now Open Source. 25 | Given the rapid pace of change in the field of AI, educators can anticipate that the teaching material will be updated, as needed. NVIDIA is committed to providing the best educational materials, and feedback is welcome. 26 | “The NVIDIA Teaching Kit on physics-ML has provided me with great resources for use in my machine learning course targeted for our engineering students,” said Hadi Meidani, associate professor of Civil & Environmental Engineering at University of Illinois Urbana-Champaign. “The examples and code greatly enable hands-on learning experiences on how machine learning is applied to scientific and engineering problems.” 27 | Get started 28 | Educators can get full, free access to the Deep Learning for Science and Engineering Teaching Kit (and many more) by joining the NVIDIA DLI Teaching Kit Program. The entire lecture part of the course is also openly available through NVIDIA On-Demand. 29 | For access to tutorial content, reach out to the Modulus team. To get started with NVIDIA Modulus hands-on experience, see Introduction to Physics-Informed Machine Learning with Modulus. 30 | --------------------- 31 | 'source': https://developer.nvidia.com/blog/nvidia-deep-learning-institute-launches-science-and-engineering-teaching-kit/, 32 | 'title': NVIDIA Deep Learning Institute Launches Science and Engineering ..., 33 | 'snippet': The Deep Learning for Science and Engineering Teaching Kit contains focused modules that combine theory, algorithms, programming, and examples. Highlights include: A primer on Python plus scientific and deep learning libraries. Deep neural network architectures, training, and optimization. Physics-informed neural networks., 34 | 'author':['Ram Cherukuri', 'Mohammad Nabian', 'View All Posts Ram Cherukuri', 'View All Posts Mohammad Nabian'], 35 | 'keywords':['kit', 'learning', 'science', 'nvidia', 'course', 'engineering', 'ai', 'institute', 'teaching', 'launches', 'modulus', 'deep'], 36 | 'meta_description':AI is quickly becoming an integral part of diverse industries, from transportation and healthcare to manufacturing and finance. AI powers chatbots…, 37 | 'meta_img':https://developer-blogs.nvidia.com/wp-content/uploads/2023/10/nvidia-deep-learning-institute-teaching-kit-logo.jpg, 38 | 'top_image':https://developer-blogs.nvidia.com/wp-content/uploads/2023/10/nvidia-deep-learning-institute-teaching-kit-logo.jpg, 39 | 'publish_date':2023-11-13 17:30:00+00:00, 40 | 'summary':To support this work, NVIDIA is collaborating with pioneers at the intersection of science, engineering, and AI to create the first Deep Learning for Science and Engineering Teaching Kit for educators in academia worldwide. 41 | “We focused on regression and mimicking the approximation theory and algorithms required in classical numerical analysis courses in the engineering curriculum.”Educators can get full, free access to the Deep Learning for Science and Engineering Teaching Kit (and many more) by joining the NVIDIA DLI Teaching Kit Program. 42 | Overview of the Science and Engineering Teaching KitEducators in various fields will find this teaching kit useful, from mechanical, structural, and electrical engineering to atmospheric science, computational science, and more. 43 | The Deep Learning for Science and Engineering Teaching Kit contains focused modules that combine theory, algorithms, programming, and examples. 44 | “The examples and code greatly enable hands-on learning experiences on how machine learning is applied to scientific and engineering problems.”Get startedEducators can get full, free access to the Deep Learning for Science and Engineering Teaching Kit (and many more) by joining the NVIDIA DLI Teaching Kit Program. 45 | --------------------------------------------------- 46 | ======================================= 47 | NVIDIA is offering new certifications in generative AI to enable developers to establish technical credibility in this important domain. 48 | Generative AI is revolutionizing industries worldwide, yet there’s a critical skills gap and need to uplevel employees to harness the technology more fully. 49 | Available for the first time from NVIDIA, this new certification enables developers, career professionals, and others to validate and showcase their generative AI skills and expertise. Our new certification program introduces two associate-level generative AI certifications, focusing on proficiency in large language models and multimodal workflow skills. 50 | “Generative AI has moved to center stage as governments, industries and organizations everywhere look to harness its transformative capabilities,” NVIDIA founder and CEO Jensen Huang recently said. 51 | The certification will become available starting at GTC, where in-person attendees can also access recommended training to prepare for a certification exam. 52 | “Organizations in every industry need to increase their expertise in this transformative technology,” said Greg Estes, vice president of developer programs at NVIDIA. “Our goals are to assist in upskilling workforces, sharpen the skills of qualified professionals, and enable individuals to demonstrate their proficiency in order to gain a competitive advantage in the job market.” 53 | The first two gen AI certifications are entry-level credentials designed to validate foundational skills for developing, integrating and maintaining applications using generative AI, large language models and multimodal models with NVIDIA solutions. 54 | See AI’s Future. Learn How to Use It. 55 | GTC 2024 — running March 18-21 in San Jose, Calif. — is the first in-person GTC event in five years, and more than 300,000 people are expected to register to attend in person or virtually. There will be 900 sessions and more than 300 exhibitors showcasing how organizations are deploying NVIDIA platforms to achieve industry breakthroughs. 56 | Attendees can choose from 20 full-day, hands-on technical workshops, with many sessions available virtually in EMEA and APAC time zones. Also, sign up for the GTC Conference + Training package for more than 40 complimentary onsite training labs. 57 | Learn more about the generative AI certification. 58 | --------------------- 59 | 'source': https://blogs.nvidia.com/blog/generative-ai-certification/, 60 | 'title': NVIDIA Introduces Generative AI Certification | NVIDIA Blog, 61 | 'snippet': NVIDIA is offering new certifications in generative AI to enable developers to establish technical credibility in this important domain. Generative AI is revolutionizing industries worldwide, yet there's a critical skills gap and need to uplevel employees to harness the technology more fully. Available for the first time from NVIDIA, this new ..., 62 | 'author':['Craig Clawson', '.Wp-Block-Co-Authors-Plus-Coauthors.Is-Layout-Flow', 'Class', 'Wp-Block-Co-Authors-Plus', 'Display Inline', '.Wp-Block-Co-Authors-Plus-Avatar', 'Where Img', 'Height Auto Max-Width', 'Vertical-Align Bottom .Wp-Block-Co-Authors-Plus-Coauthors.Is-Layout-Flow .Wp-Block-Co-Authors-Plus-Avatar', 'Vertical-Align Middle .Wp-Block-Co-Authors-Plus-Avatar Is .Alignleft .Alignright'], 63 | 'keywords':['certification', 'models', 'certifications', 'gtc', 'introduces', 'training', 'nvidia', 'generative', 'ai', 'skills', 'class', 'virtually'], 64 | 'meta_description':NVIDIA is offering new certifications in generative AI to enable developers to establish technical credibility in this important domain., 65 | 'meta_img':https://blogs.nvidia.com/wp-content/uploads/2024/03/nvt-general-AI-certification-no-copy-social-1200x675-1.jpg, 66 | 'top_image':https://blogs.nvidia.com/wp-content/uploads/2024/03/nvt-general-AI-certification-no-copy-social-1200x675-1.jpg, 67 | 'publish_date':2024-03-07 17:02:33+00:00, 68 | 'summary':NVIDIA is offering new certifications in generative AI to enable developers to establish technical credibility in this important domain. 69 | Generative AI is revolutionizing industries worldwide, yet there’s a critical skills gap and need to uplevel employees to harness the technology more fully. 70 | Available for the first time from NVIDIA, this new certification enables developers, career professionals, and others to validate and showcase their generative AI skills and expertise. 71 | Our new certification program introduces two associate-level generative AI certifications, focusing on proficiency in large language models and multimodal workflow skills. 72 | Learn more about the generative AI certification. 73 | --------------------------------------------------- 74 | ======================================= 75 | NVIDIA websites use cookies to deliver and improve the website experience. See our cookie policy for further details on how we use cookies and how to change your cookie settings. 76 | --------------------- 77 | 'source': https://resources.nvidia.com/en-us-modulus-pathfactory/science-engineering-kit, 78 | 'title': NVIDIA Deep Learning Institute Launches Science and Engineering ..., 79 | 'snippet': NVIDIA is collaborating with academic experts to create a Deep Learning for Science and Engineering Teaching Kit, aiming to equip educators with materials for incorporating AI into engineering and science curricula. The kit covers fundamental and advanced topics, including physics-informed machine learning, leveraging the open-source NVIDIA Modulus framework, and is designed for various ..., 80 | 'author':[], 81 | 'keywords':['experience', 'kit', 'learning', 'science', 'websites', 'settings', 'improve', 'policy', 'details', 'nvidia', 'website', 'engineering', 'institute', 'teaching', 'launches', 'deep', 'cookie', 'cookies'], 82 | 'meta_description':NVIDIA is collaborating with academic experts to create a Deep Learning for Science and Engineering Teaching Kit, aiming to equip educators with materials for incorporating AI into engineering and science curricula. The kit covers fundamental and advanced topics, including physics-informed machine, 83 | 'meta_img':https://cdn.pathfactory.com/assets/10412/contents/558456/thumbnails/600x/nvidia-deep-learning-institute-teaching-kit-logo.jpg, 84 | 'top_image':https://cdn.pathfactory.com/assets/10412/contents/558456/thumbnails/600x/nvidia-deep-learning-institute-teaching-kit-logo.jpg, 85 | 'publish_date':None, 86 | 'summary':NVIDIA websites use cookies to deliver and improve the website experience. 87 | See our cookie policy for further details on how we use cookies and how to change your cookie settings. 88 | --------------------------------------------------- 89 | ======================================= 90 | At NVIDIA, you’ll solve some of the world’s hardest problems and discover never-before-seen ways to improve the quality of life for people everywhere. From healthcare to robots, self-driving cars to blockbuster movies, the list of new opportunities is growing every single day. Explore all of our open roles, including internships and new college graduate positions. 91 | 92 | Come meet the NVIDIA team at the SC23 job fair at table JF15. 93 | --------------------- 94 | 'source': https://www.nvidia.com/en-us/events/supercomputing/, 95 | 'title': NVIDIA at Supercomputing 2023(SC23) | NVIDIA, 96 | 'snippet': Get Hands-On Training With the NVIDIA Deep Learning Institute Develop the skills you need to do your life's work in accelerated computing, data science, and AI and earn certificates of subject-matter competency through the NVIDIA Deep Learning Institute (DLI) ., 97 | 'author':[], 98 | 'keywords':['supercomputing', 'table', 'selfdriving', 'solve', 'youll', 'sc23', 'ways', 'team', 'worlds', 'nvidia', 'single', '2023', 'NVIDIA at Supercomputing 2023', 'NVIDIA', 'SC23', 'November 12–17', '2023', 'virtual theater'], 99 | 'meta_description':Join us online at Supercomputing 2023 (SC23) to see how GPU computing and high-performance networking are transforming computational science and AI., 100 | 'meta_img':https://www.nvidia.com/content/dam/en-zz/Solutions/events/supercomputing/2023/nv-sc23-og.jpg, 101 | 'top_image':https://www.nvidia.com/content/dam/en-zz/Solutions/events/supercomputing/2023/nv-sc23-og.jpg, 102 | 'publish_date':None, 103 | 'summary':At NVIDIA, you’ll solve some of the world’s hardest problems and discover never-before-seen ways to improve the quality of life for people everywhere. 104 | From healthcare to robots, self-driving cars to blockbuster movies, the list of new opportunities is growing every single day. 105 | Explore all of our open roles, including internships and new college graduate positions. 106 | Come meet the NVIDIA team at the SC23 job fair at table JF15. 107 | --------------------------------------------------- 108 | -------------------------------------------------------------------------------- /00.testSLM_stream.py: -------------------------------------------------------------------------------- 1 | # Chat with an intelligent assistant in your terminal model/stablelm-2-zephyr-1_6b-Q8_0.gguf 2 | from openai import OpenAI 3 | import sys 4 | from time import sleep 5 | from llama_cpp import Llama 6 | import datetime 7 | 8 | def writehistory(filename,text): 9 | with open(filename, 'a', encoding='utf-8') as f: 10 | f.write(text) 11 | f.write('\n') 12 | f.close() 13 | 14 | timestamp = datetime.datetime.now() 15 | log = f""" 16 | 17 | 18 | 19 | 20 | ######################################################################### 21 | {str(timestamp)} 22 | 23 | TEST THE SAME PROMPT ON MULTIPLE GGUF 24 | SMALL LANGUAGE MODELS 25 | 26 | --- 27 | Outline the content for an Article about the main 3 problems the AI research is facing. The title is 'Tehe AI 3-body problem. On Benchmarks, Computation and Data: if AGI progress has stalled, what is the future ahead?' 28 | The aritcle must cover the following topics: 29 | 1) Benchmarks are like a snake that bites its own tail - AI is becoming the judge for other new LLM performance review. 30 | 2) skills are not intelligence 31 | 3) computation resources required to run large Language Models are expensive, and training requires long time. Don't we have new technologies to allow normal consumer hardware available for the task? 32 | 4) Data: every AI is trained on available data, and we have saturated the available one. How we can ensure truthfulness and quality? garbage is is garbage out 33 | 34 | Format your output as a list. 35 | 36 | Outline: 37 | 38 | 39 | Write the content for an Article about the main 3 problems the AI research is facing. 40 | The title is 'The AI 3-body problem. On Benchmarks, Computation and Data: if AGI progress has stalled, what is the future ahead?' 41 | The article must be 2000 words and must include the following topics: 42 | - Benchmarks are like a snake that bites its own tail and AI is becoming the judge for other new LLM performance review. 43 | - skills are not intelligence 44 | - computation resources required to run large Language Models are expensive, and training requires long time. Don't we have new technologies to allow normal consumer hardware available for the task? 45 | - Data: every AI is trained on available data, and we have saturated the available one. How we can ensure truthfulness and quality? garbage is is garbage out 46 | 47 | 48 | Article: 49 | 50 | --- 51 | 52 | ######################################################################### 53 | 54 | """ 55 | writehistory('ModelsTest_history.txt',log) 56 | 57 | 58 | 59 | history = [ 60 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 61 | {"role": "user", "content": "Hi there!"}, 62 | {"role": "assistant", "content": "Hi, How can I help you today?"} 63 | ] 64 | print("\033[92;1m") 65 | 66 | 67 | userinput = """Write the content for an Article about the main 3 problems the AI research is facing. 68 | The title is 'The AI 3-body problem. On Benchmarks, Computation and Data: if AGI progress has stalled, what is the future ahead?' 69 | The article must be 2000 words and must include the following topics: 70 | - Benchmarks are like a snake that bites its own tail and AI is becoming the judge for other new LLM performance review. 71 | - skills are not intelligence 72 | - computation resources required to run large Language Models are expensive, and training requires long time. Don't we have new technologies to allow normal consumer hardware available for the task? 73 | - Data: every AI is trained on available data, and we have saturated the available one. How we can ensure truthfulness and quality? garbage is is garbage out 74 | 75 | 76 | Article:""" 77 | 78 | # Start the test with 5 different models 79 | ########################################### 80 | 81 | 82 | 83 | ################## dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf ########################### 84 | print("\033[95;3;6m") 85 | print("1. Loading dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf...") 86 | llm = Llama( 87 | model_path='models/dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf', 88 | n_gpu_layers=0, 89 | temperature=0.1, 90 | top_p = 0.5, 91 | n_ctx=8192, 92 | max_tokens=600, 93 | repeat_penalty=1.7, 94 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 95 | verbose=True, 96 | ) 97 | print("2. Model loaded with LlamaCPP...") 98 | print("\033[0m") #reset all 99 | history = [ 100 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 101 | {"role": "user", "content": "Hi there!"}, 102 | {"role": "assistant", "content": "Hi, How can I help you today?"} 103 | ] 104 | history.append({"role": "user", "content": userinput}) 105 | print("\033[92;1m") 106 | start = datetime.datetime.now() 107 | full_response = "" 108 | fisrtround = 0 109 | print("GENERATED dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf...\n-------------------------------------------\n") 110 | for chunk in llm.create_chat_completion( 111 | messages=history, 112 | temperature=0.35, 113 | repeat_penalty= 1.6, 114 | stop=['<|im_end|>','',""], 115 | max_tokens=2000, 116 | stream=True,): 117 | try: 118 | if chunk["choices"][0]["delta"]["content"]: 119 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 120 | full_response += chunk["choices"][0]["delta"]["content"] 121 | except: 122 | pass 123 | delta = datetime.datetime.now() - start 124 | log = f""" 125 | ======================================================================== 126 | dolphin-2.9.3-qwen2-0.5b.Q8_0.gguf 127 | ------------------------------------------------------------------------ 128 | generated in {str(delta)} 129 | --- 130 | {full_response} 131 | ------------------------------------------------------------------------ 132 | 133 | 134 | """ 135 | writehistory('ModelsTest_history.txt',log) 136 | del llm 137 | 138 | ################## models\h2o-danube2-1.8b-chat-Q8_0.gguf ########################### 139 | print("\033[95;3;6m") 140 | print("1. Loading h2o-danube2-1.8b-chat-Q8_0.gguf...") 141 | llm = Llama( 142 | model_path='models/h2o-danube2-1.8b-chat-Q8_0.gguf', 143 | n_gpu_layers=0, 144 | temperature=0.35, 145 | top_p = 0.5, 146 | n_ctx=8192, 147 | max_tokens=2000, 148 | repeat_penalty=1.7, 149 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 150 | verbose=True, 151 | ) 152 | print("2. Model loaded with LlamaCPP...") 153 | print("\033[0m") #reset all 154 | history = [ 155 | {"role": "user", "content": "Hi there!"}, 156 | {"role": "assistant", "content": "Hi, How can I help you today?"} 157 | ] 158 | history.append({"role": "user", "content": userinput}) 159 | print("\033[92;1m") 160 | start = datetime.datetime.now() 161 | full_response = "" 162 | print("GENERATED h2o-danube2-1.8b-chat-Q8_0.gguf...\n-------------------------------------------\n") 163 | fisrtround = 0 164 | for chunk in llm.create_chat_completion( 165 | messages=history, 166 | temperature=0.35, 167 | repeat_penalty= 1.6, 168 | stop=['<|im_end|>','',""], 169 | max_tokens=2000, 170 | stream=True,): 171 | try: 172 | if chunk["choices"][0]["delta"]["content"]: 173 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 174 | full_response += chunk["choices"][0]["delta"]["content"] 175 | except: 176 | pass 177 | delta = datetime.datetime.now() - start 178 | log = f""" 179 | ======================================================================== 180 | h2o-danube2-1.8b-chat-Q8_0.gguf 181 | ------------------------------------------------------------------------ 182 | generated in {str(delta)} 183 | --- 184 | {full_response} 185 | ------------------------------------------------------------------------ 186 | 187 | 188 | """ 189 | writehistory('ModelsTest_history.txt',log) 190 | del llm 191 | 192 | ################## models\PowerQwen-1.5B-v1.Q4_K_M.gguf ########################### 193 | #skipped for template problems 194 | ################## models\Phi-3-mini-128k-instruct-Q4_K_M.gguf ########################### 195 | print("\033[95;3;6m") 196 | print("1. Loading Phi-3-mini-128k-instruct-Q4_K_M.gguf...") 197 | llm = Llama( 198 | model_path='models/Phi-3-mini-128k-instruct-Q4_K_M.gguf', 199 | n_gpu_layers=0, 200 | temperature=0.35, 201 | top_p = 0.5, 202 | n_ctx=8192, 203 | max_tokens=2000, 204 | repeat_penalty=1.7, 205 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 206 | verbose=True, 207 | ) 208 | print("2. Model loaded with LlamaCPP...") 209 | print("\033[0m") #reset all 210 | history = [ 211 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 212 | {"role": "user", "content": "Hi there!"}, 213 | {"role": "assistant", "content": "Hi, How can I help you today?"} 214 | ] 215 | history.append({"role": "user", "content": userinput}) 216 | print("\033[92;1m") 217 | start = datetime.datetime.now() 218 | full_response = "" 219 | print("GENERATED Phi-3-mini-128k-instruct-Q4_K_M.gguf...\n-------------------------------------------\n") 220 | fisrtround = 0 221 | for chunk in llm.create_chat_completion( 222 | messages=history, 223 | temperature=0.35, 224 | repeat_penalty= 1.6, 225 | stop=['<|im_end|>','',""], 226 | max_tokens=2000, 227 | stream=True,): 228 | try: 229 | if chunk["choices"][0]["delta"]["content"]: 230 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 231 | full_response += chunk["choices"][0]["delta"]["content"] 232 | except: 233 | pass 234 | delta = datetime.datetime.now() - start 235 | log = f""" 236 | ======================================================================== 237 | Phi-3-mini-128k-instruct-Q4_K_M.gguf 238 | ------------------------------------------------------------------------ 239 | generated in {str(delta)} 240 | --- 241 | {full_response} 242 | ------------------------------------------------------------------------ 243 | 244 | 245 | """ 246 | writehistory('ModelsTest_history.txt',log) 247 | del llm 248 | 249 | ################## models\Qwen1.5-0.5B-Chat_llamafy.Q8_0.gguf ########################### 250 | print("\033[95;3;6m") 251 | print("1. Loading Qwen1.5-0.5B-Chat_llamafy.Q8_0.gguf...") 252 | llm = Llama( 253 | model_path='models/Qwen1.5-0.5B-Chat_llamafy.Q8_0.gguf', 254 | n_gpu_layers=0, 255 | temperature=0.35, 256 | top_p = 0.5, 257 | n_ctx=8192, 258 | max_tokens=2000, 259 | repeat_penalty=1.7, 260 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 261 | verbose=True, 262 | ) 263 | print("2. Model loaded with LlamaCPP...") 264 | print("\033[0m") #reset all 265 | history = [ 266 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 267 | {"role": "user", "content": "Hi there!"}, 268 | {"role": "assistant", "content": "Hi, How can I help you today?"} 269 | ] 270 | history.append({"role": "user", "content": userinput}) 271 | print("\033[92;1m") 272 | start = datetime.datetime.now() 273 | full_response = "" 274 | print("GENERATED Qwen1.5-0.5B-Chat_llamafy.Q8_0.gguf...\n-------------------------------------------\n") 275 | fisrtround = 0 276 | for chunk in llm.create_chat_completion( 277 | messages=history, 278 | temperature=0.35, 279 | repeat_penalty= 1.6, 280 | stop=['<|im_end|>','',""], 281 | max_tokens=2000, 282 | stream=True,): 283 | try: 284 | if chunk["choices"][0]["delta"]["content"]: 285 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 286 | full_response += chunk["choices"][0]["delta"]["content"] 287 | except: 288 | pass 289 | delta = datetime.datetime.now() - start 290 | log = f""" 291 | ======================================================================== 292 | Qwen1.5-0.5B-Chat_llamafy.Q8_0.gguf 293 | ------------------------------------------------------------------------ 294 | generated in {str(delta)} 295 | --- 296 | {full_response} 297 | ------------------------------------------------------------------------ 298 | 299 | 300 | """ 301 | writehistory('ModelsTest_history.txt',log) 302 | del llm 303 | 304 | ################## models\qwen2-deita-500m-q8_0.gguf ########################### 305 | print("\033[95;3;6m") 306 | print("1. Loading qwen2-deita-500m-q8_0.gguf...") 307 | llm = Llama( 308 | model_path='models/qwen2-deita-500m-q8_0.gguf', 309 | n_gpu_layers=0, 310 | temperature=0.35, 311 | top_p = 0.5, 312 | n_ctx=8192, 313 | max_tokens=2000, 314 | repeat_penalty=1.7, 315 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 316 | verbose=True, 317 | ) 318 | print("2. Model loaded with LlamaCPP...") 319 | print("\033[0m") #reset all 320 | history = [ 321 | {"role": "system", "content": "You are an intelligent assistant. You always provide well-reasoned answers that are both correct and helpful."}, 322 | {"role": "user", "content": "Hi there!"}, 323 | {"role": "assistant", "content": "Hi, How can I help you today?"} 324 | ] 325 | history.append({"role": "user", "content": userinput}) 326 | print("\033[92;1m") 327 | start = datetime.datetime.now() 328 | full_response = "" 329 | print("GENERATED qwen2-deita-500m-q8_0.gguf...\n-------------------------------------------\n") 330 | fisrtround = 0 331 | for chunk in llm.create_chat_completion( 332 | messages=history, 333 | temperature=0.35, 334 | repeat_penalty= 1.6, 335 | stop=['<|im_end|>','',""], 336 | max_tokens=2000, 337 | stream=True,): 338 | try: 339 | if chunk["choices"][0]["delta"]["content"]: 340 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 341 | full_response += chunk["choices"][0]["delta"]["content"] 342 | except: 343 | pass 344 | delta = datetime.datetime.now() - start 345 | log = f""" 346 | ======================================================================== 347 | qwen2-deita-500m-q8_0.gguf 348 | ------------------------------------------------------------------------ 349 | generated in {str(delta)} 350 | --- 351 | {full_response} 352 | ------------------------------------------------------------------------ 353 | 354 | 355 | """ 356 | writehistory('ModelsTest_history.txt',log) 357 | del llm 358 | 359 | ################## models\stablelm-2-zephyr-1_6b-Q5_K_M.gguf ########################### 360 | print("\033[95;3;6m") 361 | print("1. Loading stablelm-2-zephyr-1_6b-Q5_K_M.gguf...") 362 | llm = Llama( 363 | model_path='models/stablelm-2-zephyr-1_6b-Q5_K_M.gguf', 364 | n_gpu_layers=0, 365 | temperature=0.35, 366 | top_p = 0.5, 367 | n_ctx=4096, 368 | max_tokens=2000, 369 | repeat_penalty=1.7, 370 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 371 | verbose=True, 372 | ) 373 | print("2. Model loaded with LlamaCPP...") 374 | print("\033[0m") #reset all 375 | history = [ 376 | {"role": "user", "content": "Hi there!"}, 377 | {"role": "assistant", "content": "Hi, How can I help you today?"} 378 | ] 379 | history.append({"role": "user", "content": userinput}) 380 | print("\033[92;1m") 381 | start = datetime.datetime.now() 382 | full_response = "" 383 | print("GENERATED stablelm-2-zephyr-1_6b-Q5_K_M.gguf...\n-------------------------------------------\n") 384 | fisrtround = 0 385 | for chunk in llm.create_chat_completion( 386 | messages=history, 387 | temperature=0.35, 388 | repeat_penalty= 1.6, 389 | stop=['<|im_end|>','',""], 390 | max_tokens=2000, 391 | stream=True,): 392 | try: 393 | if chunk["choices"][0]["delta"]["content"]: 394 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 395 | full_response += chunk["choices"][0]["delta"]["content"] 396 | except: 397 | pass 398 | delta = datetime.datetime.now() - start 399 | log = f""" 400 | ======================================================================== 401 | stablelm-2-zephyr-1_6b-Q5_K_M.gguf 402 | ------------------------------------------------------------------------ 403 | generated in {str(delta)} 404 | --- 405 | {full_response} 406 | ------------------------------------------------------------------------ 407 | 408 | 409 | """ 410 | writehistory('ModelsTest_history.txt',log) 411 | del llm 412 | 413 | 414 | ################## models\TinyEnsemble-3x1.1B-TinyMoE.Q6_K.gguf ########################### 415 | print("\033[95;3;6m") 416 | print("1. Loading TinyEnsemble-3x1.1B-TinyMoE.Q6_K.gguf...") 417 | llm = Llama( 418 | model_path='models/TinyEnsemble-3x1.1B-TinyMoE.Q6_K.gguf', 419 | n_gpu_layers=0, 420 | temperature=0.35, 421 | top_p = 0.5, 422 | n_ctx=4096, 423 | max_tokens=2000, 424 | repeat_penalty=1.7, 425 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 426 | verbose=True, 427 | ) 428 | print("2. Model loaded with LlamaCPP...") 429 | print("\033[0m") #reset all 430 | history = [ 431 | {"role": "user", "content": "Hi there!"}, 432 | {"role": "assistant", "content": "Hi, How can I help you today?"} 433 | ] 434 | history.append({"role": "user", "content": userinput}) 435 | print("\033[92;1m") 436 | start = datetime.datetime.now() 437 | full_response = "" 438 | print("GENERATED TinyEnsemble-3x1.1B-TinyMoE.Q6_K.gguf...\n-------------------------------------------\n") 439 | fisrtround = 0 440 | for chunk in llm.create_chat_completion( 441 | messages=history, 442 | temperature=0.35, 443 | repeat_penalty= 1.6, 444 | stop=['<|im_end|>','',""], 445 | max_tokens=2000, 446 | stream=True,): 447 | try: 448 | if chunk["choices"][0]["delta"]["content"]: 449 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 450 | full_response += chunk["choices"][0]["delta"]["content"] 451 | except: 452 | pass 453 | delta = datetime.datetime.now() - start 454 | log = f""" 455 | ======================================================================== 456 | TinyEnsemble-3x1.1B-TinyMoE.Q6_K.gguf 457 | ------------------------------------------------------------------------ 458 | generated in {str(delta)} 459 | --- 460 | {full_response} 461 | ------------------------------------------------------------------------ 462 | 463 | 464 | """ 465 | writehistory('ModelsTest_history.txt',log) 466 | del llm 467 | 468 | ################## models\tinyllama-moe4-q8_0.gguf ########################### 469 | print("\033[95;3;6m") 470 | print("1. Loading tinyllama-moe4-q8_0.gguf...") 471 | llm = Llama( 472 | model_path='models/tinyllama-moe4-q8_0.gguf', 473 | n_gpu_layers=0, 474 | temperature=0.35, 475 | top_p = 0.5, 476 | n_ctx=2048, 477 | max_tokens=1800, 478 | repeat_penalty=1.7, 479 | stop=["<|im_end|>","Instruction:","### Instruction:","###",""], 480 | verbose=True, 481 | ) 482 | print("2. Model loaded with LlamaCPP...") 483 | print("\033[0m") #reset all 484 | history = [ 485 | {"role": "user", "content": "Hi there!"}, 486 | {"role": "assistant", "content": "Hi, How can I help you today?"} 487 | ] 488 | history.append({"role": "user", "content": userinput}) 489 | print("\033[92;1m") 490 | start = datetime.datetime.now() 491 | full_response = "" 492 | print("GENERATED tinyllama-moe4-q8_0.gguf...\n-------------------------------------------\n") 493 | fisrtround = 0 494 | for chunk in llm.create_chat_completion( 495 | messages=history, 496 | temperature=0.35, 497 | repeat_penalty= 1.6, 498 | stop=['<|im_end|>','',""], 499 | max_tokens=1800, 500 | stream=True,): 501 | try: 502 | if chunk["choices"][0]["delta"]["content"]: 503 | print(chunk["choices"][0]["delta"]["content"], end="", flush=True) 504 | full_response += chunk["choices"][0]["delta"]["content"] 505 | except: 506 | pass 507 | delta = datetime.datetime.now() - start 508 | log = f""" 509 | ======================================================================== 510 | tinyllama-moe4-q8_0.gguf 511 | ------------------------------------------------------------------------ 512 | generated in {str(delta)} 513 | --- 514 | {full_response} 515 | ------------------------------------------------------------------------ 516 | 517 | 518 | """ 519 | writehistory('ModelsTest_history.txt',log) 520 | del llm -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LLM-Studies 2 | Collection of Resources, repositories and snippets for LLM and Open source Generative AI 3 | 4 | 5 | 6 | ## LLM Frameworks and tools 7 | - ### [Duckduckgo_search](https://pypi.org/project/duckduckgo-search/) 8 | > Search for words, documents, images, videos, news, maps and text translation using the DuckDuckGo.com search engine. Downloading files and images to a local hard drive. 9 | 10 | 11 | ## OpenAI compatible API 12 | 13 | - https://python.langchain.com/v0.2/docs/integrations/chat/openai/ 14 | 15 | ``` 16 | # source https://github.com/fabiomatricardi/llamaCPP_Agents/blob/main/testapi.py 17 | from langchain_openai import ChatOpenAI 18 | 19 | llm = ChatOpenAI( 20 | base_url="http://localhost:8080/v1", api_key="not-needed", 21 | model="qwen1_5-0_5b-chat", 22 | temperature=0.1, 23 | max_tokens=500, 24 | timeout=None, 25 | max_retries=2, 26 | # organization="...", 27 | # other params... 28 | ) 29 | 30 | messages = [ 31 | ( 32 | "system", 33 | "You are a helpful assistant that translates English to French. Translate the user sentence.", 34 | ), 35 | ("human", "I love programming."), 36 | ] 37 | """ 38 | messages = [ 39 | { 40 | "role": "system", 41 | "content":"You are a helpful assistant that translates English to French. Translate the user sentence."}, 42 | {"role":"human", "content":"I love programming."}, 43 | ] 44 | """ 45 | 46 | ai_msg = llm.invoke(messages) 47 | #ai_msg 48 | print(ai_msg.content) 49 | ``` 50 | 51 | - https://python.langchain.com/v0.2/docs/integrations/chat/llamacpp/ 52 | - https://python.langchain.com/v0.1/docs/integrations/text_embedding/llamacpp/ 53 | 54 | 55 | ## LlamaCPP 56 | 57 | 58 | Main project Repo on GitHub https://github.com/ggerganov/llama.cpp/tree/compilade/refactor-kv-cache?tab=readme-ov-file 59 | 60 | #### Python bindings 61 | https://github.com/abetlen/llama-cpp-python?tab=readme-ov-file 62 | > compatible openAI server https://llama-cpp-python.readthedocs.io/en/stable/server/ 63 | 64 | - [LLamaCPPChat](https://python.langchain.com/v0.2/docs/integrations/chat/llamacpp/) 65 | - [LLamaCPPEmbeddings](https://python.langchain.com/v0.1/docs/integrations/text_embedding/llamacpp/) 66 | 67 | 68 | 69 | 70 | ## Agentic llm 71 | - ### [LLMs: A New Era in Web Scraping](https://medium.com/@ireihani/llms-a-new-era-in-web-scraping-1ffc6b93abda) 72 | 73 | 74 | > In the rapidly evolving landscape of machine learning and its real-world applications, a notable project has emerged on GitHub: GPT Researcher. This autonomous system functions as an advanced research assistant, drawing from the capabilities of AutoGPT and Plan-and-Solve to offer a unique approach to online research. One of its most promising applications is in the field of web scraping, a technique for extracting data from websites, where Large Language Models (LLMs) like GPT Researcher offer significant advantages. 75 | 76 | https://docs.gptr.dev/docs/gpt-researcher/pip-package 77 | 78 | 79 |

80 | 81 | - ### [CrewAI 🕷️🤖](https://github.com/unclecode/crawl4ai) 82 | > Crawl4AI has one clear task: to simplify crawling and extract useful information from web pages, making it accessible for large language models (LLMs) and AI applications. 🆓🌐 83 | > 84 | > [Colab Notebook](https://colab.research.google.com/drive/1wz8u30rvbq6Scodye9AGCw8Qg_Z8QGsk#scrollTo=GyRjyQ1UoaJr) 85 | 86 | > [How to Implement a Simple UI for CrewAI applications - A Quick Tutorial of CrewAI + Streamlit](https://levelup.gitconnected.com/how-to-implement-a-simple-ui-for-crewai-applications-91f29c04fc8f)
87 | > In this application, we use the Streamlit framework for constructing the web app by using its chat widgets that mimic a studio with group chat among several LLM-powered agents in an automated way. The chat will be ended once the final answer is generated by the group. Here is the workflow. 88 | 89 | 90 | - ### [Python Scraper Github projects](https://github.com/topics/scraper?l=python) 91 | > Here are 4,249 public repositories matching this topic... 92 | 93 | - ### [AnimeDL](https://github.com/justfoolingaround/animdl) 94 | 95 | 96 | 97 | 98 | ## Miscellaneous 99 | - ### [Python RICH](https://rich.readthedocs.io/en/latest/index.html) 100 | > [Color codes](https://rich.readthedocs.io/en/latest/appendix/colors.html) 101 | 102 | - ### [EasyGUI](https://easygui.readthedocs.io/en/master/index.html) 103 | > EasyGUI is a module for very simple, very easy GUI programming in Python. EasyGUI is different from other GUI generators in that EasyGUI is NOT event-driven. Instead, all GUI interactions are invoked by simple function calls.
104 | > EasyGui provides an easy-to-use interface for simple GUI interaction with a user. It does not require the programmer to know anything about tkinter, frames, widgets, callbacks or lambda.
105 | > EasyGUI runs on Python 2 and 3, and does not have any dependencies. 106 |

107 | 108 | 109 | 110 | - ### Code snippets 111 | - https://dev.to/_estheradebayo/6-awesome-tools-that-turn-code-snippets-to-images-54fo 112 | - https://carbon.now.sh/ 113 | 114 |


115 | 116 | - ### [Meta Chameleon](https://github.com/facebookresearch/chameleon/tree/main) 117 | > Annoucement here https://about.fb.com/news/2024/06/releasing-new-ai-research-models-to-accelerate-innovation-at-scale/
and here https://twitter.com/AIatMeta/status/1803107817345393136
118 | > As we shared in our research paper last month, Meta Chameleon is a family of models that can combine text and images as input and output any combination of text and images with a single unified architecture for both encoding and decoding. While most current late-fusion models use diffusion-based learning, Meta Chameleon uses tokenization for text and images. This enables a more unified approach and makes the model easier to design, maintain, and scale. The possibilities are endless—imagine generating creative captions for images or using a mix of text prompts and images to create an entirely new scene. https://ai.meta.com/blog/meta-fair-research-new-releases/ 120 | 121 | - ### [ParlAI](https://parl.ai/projects/recipes/) 122 | > https://github.com/facebookresearch/ParlAI
123 | >Building open-domain chatbots is a challenging area for machine learning research. While prior work has shown that scaling neural models in the number of parameters and the size of the data they are trained on gives improved results, we show that other ingredients are important for a high-performing chatbot. Good conversation requires a number of skills that an expert conversationalist blends in a seamless way: providing engaging talking points and listening to their partners, both asking and answering questions, and displaying knowledge, empathy and personality appropriately, depending on the situation. We show that large scale models can learn these skills when given appropriate training data and choice of generation strategy. We build variants of these recipes with 90M, 2.7B and 9.4B parameter neural models, and make our models and code publicly available. Human evaluations show our best models are superior to existing approaches in multi-turn dialogue in terms of engagingness and humanness measurements. We then discuss the limitations of this work by analyzing failure cases of our models. 124 | 125 | Text2Text encoder-decoder models: 126 | > https://huggingface.co/facebook/blenderbot_small-90M/tree/main
127 | > https://huggingface.co/facebook/blenderbot-400M-distill/tree/main 128 | ``` 129 | https://parl.ai/projects/ 130 | https://parl.ai/projects/bb3x/ 131 | 132 | ``` 133 | 134 | 135 | ### Mixture of Experts or MoAgents 136 | Quantized models GGUF [mradermacher/TinyEnsemble-3x1.1B-TinyMoE-GGUF](https://huggingface.co/mradermacher/TinyEnsemble-3x1.1B-TinyMoE-GGUF) 137 | 138 | 139 | Original model [TinyEnsemble-3x1.1B-TinyMoE 140 | ](https://huggingface.co/JoPmt/TinyEnsemble-3x1.1B-TinyMoE) 141 | > TinyEnsemble-3x1.1B-TinyMoE is a Mixture of Experts (MoE) made with the following models using LazyMergekit: 142 | ``` 143 | cognitivecomputations/TinyDolphin-2.8-1.1b 144 | 78health/TinyLlama_1.1B-function-calling 145 | DaertML/TinyGauss-1.1B 146 | ``` 147 | Configuration 148 | ``` 149 | base_model: cognitivecomputations/TinyDolphin-2.8-1.1b 150 | gate_mode: cheap_embed 151 | dtype: bfloat16 152 | experts: 153 | - source_model: cognitivecomputations/TinyDolphin-2.8-1.1b 154 | positive_prompts: ["write", "explain", "summarize", "how", "what", "acting"] 155 | - source_model: 78health/TinyLlama_1.1B-function-calling 156 | positive_prompts: ["code", "python", "javascript", "programming", "script", "run", "create"] 157 | - source_model: DaertML/TinyGauss-1.1B 158 | positive_prompts: ["count", "math", "algorithm", "crypto", "logic", "reason"] 159 | ``` 160 | 161 |

162 | 163 | - ### [Jamba-900M-GGUF](https://huggingface.co/Severian/Jamba-900M-GGUF/tree/main) 164 | > original model from https://huggingface.co/pszemraj/jamba-900M-v0.13-KIx2 165 | 166 | --- 167 | 168 | ### TensorOpera-Fox-1-chat 169 | Chat with web-based Document search and TensorOpera Fox-1 LlamaCPP 170 | 171 | 172 | 173 | 174 | #### Description 175 | project to talk with documents retrieved with websearch and enriched with newspaper3k 176 | forced to use llama-cpp-python and not llamafile becuase of the embeddings 177 | 178 | Using langchain for both llamaCPP LlamaCppEmbeddings and ChatLlamaCpp 179 | 180 | 181 | #### MODEL USED: TensorOpera Fox-1 182 | https://blog.tensoropera.ai/tensoropera-unveils-fox-foundation-model-a-pioneering-open-source-slm-leading-the-way-against-tech-giants/ 183 | 184 | We are thrilled to introduce TensorOpera Fox-1, our cutting-edge 1.6B parameter small language model (SLM) designed to advance scalability and ownership in the generative AI landscape. TensorOpera Fox-1 is a top-performing SLM in its class, outperforming SLMs developed by industry giants like Apple, Google, and Alibaba, making it an optimal choice for developers and enterprises looking for scalable and efficient AI deployment. 185 | 186 | #### Create Venv 187 | python311 -m venv venv 188 | ➜ venv\Scripts\activate 189 | (venv) ➜ llamacpp-agents ⚡  3.11.7 190 | 191 | ##### install dependencies 192 | ``` 193 | pip install --upgrade langchain langchain-community faiss-cpu tiktoken duckduckgo-search llama-cpp-python rich newspaper3k easygui lxml_html_clean streamlit 194 | ``` 195 | 196 | [GGUF MODEL 197 | ](https://huggingface.co/QuantFactory/Fox-1-1.6B-GGUF/tree/main) 198 | 199 | ##### RESOURCES: 200 | https://python.langchain.com/v0.2/docs/integrations/chat/llamacpp/ 201 | 202 | https://python.langchain.com/v0.1/docs/integrations/text_embedding/llamacpp/ 203 | 204 | https://python.langchain.com/v0.2/docs/concepts/#documents 205 | 206 | https://python.langchain.com/v0.1/docs/integrations/tools/ddg/ 207 | 208 | https://stackoverflow.com/questions/77782167/modulenotfounderror-no-module-named-langchain-openai 209 | 210 | https://python.langchain.com/v0.2/docs/integrations/chat/openai/ 211 | 212 | --- 213 | 214 |

215 | 216 | ### Embeddings through llama=cpp=python OpenAI server 217 | 218 | Run the serevr with llamafile 219 | ``` 220 | .\llamafile-0.8.9.exe -m .\models\e5-small-v2.Q8_0.gguf --host 0.0.0.0 --port 8001 221 | ``` 222 | 223 | in the python file 224 | ``` 225 | from openai import OpenAI 226 | import sys 227 | from time import sleep 228 | # https://platform.openai.com/docs/api-reference/fine-tuning/create?lang=python 229 | 230 | client = OpenAI(base_url="http://localhost:8001/v1", api_key="not-needed") 231 | 232 | em = client.embeddings.create( 233 | model="", 234 | input="The food was delicious and the waiter...", 235 | encoding_format="float" 236 | ) 237 | print(em.data[0].embedding) 238 | ``` 239 | 240 | Result is alist of floats 241 | ``` 242 | [-0.07770553231239319, 0.058053839951753616, -0.003731339704245329, -0.028344525024294853, -0.0819331556558609, 0.02391095831990242, 0.10274428129196167, -0.10635162144899368, 0.031019171699881554, 0.09220582246780396, 0.03185124695301056, -0.034699227660894394, -0.07392998784780502, 0.016710588708519936, -0.009577663615345955, -0.0875440165400505, 0.04762440547347069, 0.053609199821949005, -0.08525288105010986, 0.0005862514371983707, 0.00033544341567903757, -0.061387479305267334, 0.0010711036156862974, -0.06924514472484589, -0.02091783471405506, 0.04329301789402962, 0.03802476078271866, 0.06768634915351868, -0.0649380087852478, -0.07964487373828888, -0.03740384802222252, -0.0026801161002367735, 0.026413554325699806, -0.03044666163623333, 0.0382772721350193, -0.05680304020643234, -0.021981796249747276, 0.011962092481553555, 0.0104746725410223, 0.02841184288263321, -0.003782633924856782, -0.0256956834346056, 0.04235060513019562, -0.05248246341943741, -0.01174509059637785, -0.0009710678714327514, -0.024380426853895187, -0.013623673468828201, 0.07945588231086731, -0.001772428979165852, -0.04576412960886955, 0.009208419360220432, 0.09473102539777756, 0.05805292725563049, 0.06969302147626877, 0.06968113780021667, 0.015838181599974632, 0.05809232220053673, 0.05437614768743515, 0.062324777245521545, -0.029256785288453102, 0.04151704162359238, -0.10399787127971649, 0.1338437795639038, 0.07744263112545013, 0.06880676746368408, -0.05262516066431999, -0.02579922042787075, -0.04793877899646759, -0.0339786596596241, -0.009276028722524643, 0.06676141172647476, 0.03787851333618164, 0.017864761874079704, -0.004989943467080593, -0.01868177391588688, 0.04213051125407219, 0.0008882205584086478, -0.05589257553219795, 0.0666891559958458, -0.015595770440995693, -0.044562436640262604, -0.025130413472652435, 0.026039307937026024, -0.009161598049104214, -0.012770503759384155, 0.006095195189118385, -0.03540264815092087, 0.017860310152173042, -0.08059518039226532, 0.008960643783211708, 0.029980773106217384, -0.039573367685079575, 0.002225709380581975, -0.01908496394753456, -0.009972530417144299, -0.00017214946274179965, 0.04399416595697403, 0.030601197853684425, 0.05835232883691788, 0.009089638479053974, 0.007956620305776596, 0.02636985294520855, -0.058108553290367126, -0.04612968862056732, -0.032300662249326706, 0.008961913175880909, 0.032836105674505234, 0.031519949436187744, 0.006439859047532082, -0.006580711342394352, -0.03637237846851349, 0.0407034307718277, -0.012546902522444725, 0.009489111602306366, 0.07189472019672394, -0.016870012506842613, -0.060878802090883255, -0.03973037749528885, -0.02779730036854744, 0.08207202702760696, 0.06839297711849213, 0.012984618544578552, -0.04365922883152962, 0.009428095072507858, -0.059492483735084534, 0.1609777808189392, 0.07275012880563736, 0.014698316343128681, 0.021691877394914627, 0.10013225674629211, -0.05845637246966362, 0.040063485503196716, 0.02857067994773388, -0.06482607871294022, -0.04147634282708168, -0.014708303846418858, -0.006689564324915409, 0.06317062675952911, -0.03172183781862259, -0.026554251089692116, -0.07527494430541992, -0.04827079549431801, -0.14448440074920654, 0.03480589762330055, 0.06835004687309265, -0.1006544977426529, 0.061315104365348816, -0.06104129180312157, -0.01643282175064087, -0.048570163547992706, 0.0025915377773344517, 0.036091893911361694, 0.01804303005337715, -0.0477677658200264, -0.002228283090516925, 0.0458851121366024, 0.06514230370521545, -0.04923420399427414, -0.005478969309478998, -0.06881637126207352, -0.05794316157698631, 0.038011156022548676, 0.06521260738372803, 0.02207203581929207, -0.15013840794563293, 0.030438991263508797, 0.011809349060058594, 0.03515569493174553, -0.0022691702470183372, -0.006921172607690096, 0.05982997268438339, -0.035342369228601456, -0.014525988139212132, 0.07275637239217758, -0.004910834599286318, -0.02476188726723194, -0.03441869840025902, 0.07549623399972916, -0.08513734489679337, 0.07338512688875198, -0.06051208823919296, -0.03319067135453224, 0.024507878348231316, 0.11449761688709259, -0.018317505717277527, -0.05486154928803444, -0.07535598427057266, 0.004853559657931328, 0.013743198476731777, -0.007590802386403084, -0.027932599186897278, -0.02894902229309082, 0.033789683133363724, 0.03222807124257088, 0.008405196480453014, -0.019403137266635895, -0.05829869210720062, -0.012040001340210438, -0.02670944668352604, 0.07818924635648727, 0.055040959268808365, -0.03695793077349663, 0.01658860594034195, -0.029360344633460045, 0.031506672501564026, 0.045277077704668045, -0.03409908711910248, 0.00015142203483264893, 0.0071851639077067375, -0.05450136959552765, 0.010884792543947697, -0.01961529068648815, 0.06041382998228073, -0.033997081220149994, -0.06249495968222618, 0.045573458075523376, 0.0357527919113636, -0.018181046470999718, -0.004839934408664703, 0.09920988976955414, -0.03178643807768822, -0.06652602553367615, -0.11141175776720047, -0.022750934585928917, -0.05174560099840164, -0.06057467311620712, 0.051672112196683884, -0.010788897052407265, -0.011295848526060581, -0.025929275900125504, 0.0012224774109199643, 0.05387137085199356, 0.08782609552145004, -0.033967480063438416, -0.03750520199537277, 0.05379185453057289, 0.02172645926475525, 0.08999872952699661, -0.02423694171011448, -0.04397745057940483, 0.042339228093624115, -0.026889190077781677, 0.00657908758148551, -0.033668700605630875, -0.015253780409693718, -0.020794589072465897, 0.07226668298244476, -0.0406898558139801, 0.14628620445728302, 0.0697421282529831, 0.017225949093699455, -0.08902592957019806, 0.005949118174612522, 0.022570600733160973, -0.002952668583020568, -0.06955864280462265, 0.06391876190900803, -0.02092842198908329, 0.07616294175386429, 0.01134631410241127, -0.03042207658290863, -0.04781018942594528, 0.10487748682498932, -0.003711394965648651, -0.0013989208964630961, -0.056350331753492355, 0.09574436396360397, -0.03303154930472374, -0.029056133702397346, 0.059770822525024414, -0.06106216087937355, -0.03231777250766754, 0.01696535013616085, -0.014432821422815323, 0.04799453169107437, 0.009242958389222622, -0.06274783611297607, -0.04926114156842232, -0.013459327630698681, 0.05877558887004852, -0.033368296921253204, 0.02223854884505272, -0.09123201668262482, -0.01811806671321392, -0.0023599702399224043, -0.030565183609724045, 0.05312572792172432, 0.12135404348373413, -0.010465753264725208, 0.041027311235666275, -0.04170806333422661, -0.014095721766352654, -0.015314429998397827, 0.07176456600427628, -0.054333023726940155, 0.05051775649189949, 0.08677463233470917, 0.031896572560071945, 0.061361998319625854, -0.027793951332569122, -0.030888760462403297, -0.0571727491915226, -0.03652868792414665, -0.09613995999097824, 0.030825935304164886, 0.03935391455888748, -0.008442746475338936, -0.030581273138523102, -0.03340337052941322, -0.0017135083908215165, -0.01253775879740715, -0.06112384796142578, 0.05582120269536972, -0.02848602831363678, 0.030927874147892, 0.023496519774198532, -0.04479381814599037, -0.05359239503741264, -0.1468297392129898, -0.036381203681230545, 0.02778536267578602, -0.027136387303471565, 0.006466824561357498, 0.02537067048251629, -0.015966203063726425, 0.0017414146568626165, -0.06470031291246414, -0.0529756136238575, 0.07985420525074005, -0.014133680611848831, 0.12568916380405426, -0.0026620395947247744, -0.01925741322338581, 0.050303865224123, 0.03245341032743454, -0.036765068769454956, -0.04955979436635971, -0.09206835925579071, -0.01133102085441351, 0.012120837345719337, 0.14277973771095276, 0.03349079191684723, -0.018042463809251785, 0.032874759286642075, 0.04122765734791756, 0.0003585507511161268, 0.014019212685525417, 0.05173221603035927, -0.0194379985332489, -0.07092462480068207, 0.050287216901779175, -0.07988137751817703, 0.017810357734560966, 0.082575723528862, -0.0011121630668640137, -0.01180215459316969, 0.02603086642920971, -0.0029952051118016243, -0.0025801064912229776, 0.07202207297086716, -0.03426627814769745, -0.07458853721618652, 0.08489809930324554, 0.008628335781395435, -0.053580913692712784, -0.005391030106693506, 0.03212390094995499, -0.027894308790564537, 0.003957782406359911, -0.05617683008313179, 0.025063317269086838, 0.04968349635601044, 0.04089535400271416, -0.09153591096401215, -0.10259200632572174, 0.027067070826888084, -0.007999423891305923, -0.04183247685432434, 0.01480425987392664, -0.041702110320329666, 0.03351358324289322, -0.048024557530879974, -0.006607729010283947] 243 | ``` 244 | 245 | ### Trying to run embeddings over llamafile 246 | llama-cpp-compatible API server 247 | - [ ] ISSUE: I cannot get any similarity search 248 | - to be investicgated 249 | ``` 250 | # pip install -U langchain-community faiss-cpu langchain-openai tiktoken 251 | 252 | from openai import OpenAI 253 | import sys 254 | from time import sleep 255 | from rich.markdown import Markdown 256 | import warnings 257 | warnings.filterwarnings(action='ignore') 258 | import datetime 259 | from rich.console import Console 260 | console = Console(width=60) 261 | 262 | import tiktoken 263 | from time import sleep 264 | encoding = tiktoken.get_encoding("r50k_base") #context_count = len(encoding.encode(yourtext)) 265 | 266 | ############# THIS WORKS OUTSIDE LANGCHAIN ###################################### 267 | # https://platform.openai.com/docs/api-reference/fine-tuning/create?lang=python 268 | """ 269 | client = OpenAI(base_url="http://localhost:8001/v1", api_key="not-needed") 270 | 271 | em = client.embeddings.create( 272 | model="", 273 | input="The food was delicious and the waiter...", 274 | encoding_format="float" 275 | ) 276 | print(em.data[0].embedding) 277 | 278 | """ 279 | 280 | ############### LANGCHAIN COONECTOR ############################################ 281 | # https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html 282 | # WITH FAISS https://python.langchain.com/v0.2/docs/integrations/vectorstores/faiss/ 283 | # WITH CHROMA https://python.langchain.com/v0.2/docs/integrations/vectorstores/chroma/ 284 | ############################################################################### 285 | 286 | from langchain_openai import OpenAIEmbeddings 287 | 288 | embeddings = OpenAIEmbeddings(openai_api_base="http://localhost:8001/v1", 289 | openai_api_key="not-needed") 290 | 291 | from langchain_community.document_loaders import TextLoader 292 | from langchain_community.vectorstores import FAISS 293 | #from langchain_openai import OpenAIEmbeddings 294 | from langchain_text_splitters import CharacterTextSplitter 295 | from langchain.text_splitter import TokenTextSplitter 296 | console.print('Splitting documents') 297 | loader = TextLoader("china.txt", encoding='utf-8') 298 | documents = loader.load() 299 | text_splitter = TokenTextSplitter(chunk_size=200, chunk_overlap=50) 300 | texts = text_splitter.split_documents(documents) 301 | #create the vector store 302 | console.print('Creating DB') 303 | db = FAISS.from_documents(texts, embeddings) 304 | console.print(f'Lenght docs = {len(texts)}') 305 | console.print(texts) 306 | console.print(db) 307 | console.print('Db completed') 308 | 309 | query = 'high-tech' 310 | 311 | console.print('[bold red1]Testing one similarity search retriever...') 312 | #default is similarity search 313 | console.print('embedding query') 314 | query_result = embeddings.embed_query(query) 315 | docs = db.similarity_search(query,k=2) # original (query,k=2) 316 | console.print(docs) 317 | console.print('---') 318 | console.print(f'Lenght docs = {len(texts)}') 319 | docs_and_scores = db.similarity_search_with_score(query) 320 | console.print(docs_and_scores) 321 | ``` 322 | 323 | 324 | 325 | ### how to get model name from OpenAI client connection 326 | running a llama-cpp-server OpenAI API compatible from [source](https://huggingface.co/internlm/internlm2_5-7b-chat-gguf#serving) 327 | 328 | ``` 329 | At the client side, you can access the service through OpenAI API: 330 | 331 | from openai import OpenAI 332 | client = OpenAI( 333 | api_key='YOUR_API_KEY', 334 | base_url='http://localhost:8080/v1' 335 | ) 336 | model_name = client.models.list().data[0].id 337 | response = client.chat.completions.create( 338 | model=model_name, 339 | messages=[ 340 | {"role": "system", "content": "You are a helpful assistant."}, 341 | {"role": "user", "content": " provide three suggestions about time management"}, 342 | ], 343 | temperature=0.8, 344 | top_p=0.8 345 | ) 346 | print(response) 347 | ``` 348 | 349 | 350 | 351 | 352 | -------------------------------------------------------------------------------- /JUHIS-log.txt: -------------------------------------------------------------------------------- 1 | ##### 🪄 Query: *china innovation strategy* 2 | ##### 🗃️ Web Documents 3 | 📝 Unleashing "new quality productive forces": China's strategy for ... 4 | While on a visit to Heilongjiang Province in September 2023, Chinese President Xi Jinping called on the nation to mobilize “new quality productive forces” (新质生产力) to drive economic growth. He reiterated this message at the annual Central Economic Work Conference in December. And Premier Li Qiang’s annual work report at the National People’s Congress in March 2024 put as its top policy priority “striving to modernize the industrial system and developing new quality productive forces at a faster pace.” In addition, on January 31, a group of seven ministries led by the Ministry of Industry and Information Technology issued a list of “future industries” (未来产业) which are implied to be the major sectoral targets of the “new quality productive forces.”These slogans culminate a major shift in China’s overall economic policy direction that has been building for two decades, and that has accelerated noticeably in the last five or so years of Xi’s administration. China has now decisively moved away from the growth-maximizing orientation that prevailed from 1979 until the early 2010s, which prioritized systemic reforms to achieve as high a GDP growth rate as possible. The new economic vision prioritizes the acquisition and development of technology—especially “hard” technologies that require a large industrial and manufacturing base. This approach can be seen as a return both to the central tendency of Chinese economic development thinking since the mid-19th century and more specifically, to a Marxist approach to economic policy.Yet in addition to being the culmination of a long trend toward a technology-centric, industrial policy-driven growth strategy, the shift to “new quality productive forces” marks a change in the focus and methods of Xi’s economic governance. Until recently, Chinese industrial policy’s main aims were to achieve mastery in established technologies and self-sufficiency in key areas in order to resist economic containment efforts by the United States. Now, the aim is to make China a leader in the innovation of new technologies. To achieve this goal, national resources are to be mobilized through a “new national system” (新型举国体制), which implies more centralized control over capital allocation.The evolution of technology-driven development policies in ChinaChina’s leadership has long seen technology acquisition as the core task of economic and national development. The origins of this view lie in the “Self-Strengthening Movement” (自强运动), launched in the mid-19th century in response to the trauma of China’s defeats in the Opium Wars of 1839-1842 and 1856-1860. The Self-Strengtheners argued that the reason for those defeats lay in the gap in military technology between China and the European colonial powers and that Chinese policy should focus on investments that would close this technological gap. This effort basically failed, in contrast to the roughly contemporaneous Meiji Restoration, which transformed Japan into a modern industrial and military power between 1870 and 1920.Following the establishment of its government in 1949, the Chinese Communist Party (CCP) has gone through several waves of technology-centered economic development. In the 1950s, with the aid of the Soviet Union, China tried to build up basic industries (notably steel) in a drive that ended with the disastrous Great Leap Forward. In the mid-1970s, economic policymakers launched the “Four Modernizations” (四个现代化) of agriculture, industry, science and technology, and national defense. One specific component of this strategy was a plan to import capital goods and even whole factories in order to accelerate industrial development. This proved impractical due to the lack of hard currency to pay for these imports.In 1979, economic policy shifted to Deng Xiaoping’s “Reform and Opening” (改革开放), which stressed export-led manufacturing, catalyzed by foreign direct investment, along with systemic reforms to reduce the role of the state and increase reliance on market forces. Although both the “Four Modernizations” and “Reform and Opening” slogans continued to be used into the 1980s, the former gradually receded and Reform and Opening became the central guiding principle.The next quarter-century had no shortage of industrial policies and technology-acquisition plans, notably the 863 and 973 programs that respectively financed applied and basic scientific research and the “Golden Projects” that drove the deployment of information technology (IT) networks throughout the country and enabled the construction of the “Great Firewall” to insulate the domestic internet from outside influence.But the distinctive feature of Reform and Opening, relative to other Chinese developmental concepts, was that it put more emphasis on generalized economic progress, and achieving a high rate of GDP growth, than on specific technological goals. Policymakers devoted the most energy to system reforms: increasing the use of market pricing; strengthening rural property rights; creating modern banking and legal systems; lowering tariffs and import barriers; restructuring state-owned enterprises; and integrating with global economic arrangements, particularly the World Trade Organization in 2001.In 2005, however, Chinese economic policy’s center of gravity began to shift back from these general system reforms to technological development. The starting point was the December 2005 publication of the Medium- and Long-Term Plan for Science and Technology Development (国家中长期科学和技术发展规划纲要), which set out ambitious but vague goals for technological development across a range of sectors. It also introduced the concept of “indigenous innovation” (自主创新), whose precise meaning was the subject of heated debate: Chinese analysts generally portrayed it as a call to increase China’s domestic economic activity, while many Western analysts viewed it as a cover for coercive technology-transfer policies such as domestic standards.A series of technology- and industrial-policy initiatives followed over the next decade, whose details and cumulative impact have been lucidly laid out by the China economist Barry Naughton. These included:16 “major projects” (重大专项), announced in tandem with the Medium- and Long-Term Plan. These involved direct state investment in projects such as the Beidou (北斗) satellite navigation system, passenger aircraft, hypersonic missiles, and high-end semiconductors and machine tools.The Strategic Emerging Industries (战略性新兴产业, SEI) initiative of 2010, which identified seven industries as priorities for investment and development: new-generation IT, biotechnology, high-end equipment, new materials, new energy, new energy vehicles, and energy conservation and environmental protection. An eighth sector, innovative digital technology was added in 2016 and the refined list now comprises at least 20 subsectors.Made in China 2025 (中国制造 2025), a 2015 industrial-policy blueprint that came with a range of targets not just for production growth but also for the degree of domestic self-sufficiency and domestic and international market share of China-based firms. The targeted industries were largely though not entirely drawn from SEI sectors.The Innovation-Driven Development Strategy (国家创新驱动发展战略纲要) of 2016. This specifies that “By 2050, China shall be established as a world [science and technology] S&T innovation superpower (世界科技创新强国), becoming one of the world’s main centers of science and occupying the high ground in innovation.” It also asserts that “Increases in labor productivity and social productive force (社会生产力) will rely mainly on technological progress and overall innovation.”New productive forces, future industries, and the new national systemFrom this background, it can be seen that Xi’s recent advocacy of unleashing “new quality productive forces” to stimulate “future industries” under the guidance of a “new national system” of coordination and capital allocation represents the latest step in a very long trend.But the new terminology also breaks fresh ground in three areas. First, “new quality productive forces” is a specifically Marxist term, whereas previous industrial policy initiatives had technocratic, ideologically neutral titles. In classical Marxism, “productive forces” refers to the techniques—essentially, labor and capital—that enable things in nature to be converted to objects of human use.At a Politburo study session on January 31, 2024, Xi asserted that technological innovation is “the core element of developing new productive forces” (科技创新 … 是发展新质生产力的核心要素). In other words, technology is in itself a “new productive force” in addition to the classical ones of labor and capital, and technological innovation can in turn spawn additional “new productive forces.” While we should not overstate the ideological content of Xi’s policies—his economic strategy significantly incorporates market forces as well as state direction—the more open embrace of Marxist terminology serves an important signaling function. It draws a clear distinction between Xi’s economic program and the more explicitly market-oriented consensus of the Reform and Opening period, and it underscores the centrality of the CCP (rather than the government or private sector) in setting the nation’s overall course.Second, the goal of development using “new quality productive forces” is to generate disruptive breakthroughs, not simply produce incremental improvements on existing technologies. In a January 7 speech at Peking University, former Chongqing mayor Huang Qifan, who is now an influential economic commentator, argued that “‘New quality productive forces’ are not simply ordinary technological progress and marginal improvement; instead they involve disruptive technological innovation that promotes the development of ‘new manufacturing.’” (“新质生产力”并非普通的科技进步和边际上的改进, 而是要有颠覆性的科技创新 … 推动“新制造”的发展).The implication is that while previous industrial policy efforts mainly focused on achieving competence, self-sufficiency, or global market share in established technologies, the new generation of industrial policies must focus on “future industries” that are nascent or do not yet exist. The need to move the policy targets from the old SEI list to the new “future industries” list is argued in a report from the Chinese Academy of Science.The official list of “future industries” published by the Ministry of Industry and Information Technology in January spans several broad fields (manufacturing, information, materials, energy, space, and health) and includes items such as humanoid robots, nano-manufacturing, quantum computing, nuclear fusion, hydrogen energy, exploration of the moon and Mars, deep-sea mining, and genetic engineering.Third, the realization of these vast ambitions requires a “new national system” (新型举国体制) of centralized coordination and financial support. This term has been floating around party documents for many years but became a central part of Xi’s discourse beginning with a speech to the Chinese Academy of Sciences in May 2021. It hearkens back to a centralized “national system” effort to spur technological development following the 1960 Sino-Soviet split and the withdrawal of Soviet technical advisers. The basic concept is that, in a world where China can no longer rely on trade with and investment from other countries (especially the United States) to generate the technology it needs, all available domestic resources must be marshaled to support domestic scientific progress. As suggested by both the SEI and the future industries lists, the range of target sectors is staggeringly broad.The main mechanisms of the “new national system” are a new Communist Party coordinating body, the Central Science and Technology Commission (CSTC); increased funding for basic scientific research under a competitive grant model; and improved coordination between the public and private sectors via a revamped system of national laboratories and innovation centers, which are increasingly required to collaborate with both private and state-owned companies in what Barry Naughton calls a “consortium” system.The CSTC is the 10th party commission set up by Xi, and while its membership and specific activities have not been disclosed, it is likely to follow the pattern set by others, such as the Central Cyberspace Affairs Commission (中央网络安全和信息化委员会) and the Central Financial Commission (中央金融委员会). The commissions set broad policy direction and ensure policy compliance while leaving execution to their subordinate government agencies—in the CSTC’s case, the Ministry of Science and Technology. The enhanced public-private cooperation mechanisms are as yet largely aspirational: most of them are organized at the subnational level and their success will depend on the ability of local leaders to reconcile a host of competing commercial and political interests.There is also likely to be some reform of funding mechanisms. Beginning with the establishment of the National Integrated Circuit Fund in 2014, Beijing promoted a venture capital-like model under which “government guidance funds” raised capital from a variety of public and private sources and invested them in startup technology firms—a model the Economist magazine dubbed the “incubator state.” Predictably, this led to widespread corruption and, arguably, a failure to meet the state’s technology goals. The “new national system” implies that funding will be subject to tighter oversight, but it is as yet unclear how this will work in practice, given that many of the guidance funds remain in operation and a new $40 billion semiconductor fund has recently been launched.Prospects for successIt is clear that the combination of “new quality productive forces,” “future industries,” and the “new national system” represents another step toward a more centralized, top-down form of economic management, which is more focused on developing specific technologies deemed important by the state than maximizing GDP growth. The new terminology also signals a more unapologetic embrace of Marxist concepts, largely for the purpose of maintaining strategic focus and discipline. But it is important also to note the ways in which it does not represent a simple return to central planning.For one thing, all of the documents cited in this analysis explicitly recognize that companies are the main drivers of innovation and that market forces have a large role to play; Xi’s Politburo study session remarks, for instance, cite the need to “establish a high-standard market system (建立高标准市场体系).Xi is also explicit in stating that a criterion for the success of the new productive forces is that they produce “a substantial increase in total factor productivity” (以全要素生产率大幅提升为核心标志). This is an important nod to conventional, non-Marxist economics: Most analyses of China’s recent economic performance from this perspective note that the annual rate of overall (total factor) productivity growth in China has fallen dramatically in recent years, from over 3% in the decades before the global financial crisis in 2008, to around 1% in the subsequent decade, and probably close to zero in the past several years. Pessimistic forecasts of China’s future economic trajectory rest largely on the claim that without strong productivity, it will be impossible for China to sustain fast GDP growth. Xi’s intent is not to ignore the need for economic growth altogether. Rather, he is asserting a theory that rapid economic growth can in fact be achieved by a single-minded pursuit of technological progress: new technologies will create the productivity gains necessary for fast economic growth.It is too early to say whether this technology-driven development strategy will succeed in generating sustained fast GDP growth. There are certainly good reasons for skepticism, since the large majority of economic activity and employment lies outside the high-tech sectors—notably, in consumer-oriented services. It is far from obvious that success in high-tech hardware will lead to spillovers that boost productivity in the rest of the economy.But there are good reasons to think that this strategy will produce many technological success stories. For one thing, China’s much-discussed dominance of renewable-energy technologies such as solar, wind, batteries, and electric vehicles is the result of a similar, but much less systematic, industrial policy effort. This success is not isolated. China now accounts for about 20% of the world’s manufactured exports and about 30% of global manufacturing production value; these shares so far have proved resistant to trade “decoupling” efforts led by the United States.This strong and diverse manufacturing ecosystem, combined with a large and technically skilled workforce, means that more systematic government-led efforts in coordination and basic research are likely to prove fruitful. But China will remain heavily intertwined with the rest of the world economy, both because it will depend on external markets to absorb its inevitable production surpluses, and because even an economy as large as China’s has no hope of completely localizing the intricate supply chains for high-tech goods. 5 | -- 6 | 'source': https://www.brookings.edu/articles/unleashing-new-quality-productive-forces-chinas-strategy-for-technology-led-growth/ 7 | 'title': Unleashing "new quality productive forces": China's strategy for ... 8 | 'snippet': This specifies that "By 2050, China shall be established as a world [science and technology] S&T innovation superpower (世界科技创新强国), becoming one of the world's main centers of ... 9 | 'author':['Joel Wuthnow', 'Patricia M. Kim', 'Kevin Dong', 'Mallie Prytherch', 'Diana Fu', 'Emile Dirks', 'Mark Maccarthy', 'Caitlin Welsh', 'Jude Blanchette', 'Lily Mcelwee'] 10 | 'keywords':['strategy', 'unleashing', 'productive', 'development', 'system', 'quality', 'growth', 'national', 'technological', 'forces', 'technology', 'china', 'economic', 'industries', 'technologyled', 'chinas'] 11 | 'meta_description':The shift to “new quality productive forces” marks a change in the focus and methods of Xi’s economic governance. 12 | 'meta_img':https://www.brookings.edu/wp-content/uploads/2024/06/2024-03-05T034920Z_1660337402_RC2DF6ALCZYV_RTRMADP_3_CHINA-PARLIAMENT.jpg?quality=75 13 | 'top_image':https://www.brookings.edu/wp-content/uploads/2024/06/2024-03-05T034920Z_1660337402_RC2DF6ALCZYV_RTRMADP_3_CHINA-PARLIAMENT.jpg?quality=75 14 | 'publish_date':None 15 | 'summary':While on a visit to Heilongjiang Province in September 2023, Chinese President Xi Jinping called on the nation to mobilize “new quality productive forces” (新质生产力) to drive economic growth. 16 | Following the establishment of its government in 1949, the Chinese Communist Party (CCP) has gone through several waves of technology-centered economic development. 17 | First, “new quality productive forces” is a specifically Marxist term, whereas previous industrial policy initiatives had technocratic, ideologically neutral titles. 18 | In classical Marxism, “productive forces” refers to the techniques—essentially, labor and capital—that enable things in nature to be converted to objects of human use. 19 | Second, the goal of development using “new quality productive forces” is to generate disruptive breakthroughs, not simply produce incremental improvements on existing technologies. 20 | ------------------------//--------------------------- 21 | 📝 Innovation-Driven Strategy Boost the Dynamic Transformation of China's ... 22 | The 18th National Congress of the Communist Party of China clearly proposed the “implementation of an innovation-driven development strategy,” emphasizing that “scientific and technological innovation is the strategic support to improve social productivity and comprehensive national strength, must be placed in the core position of the overall development of the country.” 23 | -- 24 | 'source': https://link.springer.com/chapter/10.1007/978-981-97-0956-4_6 25 | 'title': Innovation-Driven Strategy Boost the Dynamic Transformation of China's ... 26 | 'snippet': The 18th National Congress of the Communist Party of China clearly proposed the "implementation of an innovation-driven development strategy," emphasizing that "scientific and technological innovation is the strategic support to improve social productivity and comprehensive national strength, must be placed in the core position of the overall development of the country." 27 | 'author':['Xiao', 'Zhejiang University', 'Pan', 'Zhejiang Institute Of Administration', 'Chen', 'Hangzhou Normal University', 'Hangzhou', 'Wen Xiao', 'Jiadong Pan', 'Hao Chen'] 28 | 'keywords':['strategic', 'strategy', 'boost', 'proposed', 'scientific', 'development', 'strength', 'innovationdriven', 'social', 'national', 'technological', 'transformation', 'economic', 'dynamic', 'chinas', 'support'] 29 | 'meta_description':The 18th National Congress of the Communist Party of China clearly proposed the “implementation of an innovation-driven development strategy,” emphasizing that “scientific and technological innovation is the strategic support to improve social... 30 | 'meta_img':https://static-content.springer.com/cover/book/978-981-97-0956-4.jpg 31 | 'top_image':https://static-content.springer.com/cover/book/978-981-97-0956-4.jpg 32 | 'publish_date':2024-06-28 00:00:00 33 | 'summary':The 18th National Congress of the Communist Party of China clearly proposed the “implementation of an innovation-driven development strategy,” emphasizing that “scientific and technological innovation is the strategic support to improve social productivity and comprehensive national strength, must be placed in the core position of the overall development of the country.” 34 | ------------------------//--------------------------- 35 | 📝 How China Became the World's Clean Tech Giant 36 | China’s clean energy sector was the biggest driver of its GDP growth in 2023, contributing 40 percent (around $1.6 trillion) of its economic expansion. The country’s commitment to renewable energy is underscored by its substantial investments in the industry.Take the solar sector as an example. Chinese investments in new photovoltaic (PV) supply capacity over the last 10 years exceeded $50 billion – ten times more than all of Europe. This investment surge has strengthened China’s energy independence and promoted substantial job creation, with over 300,000 manufacturing jobs across the solar PV value chain added since 2011. China now commands over 80 percent share in all manufacturing stages of solar panels, from polysilicon to modules, solidifying its global leadership in solar energy.Moreover, China’s wind power sector continues to expand, evidenced by the addition of 37 gigawatts (GW) in wind capacity in 2022, including significant growth in offshore farms.Notably, China’s dominance also extends to the electric vehicle (EV) market. China accounted for nearly 60 percent of global new electric car registrations in 2022. China’s electric cars comprise 29 percent of total domestic car sales, surpassing the 2025 national target well ahead of schedule. The country’s ambitious renewable energy targets outlined in the 14th Five-Year Plan further signal a sustained drive toward cleaner energy solutions.This piece delves into China’s dominance in three domains: wind power, solar PV technology, and electric and hybrid electric vehicles (EVs and HEVs). It explains how China’s blend of policy initiatives, targeted investments, technological collaborations, and robust in-house research and development (R&D) efforts has propelled the country to the forefront of global renewable energy production and sustainable transportation innovation.Wind PowerChina’s wind power sector has seen remarkable growth since its inception in the mid-1980s when it began importing turbines from Europe. Over time, China has emerged as the world’s largest wind power market, with the country’s manufacturers supplying nearly 60 percent of installed capacity worldwide in 2022, achieved in part due to its more than 40 domestic wind turbine manufacturers.Particularly notable is the rapid rise of Chinese companies such as Sinovel, Goldwind, and Dongfang, which transitioned from not being in the global top 10 in 2006 to becoming key players by 2009. This transformation was facilitated by strategic partnerships with foreign technology firms, initially through licensing agreements that evolved into co-design relationships as Chinese companies developed their in-house design capabilities.This unconventional strategy succeeded because foreign technology partners were not manufacturing competitors but specialized technology design houses achieving new business ventures through co-design relationships. By leveraging unconventional transfer strategies and engaging in co-design processes with foreign partners, Chinese firms were able to produce turbines comparable in size and sophistication to global competitors’ offerings. Additionally, strategic acquisitions such as Goldwind’s purchase of German Vensys contributed to building strong overseas R&D capabilities.Furthermore, government support in the form of R&D grants has enabled Chinese manufacturers to invest significantly in in-house research and development, leading to innovations such as Sinovel’s 5 MW offshore turbine and the establishment of specialized R&D centers like Sinovel’s National Offshore Wind Power Technology and Equipment R&D Center. While local technology agreements with local centers of excellence have not played a significant role for national champions, other Chinese companies have benefitted from such partnerships, contributing to the overall advancement and competitiveness of China’s wind power sector on the global stage.As a result, according to the Global Wind Report 2023 China accounted for 60 percent of global wind turbine manufacturing capacity in 2023, with Europe in a distant second at 19 percent and the U.S. trailing even farther behind at 9 percent.Solar PV SectorChina’s solar photovoltaic sector has likewise undergone a significant evolution, transitioning from component supply to production of complete panels and becoming the world’s largest producer of solar PV cells.This transformation has been driven by a strategic focus on export-oriented technological upgrading, with Chinese companies capturing a more than 80 percent share of the global PV export market in 2023. While initially reliant on foreign markets, the domestic market has also grown due to substantial subsidies, accounting for 50–70 percent of total solar PV investments.By 2009, three Chinese national champions, including Suntech Power, had secured positions in the global top 10 for PV sales through a combination of local and international technology integration. Suntech Power, for instance, leveraged a mix of local and international technology transfers. The company developed world-class technological expertise by combining in-house R&D with various mechanisms – licensing, a joint venture, overseas FDI and acquisitions, collaboration with the University of New South Wales, and partnerships with research institutions such as Sun Yat-sen University and Shanghai University of Technology.Similarly, Trina Solar and Yingli Solar have adopted comprehensive R&D strategies supported by government initiatives like establishing state key laboratories and collaborating with key suppliers, local universities, and renowned international research institutions like MIT in the United States and Australia National University. These companies have expanded their global footprint through acquisitions and overseas R&D centers, enhancing their technological capability and competitiveness in the global solar PV market.Electric and Hybrid Electric Vehicles SectorChina has implemented favorable policies to position itself as a leading plug-in hybrid and electric passenger vehicle producer. From 2009-2022, China’s government devoted more than 200 billion yuan ($28 billion) to subsidies and tax breaks for the EV industry. China already holds a prominent position as the top producer of rechargeable batteries, a crucial technology within the electric vehicle supply chain.China’s electric and hybrid electric vehicle sector has seen notable instances of conventional technology transfers, particularly evident in the establishment of joint ventures. These ventures were primarily driven by a market pull due to stringent emissions standards, surpassing those of the United States, and government regulations mandating Chinese majority share joint ventures for foreign auto companies. SAIC Motor, China’s largest auto manufacturer, has forged joint ventures with a U.S. lithium-ion battery company and Volkswagen, along with setting up collaborative facilities for developing hybrid and all-electric vehicle technologies with General Motors. SAIC has also expanded its international presence through overseas R&D efforts, such as acquiring a U.K.-based company. Similarly, other companies like Chery Auto have focused on in-house R&D efforts to develop electric and hybrid vehicles, especially in the small-car segment.Build Your Dreams (BYD), a Chinese electric vehicle manufacturer, has taken a distinct approach by acquiring a local car manufacturer and heavily investing in in-house R&D to integrate its lithium-ion battery technology with car manufacturing. BYD’s strategy of localized innovation led to the introduction of the first plug-in hybrid electric vehicle in 2008. Building upon its in-house innovation platform, BYD has now established a joint venture for R&D, combining Daimler’s car platform with BYD’s battery and electric motor technology to co-design electric vehicles under a joint brand.This blend of policy support, strategic partnerships, and in-house innovation has positioned China as a major player in the global EV and HEV market. The same model made China into the global champion of wind and solar supply chains. 37 | -- 38 | 'source': https://thediplomat.com/2024/04/how-china-became-the-worlds-clean-tech-giant/ 39 | 'title': How China Became the World's Clean Tech Giant 40 | 'snippet': China's clean energy sector was the biggest driver of its GDP growth in 2023, contributing 40 percent (around $1.6 trillion) of its economic expansion. The country's commitment to renewable ... 41 | 'author':[] 42 | 'keywords':['pv', 'worlds', 'power', 'electric', 'clean', 'rd', 'tech', 'wind', 'technology', 'solar', 'global', 'china', 'chinese', 'giant', 'chinas', 'Environment', 'East Asia', 'China', 'China clean energy', 'China EV industry', 'China solar energy', 'China Solar Industry', 'China wind energy'] 43 | 'meta_description':Analyzing China’s dominance in the wind, solar, and electric vehicle sectors. 44 | 'meta_img':https://thediplomat.com/wp-content/uploads/2022/11/sizes/td-story-s-2/thediplomat_2022-11-29-160235.jpg 45 | 'top_image':https://thediplomat.com/wp-content/uploads/2022/11/sizes/td-story-s-2/thediplomat_2022-11-29-160235.jpg 46 | 'publish_date':None 47 | 'summary':Moreover, China’s wind power sector continues to expand, evidenced by the addition of 37 gigawatts (GW) in wind capacity in 2022, including significant growth in offshore farms. 48 | China’s electric cars comprise 29 percent of total domestic car sales, surpassing the 2025 national target well ahead of schedule. 49 | Wind PowerChina’s wind power sector has seen remarkable growth since its inception in the mid-1980s when it began importing turbines from Europe. 50 | As a result, according to the Global Wind Report 2023 China accounted for 60 percent of global wind turbine manufacturing capacity in 2023, with Europe in a distant second at 19 percent and the U.S. trailing even farther behind at 9 percent. 51 | Solar PV SectorChina’s solar photovoltaic sector has likewise undergone a significant evolution, transitioning from component supply to production of complete panels and becoming the world’s largest producer of solar PV cells. 52 | ------------------------//--------------------------- 53 | 54 | --------------------------------------------------------------------------------