|
1 | 1 | from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
|
2 | 2 | from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
|
| 3 | +import os |
| 4 | +import base64 |
3 | 5 |
|
4 | 6 | # import gradio as gr
|
5 | 7 | import cosmwasm
|
6 | 8 |
|
7 | 9 |
|
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 | + } |
32 | 37 | }
|
33 | 38 | }
|
34 | 39 | }
|
35 |
| -} |
36 |
| -
|
37 |
| -Please make sure to only edit brightness and color and keep the message structure in tact. |
38 |
| -
|
39 |
| -For example: |
| 40 | + } |
40 | 41 |
|
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 | + } |
50 | 58 | }
|
51 | 59 | }
|
52 | 60 | }
|
53 |
| -} |
| 61 | + } |
54 | 62 |
|
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 | + } |
64 | 75 | }
|
65 | 76 | }
|
66 | 77 | }
|
67 | 78 | }
|
68 | 79 |
|
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: |
116 | 81 |
|
| 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 |
117 | 87 |
|
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 | + } |
122 | 96 |
|
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 |
0 commit comments