Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: code generator (wip) #257

Draft
wants to merge 30 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
23fe1dc
feat: code runner (wip)
marcomariscal Sep 25, 2023
c49a1e6
fix: spelling
marcomariscal Sep 25, 2023
e31cd0a
feat: config for generate_code
marcomariscal Sep 25, 2023
d237a5a
chore: index bump
marcomariscal Sep 25, 2023
5390e55
feat: generate code tool into index_widget
marcomariscal Sep 25, 2023
d21799d
feat: generate_code handler (wip)
marcomariscal Sep 25, 2023
0849d6d
fix: try to prioritize gen_code when code context
marcomariscal Sep 26, 2023
c4c8c78
fix: no display
marcomariscal Sep 26, 2023
bb5d171
fix: remove gen code
marcomariscal Sep 26, 2023
daa2397
feat: add gen code
marcomariscal Sep 26, 2023
557f143
feat: gen code
marcomariscal Sep 26, 2023
06ec848
fix: reinstate gen code
marcomariscal Sep 26, 2023
ca0ba24
fix: don't inherit from index lookup (since we don't need to lookup a…
marcomariscal Sep 26, 2023
0ebfab0
fix: better return description
marcomariscal Sep 26, 2023
7a0e17e
fix: return value descript
marcomariscal Sep 26, 2023
c995add
fix: inherit from base tool
marcomariscal Sep 26, 2023
2f9f792
fix: remove example in prompt
marcomariscal Sep 26, 2023
be0d525
fix: prompt
marcomariscal Sep 26, 2023
543e8c6
fix: add 'display'
marcomariscal Sep 26, 2023
ca6bb86
chore: name change
marcomariscal Sep 26, 2023
2cce3af
chore: order
marcomariscal Sep 26, 2023
5387440
fix: rename
marcomariscal Sep 26, 2023
92602ae
fix: ref
marcomariscal Sep 26, 2023
3e72746
feat: template instruction update
marcomariscal Sep 26, 2023
0418c33
fix: update ref
marcomariscal Sep 26, 2023
03dd107
fix: better output description
marcomariscal Sep 27, 2023
930e928
fix: need output descript
marcomariscal Sep 27, 2023
68f2797
feat: trying to implement a CodeContainer for the generate_js_code ha…
marcomariscal Sep 27, 2023
379bebb
chore: print
marcomariscal Sep 27, 2023
6dab4ac
feat: code container
marcomariscal Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eval/eval_widgets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ display_uniswap
fetch_nfts_owned_by_address_or_domain
fetch_nfts_owned_by_user
fetch_link_suggestion
generate_js_code
51 changes: 31 additions & 20 deletions knowledge_base/widgets.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
- _name_: display_transfer
description: Transfer a token from a user's wallet to another address
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to uncomment; this is just a test to try to get the generate_js_code handler func to take priority

parameters:
properties:
address:
description: Transfer recipient address.
type: string
amount:
description: Quantity to transfer.
type: string
token:
description: Symbol of the token being transferred.
type: string
required:
- token
- amount
- address
type: object
return_value_description: ''
# - _name_: display_transfer
# description: Transfer a token from a user's wallet to another address
# parameters:
# properties:
# address:
# description: Transfer recipient address.
# type: string
# amount:
# description: Quantity to transfer.
# type: string
# token:
# description: Symbol of the token being transferred.
# type: string
# required:
# - token
# - amount
# - address
# type: object
# return_value_description: ''
- _name_: fetch_nft_buy_asset
description: Buy an NFT asset of a collection on the OpenSea marketplace, given
its network, address, and token ID. Don't use this if we don't have the collection
Expand Down Expand Up @@ -854,4 +854,15 @@
- amount
- vault
type: object
return_value_description: ''
return_value_description: ''
- _name_: generate_js_code
description: generate code for the user based on the query; this widget should always be chosen for anything relating to code within a query
parameters:
properties:
query:
description: a standalone question representing the user's intent to perform an action via code
type: string
required:
- query
type: object
return_value_description: formatted javascript code
3 changes: 2 additions & 1 deletion tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from . import index_app_info
from . import index_api_tool
from . import app_usage_guide
from . import index_link_suggestion
from . import index_link_suggestion
from . import generate_js_code
71 changes: 71 additions & 0 deletions tools/generate_js_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from pydantic import Extra

import registry
import streaming
from .base import BaseTool, BASE_TOOL_DESCRIPTION_TEMPLATE


TEMPLATE = '''You are an expert Web3 developer well versed in using JS to interact with the ecosystem, you will help the user perform actions based on their request by generating functional JS code

# INSTRUCTIONS
- Assume user wallet already connected to browser so never ask for a private key, Infura project ID, or any credentials
- Print out transaction hash if applicable
- Always use ethers.js
- Assume there is an ethers.js provider and signer available and can be provided to the function or code
- The code should return a function or a promise that can be called to perform the action
- Your final output should be a formatted JS code function with comments, which can be run on a frontend; don't include anything else for now (ie: messages that precede the code, etc.)

---
User: {question}
Assistant:'''


@registry.register_class
class GenerateJSCodeTool(BaseTool):
"""Tool for generating code to perform a user request."""

_chain: LLMChain

class Config:
"""Configuration for this pydantic object."""
extra = Extra.allow

def __init__(
self,
*args,
**kwargs
) -> None:
prompt = PromptTemplate(
input_variables=["question"],
template=TEMPLATE,
)
new_token_handler = kwargs.get('new_token_handler')
chain = streaming.get_streaming_chain(prompt, new_token_handler)
description = BASE_TOOL_DESCRIPTION_TEMPLATE.format(
tool_description="generate code based on the user query",
input_description="a standalone query where user wants to generate code to perform an action",
output_description="a JSON object with a code field, which contains a formatted JS code function with comments, which can be run on a frontend; don't include anything else for now (ie: messages that precede the code, etc.)"
)

super().__init__(
*args,
_chain=chain,
description=description,
**kwargs
)

def _run(self, query: str) -> str:
example = {
"question": query,
"stop": "User",
}
result = self._chain.run(example)
print('result in generate_js_code', result)

return result

async def _arun(self, query: str) -> str:
raise NotImplementedError(
f"{self.__class__.__name__} does not support async")
Loading
Loading