Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec21dbb

Browse files
author
bluenote
committed
wip: message keys
1 parent e5e35c5 commit ec21dbb

File tree

2 files changed

+140
-103
lines changed

2 files changed

+140
-103
lines changed
Lines changed: 136 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,159 @@
11
from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
22
from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
3+
import os
4+
import base64
35

46
# import gradio as gr
57
import cosmwasm
68

79

8-
template = "{history}{mw_input}"
9-
human_input = ""
10-
11-
mw_prompt = PromptTemplate(input_variables=["history", "mw_input"], template=template)
12-
13-
mw_chain = LLMChain(
14-
llm=OpenAI(temperature=0),
15-
prompt=mw_prompt,
16-
verbose=True,
17-
memory=ConversationalBufferWindowMemory(k=2),
18-
)
19-
20-
mw_learnings = """
21-
Whenever I say "Microworld Update:" followed by a description of the aesthetic, modify the following message
22-
with the hexadecimal color value and brightness value that may best represent the mood and vibes of the specification.
23-
Include specification string itself instead of <specification> into the title field. The output must be valid JSON.
24-
25-
{
26-
"propose": {
27-
"msg": {
28-
"propose": {
29-
"title": "<specification>",
30-
"description": "'brightness': '<brightness-value>', 'color': '<hexadecimal-color-value>'",
31-
"msgs": []
10+
class Agent:
11+
def init(self):
12+
self.subdao = os.getenv("SUBDAO")
13+
template = "{history}{mw_input}"
14+
mw_prompt = PromptTemplate(input_variables=["history", "mw_input"], template=template)
15+
16+
self.mw_chain = LLMChain(
17+
llm=OpenAI(temperature=0),
18+
prompt=mw_prompt,
19+
verbose=True,
20+
memory=ConversationalBufferWindowMemory(k=2),
21+
)
22+
23+
mw_learnings = """
24+
Whenever I say "Microworld Update:" followed by a description of the aesthetic, modify the following message
25+
with the hexadecimal color value and brightness value that may best represent the mood and vibes of the specification.
26+
Include specification string itself instead of <specification> into the title field. The output must be valid JSON.
27+
28+
{
29+
"microworld": {
30+
"propose": {
31+
"msg": {
32+
"propose": {
33+
"title": "<specification>",
34+
"description": "'brightness': '<brightness-value>', 'color': '<hexadecimal-color-value>'",
35+
"msgs": []
36+
}
3237
}
3338
}
3439
}
35-
}
36-
37-
Please make sure to only edit brightness and color and keep the message structure in tact.
38-
39-
For example:
40+
}
4041
41-
Input: shadowy supercoder
42-
Output:
43-
{
44-
"propose": {
45-
"msg": {
46-
"propose": {
47-
"title": "shadowy supercoder",
48-
"description": "'brightness': 40, color: '#333333'",
49-
"msgs": []
42+
Please make sure to only edit brightness and color and keep the message structure in tact.
43+
44+
For example:
45+
46+
Input: shadowy supercoder
47+
Output:
48+
{
49+
"microworld":
50+
{
51+
"propose": {
52+
"msg": {
53+
"propose": {
54+
"title": "shadowy supercoder",
55+
"description": "'brightness': 40, color: '#333333'",
56+
"msgs": []
57+
}
5058
}
5159
}
5260
}
53-
}
61+
}
5462
55-
Input: luxury space gay communism
56-
Out
57-
{
58-
"propose": {
59-
"msg": {
60-
"propose": {
61-
"title": "luxury space gay communism",
62-
"description": "'brightness': '100', 'color': '#C0C0C0'",
63-
"msgs": []
63+
Input: luxury space gay communism
64+
Output:
65+
{
66+
"microworld":
67+
{
68+
"propose": {
69+
"msg": {
70+
"propose": {
71+
"title": "luxury space gay communism",
72+
"description": "'brightness': '100', 'color': '#C0C0C0'",
73+
"msgs": []
74+
}
6475
}
6576
}
6677
}
6778
}
6879
69-
And whenever I say things like:
70-
71-
- include juno9000 into DAO
72-
- add to Plurigrid juno9000
73-
- + juno9000 to DAO
74-
- juno9000 add plzzzz
75-
- Add my address juno9000 to the DAO
76-
77-
and similar things, where "juno9000" is the address of the member to be added,
78-
I want you to output the following message:
79-
add_members_msg = {
80-
"update_members": {
81-
"add": [{"weight": 1, "addr": "<addr>"}],
82-
"remove": [],
83-
}
84-
}
85-
86-
Make sure you only modify the <addr> field and the rest of the message remains the same.
87-
88-
Thanks!
89-
"""
90-
91-
mw_chain.predict(mw_input=mw_learnings)
92-
93-
# retries = 0
94-
95-
96-
async def output(your_microworld_aesthetic):
97-
print(your_microworld_aesthetic)
98-
msg = mw_chain.predict(mw_input="Microworld Update: {}".format(your_microworld_aesthetic))
99-
# if needed structurally edit with regex: msg_edited = re.sub("^AI:\s*", "", output)
100-
print(msg)
101-
# Retry cosmwasm msg automatically until valid
102-
try:
103-
await cosmwasm.execute_msg(msg)
104-
except Exception as e:
105-
print(e)
106-
# print("CosmWasm message not valid, retrying...")
107-
# retries += 1
108-
# if retries < 5:
109-
# await output(your_microworld_aesthetic)
110-
# else:
111-
print("CosmWasm message not valid, giving up.")
112-
return (
113-
"View your proposal here!: https://daodao.zone/dao/juno1jeq3xqkn9kypghjeqjnhsdlnjsltajm8r3s80tem3juxsjlfan3s2kzmsd#proposals\n"
114-
+ msg
115-
)
80+
And whenever I say things like:
11681
82+
- include juno9000 into DAO
83+
- add to Plurigrid juno9000
84+
- + juno9000 to DAO
85+
- juno9000 add plzzzz
86+
- Add my address juno9000 to the DAO
11787
118-
async def test(message):
119-
msg = mw_chain.predict(mw_input=message)
120-
return msg
121-
88+
and similar things, where "juno9000" is the address of the member to be added,
89+
I want you to output the following message:
90+
{"members": add_members_msg = {
91+
"update_members": {
92+
"add": [{"weight": 1, "addr": "<addr>"}],
93+
"remove": [],
94+
}
95+
}
12296
123-
# demo = gr.Interface(fn=output, inputs="text", outputs="text", input_desc="Microworld vibe")
124-
# demo.launch()
97+
Make sure you only modify the <addr> field and the rest of the message remains the same.
98+
99+
Thanks!
100+
"""
101+
102+
self.mw_chain.predict(mw_input=mw_learnings)
103+
104+
async def execute_cosmwasm(self, your_microworld_aesthetic):
105+
subdao = os.getenv("SUBDAO")
106+
print(your_microworld_aesthetic)
107+
msg = self.mw_chain.predict(
108+
mw_input="Microworld Update: {}".format(your_microworld_aesthetic)
109+
)
110+
print(msg)
111+
try:
112+
await cosmwasm.execute_msg(msg)
113+
except Exception as e:
114+
print(e)
115+
return "CosmWasm message not valid, giving up. Try again with a different wording."
116+
return (
117+
"View your proposal here!: https://daodao.zone/dao/{subdao}#proposals\n".format(
118+
subdao=subdao
119+
)
120+
+ msg
121+
)
122+
123+
async def handle_query(self, message):
124+
msg = self.mw_chain.predict(mw_input=message)
125+
# return self.execute_cosmwasm(self.format_add_member_msg(msg))
126+
print(msg)
127+
128+
def format_add_member_msg(self, llm_response):
129+
response_bytes = llm_response.encode("ascii")
130+
base64_bytes = base64.b64encode(response_bytes)
131+
base64_msg = base64_bytes.decode("ascii")
132+
133+
msg = """
134+
{
135+
"propose": {
136+
"msg": {
137+
"propose": {
138+
"title": "Add member to a DAO",
139+
"description": "Adding a new member",
140+
"msgs": [
141+
{
142+
"wasm": {
143+
"execute": {
144+
"contract_addr": "%s",
145+
"funds": [],
146+
"msg": "%s"
147+
}
148+
}
149+
}
150+
]
151+
}
152+
}
153+
}
154+
}
155+
""" % (
156+
self.subdao,
157+
base64_msg,
158+
)
159+
return msg

zulip_bots/zulip_bots/bots/plurigrid-langchain/plurigrid-langchain.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55

66
class PlurigridHandler(object):
7-
# def initialize(self, bot_handler):
7+
def initialize(self, bot_handler):
8+
self.agent = agent.Agent()
9+
self.agent.init()
810

911
def usage(self):
1012
return """
@@ -14,7 +16,7 @@ def usage(self):
1416

1517
def handle_message(self, message, bot_handler):
1618
loop = asyncio.get_event_loop()
17-
coroutine = agent.test(message["content"])
19+
coroutine = self.agent.handle_query(message["content"])
1820
reply = loop.run_until_complete(coroutine)
1921
bot_handler.send_reply(message, reply)
2022

0 commit comments

Comments
 (0)