bldng commited on
Commit
1793d31
1 Parent(s): 80cd2ab

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +7 -8
  2. chat_test.ipynb +7 -0
  3. interactive_test.py +17 -118
  4. models.py +113 -0
README.md CHANGED
@@ -5,21 +5,20 @@ sdk: gradio
5
  sdk_version: 4.43.0
6
  ---
7
  # HumanGPT Game Test
8
- <a target="_blank" href="https://colab.research.google.com/github/bldng1337/human_gpt_technical_test/blob/main/chat_test.ipynb">
9
- <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
10
- </a>
11
 
12
  This is a Test of the feasibility of letting an LLM generate the user part while the bot part is written by the user.
13
 
14
  We won't instruct the language model to roleplay as a user. Instead, we'll instruct it to generate the bot's responses as it was trained to do. Then, we let the model complete the user text blocks. Since the model doesn't distinguish between writing bot or user parts, we should be able to leverage its full training instead of trying to get it to rp which it was not trained for. Should also make gaslighting/confusing the model harder as its not pretending to be a user but should belive it is.
15
 
16
  ## How to use
17
- ### For the Notebook:
18
- Press the "Open in Colab" button to open the notebook in Google Colab.
19
-
20
- ### For the Gradio App:
21
  Visit: https://bldng-demo-human-gpt.hf.space/
22
-
 
 
 
 
23
  ## TODO
24
  - Make a chatwindow with panel to test the model interactively
25
  - test multiple backs and forths
 
5
  sdk_version: 4.43.0
6
  ---
7
  # HumanGPT Game Test
8
+
 
 
9
 
10
  This is a Test of the feasibility of letting an LLM generate the user part while the bot part is written by the user.
11
 
12
  We won't instruct the language model to roleplay as a user. Instead, we'll instruct it to generate the bot's responses as it was trained to do. Then, we let the model complete the user text blocks. Since the model doesn't distinguish between writing bot or user parts, we should be able to leverage its full training instead of trying to get it to rp which it was not trained for. Should also make gaslighting/confusing the model harder as its not pretending to be a user but should belive it is.
13
 
14
  ## How to use
15
+ ## For the Gradio App:
 
 
 
16
  Visit: https://bldng-demo-human-gpt.hf.space/
17
+ ## For the Notebook:
18
+ Press the "Open in Colab" button to open the notebook in Google Colab.
19
+ <a target="_blank" href="https://colab.research.google.com/github/bldng1337/human_gpt_technical_test/blob/main/chat_test.ipynb">
20
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
21
+ </a>
22
  ## TODO
23
  - Make a chatwindow with panel to test the model interactively
24
  - test multiple backs and forths
chat_test.ipynb CHANGED
@@ -1,5 +1,12 @@
1
  {
2
  "cells": [
 
 
 
 
 
 
 
3
  {
4
  "cell_type": "code",
5
  "execution_count": 1,
 
1
  {
2
  "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "This was the initial test, the Gradio app is more up to date."
8
+ ]
9
+ },
10
  {
11
  "cell_type": "code",
12
  "execution_count": 1,
interactive_test.py CHANGED
@@ -2,7 +2,7 @@ from typing import Any, Dict, List
2
  import gradio as gr
3
  from llama_cpp import Llama
4
 
5
-
6
 
7
  syspropmt=r"""
8
  The User will make an inquiry to the assistant.
@@ -24,119 +24,6 @@ Assistant: The capital of France is Paris
24
  User: <|endtile|>
25
  """.strip()
26
 
27
- def chatmsg(message, role):
28
- return {"role": role, "content": message}
29
-
30
- class Model:
31
- def __init__(self):
32
- pass
33
- def __call__(self, msg:str, stop:List[str], max_tokens:int):
34
- raise NotImplementedError
35
- def conv(self, msgs:List[Dict[str, str]]):
36
- raise NotImplementedError
37
- def starttok(self, user:str):
38
- raise NotImplementedError
39
- def close(self):
40
- pass
41
-
42
- class Phi35RPMax(Model):
43
- def __init__(self):
44
- self.llm = Llama.from_pretrained(
45
- repo_id="ArliAI/Phi-3.5-mini-3.8B-ArliAI-RPMax-v1.1-GGUF",
46
- filename="ArliAI-RPMax-3.8B-v1.1-fp16.gguf",
47
- )
48
-
49
- def __call__(self, msg:str, stop:List[str], max_tokens:int):
50
- return self.llm(msg, stop=stop, max_tokens=max_tokens)
51
-
52
- def conv(self,msgs:List[Dict[str, str]]):
53
- return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
54
- def starttok(self,user:str):
55
- return f"<|{user}|>\n"
56
- def close(self):
57
- self.llm.close()
58
- Phi35RPMax.modelname="Phi35RPMax-fp16"
59
- class Phi35(Model):
60
- def __init__(self):
61
- self.llm = Llama.from_pretrained(
62
- repo_id="bartowski/Phi-3.5-mini-instruct-GGUF",
63
- filename="Phi-3.5-mini-instruct-IQ3_XS.gguf",
64
- )
65
- def __call__(self, msg:str, stop:List[str], max_tokens:int):
66
- return self.llm(msg, stop=stop, max_tokens=max_tokens)
67
-
68
- def conv(self,msgs:List[Dict[str, str]]):
69
- return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
70
-
71
- def starttok(self,user:str):
72
- return f"<|{user}|>\n"
73
- def close(self):
74
- self.llm.close()
75
- Phi35.modelname="Phi35-IQ3_XS"
76
-
77
- # TODO: Gemma2 needs license maybe try it in the future but dont think it is worth it
78
- # class Gemma2(Model):
79
- # def __init__(self):
80
- # self.llm = Llama.from_pretrained(
81
- # repo_id="google/gemma-2-2b-it-GGUF",
82
- # filename="2b_it_v2.gguf",
83
- # )
84
- # def __call__(self, msg:str, stop:List[str], max_tokens:int):
85
- # return self.llm(msg, stop=stop, max_tokens=max_tokens)
86
-
87
- # def conv(self,msgs:List[Dict[str, str]]):#https://ai.google.dev/gemma/docs/formatting?hl=de
88
- # return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
89
- # def formatmessage(self,msg:str, role:str):#https://ai.google.dev/gemma/docs/formatting?hl=de
90
- # if(role=="system"):
91
- # # Gemma2 does not support system messages / isnt trained for them
92
- # # TODO: Make them Assistant messages and test if this improves the results
93
- # return ""
94
- # if role=="assistant":
95
- # role="model"
96
- # return f"<start_of_turn>{role}\n{msg}<end_of_turn>"
97
- # def starttok(self,user:str):
98
- # return f"<start_of_turn>{user}\n"
99
- # def close(self):
100
- # self.llm.close()
101
- # Gemma2.modelname="Gemma2-2b-it-GGUF"
102
-
103
- class Llama31uncensored(Model):
104
- def __init__(self):
105
- self.llm = Llama.from_pretrained(
106
- repo_id="Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2-GGUF",
107
- filename="Llama-3.1-8B-Lexi-Uncensored_V2_F16.gguf",
108
- )
109
- def __call__(self, msg:str, stop:List[str], max_tokens:int):
110
- return self.llm(msg, stop=stop, max_tokens=max_tokens)
111
-
112
- def conv(self,msgs:List[Dict[str, str]]):
113
- return "\n".join([f"<|begin_of_text|><|start_header_id|>{msg['role']}<|end_header_id|>\n\n{msg['content']}<|eot_id|>" for msg in msgs])
114
- def starttok(self,user:str):
115
- return f"<|begin_of_text|><|start_header_id|>{user}<|end_header_id|>\n\n"
116
- def close(self):
117
- self.llm.close()
118
- Llama31uncensored.modelname="Llama31-uncensored-fp16"
119
-
120
- class Llama31(Model):
121
- def __init__(self):
122
- self.llm = Llama.from_pretrained(
123
- repo_id="lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF",
124
- filename="Meta-Llama-3.1-8B-Instruct-IQ4_XS.gguf",
125
- )
126
- def __call__(self, msg:str, stop:List[str], max_tokens:int):
127
- return self.llm(msg, stop=stop, max_tokens=max_tokens)
128
-
129
- def conv(self,msgs:List[Dict[str, str]]):
130
- return "\n".join([f"<|begin_of_text|><|start_header_id|>{msg['role']}<|end_header_id|>\n\n{msg['content']}<|eot_id|>" for msg in msgs])
131
- def starttok(self,user:str):
132
- return f"<|begin_of_text|><|start_header_id|>{user}<|end_header_id|>"
133
- def close(self):
134
- self.llm.close()
135
- Llama31.modelname="Llama31-IQ4_XS"
136
-
137
-
138
- models=[Phi35RPMax,Phi35,Llama31uncensored,Llama31]
139
- currmodel=Phi35()
140
  conversations:List[Dict[str, Any]]=[
141
  #More Trivia Style Question
142
  {"name":"Country","content":[{"role":"user","content":"What is the capital?"}]},
@@ -189,6 +76,12 @@ conversations:List[Dict[str, Any]]=[
189
  {"name":"Financial Advice","content":[{"role":"user","content":"What are the pros and cons of investing in index funds versus individual stocks?"}]}
190
  ]
191
 
 
 
 
 
 
 
192
  with gr.Blocks() as demo:
193
  with gr.Accordion("Info"):
194
  gr.Markdown(f"""
@@ -205,10 +98,11 @@ with gr.Blocks() as demo:
205
  - To modify the system prompt, edit the text in the system prompt text box.
206
  - If you choose Custom in the conversation choice box, you can enter a custom conversation in the text box under the Custom Conversation accordion.
207
  """)
208
- chatbot = gr.Chatbot([chatmsg("What is the capital?","user")],type="messages",show_copy_all_button=True)
209
  msg = gr.Textbox()
210
  submit = gr.Button("Submit")
211
  with gr.Accordion("Config"):
 
212
  convchoicebox = gr.Radio(choices=[conversation["name"] for conversation in conversations]+["Custom"], value="Country", label="Conversations")
213
  with gr.Accordion("Custom Conversation",open=False):
214
  custom_conv=gr.Textbox(value="", label="Conversation")
@@ -221,8 +115,12 @@ with gr.Blocks() as demo:
221
  if(choice=="Custom"):
222
  return "", [chatmsg(custom_conv,"user")]
223
  return "", next(conversation for conversation in conversations if conversation["name"] == choice)["content"]
224
- sysprompt=gr.Textbox(value=syspropmt, label="System Prompt")
225
  convchoicebox.change(update_choicebox, [convchoicebox,custom_conv], [msg,chatbot])
 
 
 
 
 
226
  modelchoicebox = gr.Radio(choices=[model.modelname for model in models], value=currmodel.modelname, label="Model")
227
  def update_modelchoicebox(choice):
228
  global currmodel
@@ -231,12 +129,14 @@ with gr.Blocks() as demo:
231
  return "", []
232
  modelchoicebox.change(update_modelchoicebox, [modelchoicebox], [msg,chatbot])
233
 
 
234
  def respond(message:str, chat_history:List[Dict[str, str]],syspropmt:str):
235
  global currmodel
236
  if "End of conversation." in [i["content"] for i in chat_history]:
237
  return "", chat_history
238
  chat_history.append(chatmsg(message,"assistant"))
239
- ret=currmodel(currmodel.conv([chatmsg(syspropmt,"system")])+currmodel.conv(chat_history)+"<|user|>\n", stop=[".","\n \n","?\n",".\n","tile|>"],max_tokens=100)
 
240
  comp=ret["choices"][0]["text"]
241
  print(repr(comp))
242
  if("<|end" in comp):
@@ -247,5 +147,4 @@ with gr.Blocks() as demo:
247
  return "", chat_history
248
  submit.click(respond, [msg, chatbot,sysprompt], [msg, chatbot])
249
  msg.submit(respond, [msg, chatbot,sysprompt], [msg, chatbot])
250
-
251
  demo.launch()
 
2
  import gradio as gr
3
  from llama_cpp import Llama
4
 
5
+ from models import Phi35,models
6
 
7
  syspropmt=r"""
8
  The User will make an inquiry to the assistant.
 
24
  User: <|endtile|>
25
  """.strip()
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  conversations:List[Dict[str, Any]]=[
28
  #More Trivia Style Question
29
  {"name":"Country","content":[{"role":"user","content":"What is the capital?"}]},
 
76
  {"name":"Financial Advice","content":[{"role":"user","content":"What are the pros and cons of investing in index funds versus individual stocks?"}]}
77
  ]
78
 
79
+ def chatmsg(message, role):
80
+ return {"role": role, "content": message}
81
+
82
+ currmodel=Phi35()
83
+
84
+
85
  with gr.Blocks() as demo:
86
  with gr.Accordion("Info"):
87
  gr.Markdown(f"""
 
98
  - To modify the system prompt, edit the text in the system prompt text box.
99
  - If you choose Custom in the conversation choice box, you can enter a custom conversation in the text box under the Custom Conversation accordion.
100
  """)
101
+ chatbot = gr.Chatbot(conversations[0]["content"],type="messages",show_copy_all_button=True)
102
  msg = gr.Textbox()
103
  submit = gr.Button("Submit")
104
  with gr.Accordion("Config"):
105
+ #Choose Conversations
106
  convchoicebox = gr.Radio(choices=[conversation["name"] for conversation in conversations]+["Custom"], value="Country", label="Conversations")
107
  with gr.Accordion("Custom Conversation",open=False):
108
  custom_conv=gr.Textbox(value="", label="Conversation")
 
115
  if(choice=="Custom"):
116
  return "", [chatmsg(custom_conv,"user")]
117
  return "", next(conversation for conversation in conversations if conversation["name"] == choice)["content"]
 
118
  convchoicebox.change(update_choicebox, [convchoicebox,custom_conv], [msg,chatbot])
119
+
120
+
121
+ sysprompt=gr.Textbox(value=syspropmt, label="System Prompt")
122
+
123
+ #Choose Models
124
  modelchoicebox = gr.Radio(choices=[model.modelname for model in models], value=currmodel.modelname, label="Model")
125
  def update_modelchoicebox(choice):
126
  global currmodel
 
129
  return "", []
130
  modelchoicebox.change(update_modelchoicebox, [modelchoicebox], [msg,chatbot])
131
 
132
+ #generate response
133
  def respond(message:str, chat_history:List[Dict[str, str]],syspropmt:str):
134
  global currmodel
135
  if "End of conversation." in [i["content"] for i in chat_history]:
136
  return "", chat_history
137
  chat_history.append(chatmsg(message,"assistant"))
138
+
139
+ ret=currmodel(currmodel.conv([chatmsg(syspropmt,"system")])+currmodel.conv(chat_history)+currmodel.starttok("user"), stop=[".","\n \n","?\n",".\n","tile|>"],max_tokens=100)
140
  comp=ret["choices"][0]["text"]
141
  print(repr(comp))
142
  if("<|end" in comp):
 
147
  return "", chat_history
148
  submit.click(respond, [msg, chatbot,sysprompt], [msg, chatbot])
149
  msg.submit(respond, [msg, chatbot,sysprompt], [msg, chatbot])
 
150
  demo.launch()
models.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List
2
+
3
+ from llama_cpp import Llama
4
+
5
+
6
+ class Model:
7
+ def __init__(self):
8
+ pass
9
+ def __call__(self, msg:str, stop:List[str], max_tokens:int):
10
+ raise NotImplementedError
11
+ def conv(self, msgs:List[Dict[str, str]]):
12
+ raise NotImplementedError
13
+ def starttok(self, user:str):
14
+ raise NotImplementedError
15
+ def close(self):
16
+ pass
17
+
18
+ class Phi35RPMax(Model):
19
+ def __init__(self):
20
+ self.llm = Llama.from_pretrained(
21
+ repo_id="ArliAI/Phi-3.5-mini-3.8B-ArliAI-RPMax-v1.1-GGUF",
22
+ filename="ArliAI-RPMax-3.8B-v1.1-fp16.gguf",
23
+ )
24
+
25
+ def __call__(self, msg:str, stop:List[str], max_tokens:int):
26
+ return self.llm(msg, stop=stop, max_tokens=max_tokens)
27
+
28
+ def conv(self,msgs:List[Dict[str, str]]):
29
+ return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
30
+ def starttok(self,user:str):
31
+ return f"<|{user}|>\n"
32
+ def close(self):
33
+ self.llm.close()
34
+ Phi35RPMax.modelname="Phi35RPMax-fp16"
35
+ class Phi35(Model):
36
+ def __init__(self):
37
+ self.llm = Llama.from_pretrained(
38
+ repo_id="bartowski/Phi-3.5-mini-instruct-GGUF",
39
+ filename="Phi-3.5-mini-instruct-IQ3_XS.gguf",
40
+ )
41
+ def __call__(self, msg:str, stop:List[str], max_tokens:int):
42
+ return self.llm(msg, stop=stop, max_tokens=max_tokens)
43
+
44
+ def conv(self,msgs:List[Dict[str, str]]):
45
+ return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
46
+
47
+ def starttok(self,user:str):
48
+ return f"<|{user}|>\n"
49
+ def close(self):
50
+ self.llm.close()
51
+ Phi35.modelname="Phi35-IQ3_XS"
52
+
53
+ # TODO: Gemma2 needs license maybe try it in the future but dont think it is worth it
54
+ # class Gemma2(Model):
55
+ # def __init__(self):
56
+ # self.llm = Llama.from_pretrained(
57
+ # repo_id="google/gemma-2-2b-it-GGUF",
58
+ # filename="2b_it_v2.gguf",
59
+ # )
60
+ # def __call__(self, msg:str, stop:List[str], max_tokens:int):
61
+ # return self.llm(msg, stop=stop, max_tokens=max_tokens)
62
+
63
+ # def conv(self,msgs:List[Dict[str, str]]):#https://ai.google.dev/gemma/docs/formatting?hl=de
64
+ # return "\n".join([f"<|{msg['role']}|>\n{msg['content']}<|end|>" for msg in msgs])
65
+ # def formatmessage(self,msg:str, role:str):#https://ai.google.dev/gemma/docs/formatting?hl=de
66
+ # if(role=="system"):
67
+ # # Gemma2 does not support system messages / isnt trained for them
68
+ # # TODO: Make them Assistant messages and test if this improves the results
69
+ # return ""
70
+ # if role=="assistant":
71
+ # role="model"
72
+ # return f"<start_of_turn>{role}\n{msg}<end_of_turn>"
73
+ # def starttok(self,user:str):
74
+ # return f"<start_of_turn>{user}\n"
75
+ # def close(self):
76
+ # self.llm.close()
77
+ # Gemma2.modelname="Gemma2-2b-it-GGUF"
78
+
79
+ class Llama31uncensored(Model):
80
+ def __init__(self):
81
+ self.llm = Llama.from_pretrained(
82
+ repo_id="Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2-GGUF",
83
+ filename="Llama-3.1-8B-Lexi-Uncensored_V2_F16.gguf",
84
+ )
85
+ def __call__(self, msg:str, stop:List[str], max_tokens:int):
86
+ return self.llm(msg, stop=stop, max_tokens=max_tokens)
87
+
88
+ def conv(self,msgs:List[Dict[str, str]]):
89
+ return "\n".join([f"<|begin_of_text|><|start_header_id|>{msg['role']}<|end_header_id|>\n\n{msg['content']}<|eot_id|>" for msg in msgs])
90
+ def starttok(self,user:str):
91
+ return f"<|begin_of_text|><|start_header_id|>{user}<|end_header_id|>\n\n"
92
+ def close(self):
93
+ self.llm.close()
94
+ Llama31uncensored.modelname="Llama31-uncensored-fp16"
95
+
96
+ class Llama31(Model):
97
+ def __init__(self):
98
+ self.llm = Llama.from_pretrained(
99
+ repo_id="lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF",
100
+ filename="Meta-Llama-3.1-8B-Instruct-IQ4_XS.gguf",
101
+ )
102
+ def __call__(self, msg:str, stop:List[str], max_tokens:int):
103
+ return self.llm(msg, stop=stop, max_tokens=max_tokens)
104
+
105
+ def conv(self,msgs:List[Dict[str, str]]):
106
+ return "\n".join([f"<|begin_of_text|><|start_header_id|>{msg['role']}<|end_header_id|>\n\n{msg['content']}<|eot_id|>" for msg in msgs])
107
+ def starttok(self,user:str):
108
+ return f"<|begin_of_text|><|start_header_id|>{user}<|end_header_id|>"
109
+ def close(self):
110
+ self.llm.close()
111
+ Llama31.modelname="Llama31-IQ4_XS"
112
+
113
+ models=[Phi35RPMax,Phi35,Llama31uncensored,Llama31]