-
Notifications
You must be signed in to change notification settings - Fork 17
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
marcomariscal
wants to merge
30
commits into
dev
Choose a base branch
from
feat/codegen
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 c49a1e6
fix: spelling
marcomariscal e31cd0a
feat: config for generate_code
marcomariscal d237a5a
chore: index bump
marcomariscal 5390e55
feat: generate code tool into index_widget
marcomariscal d21799d
feat: generate_code handler (wip)
marcomariscal 0849d6d
fix: try to prioritize gen_code when code context
marcomariscal c4c8c78
fix: no display
marcomariscal bb5d171
fix: remove gen code
marcomariscal daa2397
feat: add gen code
marcomariscal 557f143
feat: gen code
marcomariscal 06ec848
fix: reinstate gen code
marcomariscal ca0ba24
fix: don't inherit from index lookup (since we don't need to lookup a…
marcomariscal 0ebfab0
fix: better return description
marcomariscal 7a0e17e
fix: return value descript
marcomariscal c995add
fix: inherit from base tool
marcomariscal 2f9f792
fix: remove example in prompt
marcomariscal be0d525
fix: prompt
marcomariscal 543e8c6
fix: add 'display'
marcomariscal ca6bb86
chore: name change
marcomariscal 2cce3af
chore: order
marcomariscal 5387440
fix: rename
marcomariscal 92602ae
fix: ref
marcomariscal 3e72746
feat: template instruction update
marcomariscal 0418c33
fix: update ref
marcomariscal 03dd107
fix: better output description
marcomariscal 930e928
fix: need output descript
marcomariscal 68f2797
feat: trying to implement a CodeContainer for the generate_js_code ha…
marcomariscal 379bebb
chore: print
marcomariscal 6dab4ac
feat: code container
marcomariscal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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