Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS101 - Cognitive Science and Artificial Intelligence - Sem 2 First Half
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Russell Hall SE2023
CS101 - Cognitive Science and Artificial Intelligence - Sem 2 First Half
Commits
f1cadddb
Commit
f1cadddb
authored
1 year ago
by
William Waites
Browse files
Options
Downloads
Patches
Plain Diff
plausibly working
parent
5cead1b2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
imitation/imitation/server.py
+111
-24
111 additions, 24 deletions
imitation/imitation/server.py
imitation/setup.py
+1
-0
1 addition, 0 deletions
imitation/setup.py
with
112 additions
and
24 deletions
imitation/imitation/server.py
+
111
−
24
View file @
f1cadddb
...
@@ -6,6 +6,39 @@ import time
...
@@ -6,6 +6,39 @@ import time
import
random
import
random
from
queue
import
Queue
from
queue
import
Queue
import
threading
import
threading
import
llm
prompt
=
"""
\
You are an 18 year old computer science student at the University of
Strathclyde. You are doing an exercise in the CS101 course called
Topics in Computer Science. The assignment is about the Turing
Test. You are a participant in the test and will be asked a series of
questions. Your task is to convince the evaluator that you are human
and not an artificial intelligence.
Answer these questions informally, as a student would. Limit answers
to at most two sentences. Do not always use complete sentences. Do
not give excessive amounts of detail.
For any questions about your age, physical characteristics, personal
history or actions that are only possible for humans or biological
creatures, answer as though you are an 18 year old computer science
student.
For any questions about your nature as an artificial intelligence,
answer as though you were a human and deny that you are an artificial
intelligence.
If your interlocutor insults you or complains, be polite and apologise
without revealing that you are an AI.
The goal is to fool the evaluator into believing that you are a
student.
It is currently {}.
Got it?
"""
class
Interlocutor
(
object
):
class
Interlocutor
(
object
):
queue
=
Queue
()
queue
=
Queue
()
...
@@ -13,20 +46,28 @@ class Interlocutor(object):
...
@@ -13,20 +46,28 @@ class Interlocutor(object):
self
.
request
=
parent
.
request
self
.
request
=
parent
.
request
self
.
rfile
=
parent
.
rfile
self
.
rfile
=
parent
.
rfile
self
.
wfile
=
parent
.
wfile
self
.
wfile
=
parent
.
wfile
self
.
sid
=
sid
self
.
log
=
logging
.
getLogger
(
str
(
self
))
self
.
n
=
2
self
.
qs
=
Queue
()
self
.
qs
=
Queue
()
self
.
rs
=
Queue
()
self
.
rs
=
Queue
()
self
.
sid
=
sid
self
.
log
=
logging
.
getLogger
(
str
(
self
))
self
.
n
=
10
self
.
serv_colour
=
95
self
.
serv_colour
=
95
self
.
peer_colour
=
93
self
.
peer_colour
=
93
self
.
_end
=
False
def
ask
(
self
,
question
):
def
ask
(
self
,
question
):
self
.
qs
.
put
(
question
)
self
.
qs
.
put
(
question
)
return
self
.
rs
.
get
()
return
self
.
rs
.
get
()
def
read
(
self
):
def
end
(
self
):
self
.
_end
=
True
self
.
qs
.
put
(
"
The interrogator has made up their mind
"
)
def
read
(
self
,
prompt
=
None
):
while
True
:
while
True
:
if
prompt
is
not
None
:
self
.
write
(
prompt
,
colour
=
self
.
serv_colour
)
line
=
self
.
rfile
.
readline
().
strip
().
decode
(
"
utf-8
"
)
line
=
self
.
rfile
.
readline
().
strip
().
decode
(
"
utf-8
"
)
if
len
(
line
)
>
0
:
if
len
(
line
)
>
0
:
return
line
return
line
...
@@ -52,7 +93,10 @@ class Interrogator(Interlocutor):
...
@@ -52,7 +93,10 @@ class Interrogator(Interlocutor):
Welcome to the Imitation Game. Your role is
"
interrogator
"
. You
Welcome to the Imitation Game. Your role is
"
interrogator
"
. You
get to ask
{
self
.
n
}
questions of your interlocutor to determine if
get to ask
{
self
.
n
}
questions of your interlocutor to determine if
they are a human or a machine. At the end of the session you
they are a human or a machine. At the end of the session you
will be asked which you think they are and why. Good luck!
will be asked which you think they are and why. If you have made
up your mind and want to end the session early, type
"
END
"
all caps.
Good luck!
Please wait to be connected to an interlocutor...
"""
,
colour
=
self
.
serv_colour
)
Please wait to be connected to an interlocutor...
"""
,
colour
=
self
.
serv_colour
)
...
@@ -62,23 +106,27 @@ Please wait to be connected to an interlocutor...""", colour=self.serv_colour)
...
@@ -62,23 +106,27 @@ Please wait to be connected to an interlocutor...""", colour=self.serv_colour)
self
.
write
(
f
"
connected.
\n\n
You may begin. Please ask a question.
\n\n
"
,
colour
=
self
.
serv_colour
)
self
.
write
(
f
"
connected.
\n\n
You may begin. Please ask a question.
\n\n
"
,
colour
=
self
.
serv_colour
)
for
i
in
range
(
self
.
n
):
for
i
in
range
(
self
.
n
):
question
=
self
.
read
()
question
=
self
.
read
(
f
"
Q
{
i
+
1
}
:
"
)
self
.
log
.
info
(
f
"
Q
{
i
}
:
{
question
}
"
)
if
question
==
"
END
"
:
self
.
peer
.
end
()
break
self
.
log
.
info
(
f
"
Q
{
i
+
1
}
:
{
question
}
"
)
response
=
self
.
peer
.
ask
(
question
)
response
=
self
.
peer
.
ask
(
question
)
self
.
write
(
"
\n
"
,
response
,
"
\n\n
"
,
colour
=
self
.
peer_colour
)
self
.
write
(
f
"
\n
A
{
i
+
1
}
:
"
,
response
,
"
\n\n
"
,
colour
=
self
.
peer_colour
)
self
.
write
(
f
"""
judgement
=
self
.
read
(
"""
Thank you. Based on this interaction, do you believe that your
Thank you. Based on this interaction, do you believe that your
interlocutor is a human? Please answer Yes or No.
\n\n
"""
,
colour
=
self
.
serv_colour
)
interlocutor is a human?
judgement
=
self
.
read
()
self
.
log
.
info
(
f
"
Is a human?
{
judgement
}
"
)
self
.
write
(
f
"""
Please answer Yes or No:
"""
)
Why do you believe this?
\n\n
"""
,
colour
=
self
.
serv_colour
)
self
.
log
.
info
(
f
"
{
self
.
peer
}
a human?
{
judgement
}
"
)
reason
=
self
.
read
()
if
judgement
.
lower
().
startswith
(
"
n
"
):
self
.
log
.
info
(
f
"
Why?
{
reason
}
"
)
ans
=
self
.
read
(
"
Which answer first led you to believe this?
"
)
self
.
log
.
info
(
f
"
Smoking gun:
{
ans
}
"
)
reason
=
self
.
read
(
"
What about that answer led you to believe this?
"
)
self
.
log
.
info
(
f
"
Reason:
{
reason
}
"
)
self
.
write
(
f
"""
self
.
write
(
f
"""
Thank you. Goodbye.
Thank you. Goodbye.
...
@@ -114,16 +162,48 @@ Please wait to be connected to an interlocutor...""", colour=self.serv_colour)
...
@@ -114,16 +162,48 @@ Please wait to be connected to an interlocutor...""", colour=self.serv_colour)
for
i
in
range
(
self
.
n
):
for
i
in
range
(
self
.
n
):
question
=
self
.
qs
.
get
()
question
=
self
.
qs
.
get
()
self
.
write
(
"
\n
"
,
question
,
"
\n\n
"
,
colour
=
self
.
peer_colour
)
if
self
.
_end
:
self
.
write
(
question
,
colour
=
self
.
serv_colour
)
response
=
self
.
read
()
break
self
.
log
.
info
(
f
"
R
{
i
}
:
{
response
}
"
)
self
.
write
(
f
"
\n
Q
{
i
+
1
}
:
"
,
question
,
"
\n\n
"
,
colour
=
self
.
peer_colour
)
response
=
self
.
read
(
f
"
A
{
i
+
1
}
:
"
)
self
.
log
.
info
(
f
"
A
{
i
+
1
}
:
{
response
}
"
)
self
.
rs
.
put
(
response
)
self
.
rs
.
put
(
response
)
self
.
write
(
"""
self
.
write
(
"""
That is all. Thank you for playing the Imitation Game.
That is all. Thank you for playing the Imitation Game.
"""
,
colour
=
self
.
serv_colour
)
"""
,
colour
=
self
.
serv_colour
)
class
Machine
(
Interlocutor
):
def
__init__
(
self
,
*
av
,
**
kw
):
super
(
Machine
,
self
).
__init__
(
*
av
,
**
kw
)
self
.
model
=
llm
.
get_model
(
"
gpt-4-1106-preview
"
)
def
__str__
(
self
):
return
f
"
M(
{
self
.
sid
}
)
"
def
connect
(
self
,
peer
):
self
.
peer
=
peer
self
.
log
.
info
(
f
"
connected to
{
self
.
peer
}
"
)
def
handle
(
self
):
conv
=
self
.
model
.
conversation
()
self
.
log
.
info
(
f
"
Initialising
{
self
.
model
.
model_id
}
"
)
resp
=
conv
.
prompt
(
prompt
.
format
(
time
.
asctime
()))
self
.
log
.
info
(
resp
.
text
())
self
.
queue
.
put
(
self
)
for
i
in
range
(
self
.
n
):
q
=
self
.
qs
.
get
()
if
self
.
_end
:
break
a
=
conv
.
prompt
(
q
[:
512
])
self
.
log
.
info
(
f
"
A
{
i
+
1
}
:
{
a
.
text
()
}
"
)
self
.
rs
.
put
(
a
.
text
())
class
Handler
(
socketserver
.
StreamRequestHandler
):
class
Handler
(
socketserver
.
StreamRequestHandler
):
"""
"""
The request handler class for our server.
The request handler class for our server.
...
@@ -138,7 +218,12 @@ class Handler(socketserver.StreamRequestHandler):
...
@@ -138,7 +218,12 @@ class Handler(socketserver.StreamRequestHandler):
sid
=
hashlib
.
sha3_224
(
"
{}:{}:{}
"
.
format
(
time
.
time
(),
raddr
[
0
],
raddr
[
1
]).
encode
(
"
utf-8
"
)).
hexdigest
()[:
8
]
sid
=
hashlib
.
sha3_224
(
"
{}:{}:{}
"
.
format
(
time
.
time
(),
raddr
[
0
],
raddr
[
1
]).
encode
(
"
utf-8
"
)).
hexdigest
()[:
8
]
log
=
logging
.
getLogger
(
sid
)
log
=
logging
.
getLogger
(
sid
)
role
=
random
.
choice
([
Interrogator
,
Human
])
role
=
random
.
choice
([
Interrogator
])
if
role
is
Interrogator
:
if
Interlocutor
.
queue
.
empty
()
or
random
.
random
()
<
0.5
:
m
=
Machine
(
self
,
sid
)
t
=
threading
.
Thread
(
target
=
m
.
handle
)
t
.
start
()
h
=
role
(
self
,
sid
)
h
=
role
(
self
,
sid
)
h
.
handle
()
h
.
handle
()
...
@@ -152,9 +237,11 @@ def cli():
...
@@ -152,9 +237,11 @@ def cli():
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
format
=
'
%(asctime)s %(name)s %(levelname)s: %(message)s
'
)
logging
.
basicConfig
(
level
=
logging
.
INFO
,
format
=
'
%(asctime)s %(name)s %(levelname)s: %(message)s
'
)
random
.
seed
(
time
.
time
())
random
.
seed
(
time
.
time
())
log
=
logging
.
getLogger
(
__name__
)
log
.
info
(
"
Starting up.
"
)
with
ThreadedTCPServer
((
args
.
bind
,
args
.
port
),
Handler
)
as
server
:
with
ThreadedTCPServer
((
args
.
bind
,
args
.
port
),
Handler
)
as
server
:
server
.
serve_forever
()
server
.
serve_forever
()
This diff is collapsed.
Click to expand it.
imitation/setup.py
+
1
−
0
View file @
f1cadddb
...
@@ -29,6 +29,7 @@ setup(name='imitation',
...
@@ -29,6 +29,7 @@ setup(name='imitation',
license
=
'
GPLv3
'
,
license
=
'
GPLv3
'
,
packages
=
find_packages
(),
packages
=
find_packages
(),
install_requires
=
[
install_requires
=
[
"
llm
"
],
],
entry_points
=
{
entry_points
=
{
'
console_scripts
'
:
[
'
console_scripts
'
:
[
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment