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

Contract generation #4801

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 37 additions & 1 deletion apps/remix-ide/src/app/plugins/solcoderAI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const profile = {
name: 'solcoder',
displayName: 'solcoder',
description: 'solcoder',
methods: ['code_generation', 'code_completion', "solidity_answer", "code_explaining", "code_insertion"],
methods: ['code_generation', 'code_completion', "solidity_answer", "code_explaining", "code_insertion", "contract_generation"],
events: [],
maintainedBy: 'Remix',
}
Expand Down Expand Up @@ -218,6 +218,42 @@ export class SolCoder extends Plugin {
}
}

async contract_generation(prompt): Promise<any> {
this.emit("aiInfering")
let result
try {
result = await(
await fetch(this.api_url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ "data":[
prompt, // string in 'context_code' Textbox component
"contract_generation",
false, // boolean in 'stream_result' Checkbox component
2000, // number (numeric value between 0 and 2000) in 'max_new_tokens' Slider component
0.9, // number (numeric value between 0.01 and 1) in 'temperature' Slider component
0.90, // number (numeric value between 0 and 1) in 'top_p' Slider component
50, // number (numeric value between 1 and 200) in 'top_k' Slider component
]}),
})
).json()

if ("error" in result){
return result
}
return result.data

} catch (e) {
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: `Unable to get a response ${e.message}` })
return
} finally {
this.emit("aiInferingDone")
}
}

_build_solgpt_promt(user_promt:string){
if (this.solgpt_chat_history.length === 0){
return user_promt
Expand Down
39 changes: 39 additions & 0 deletions libs/remix-ui/editor/src/lib/providers/inlineCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
props: EditorUIProps
monaco: any
completionEnabled: boolean
isGeneratingContract: boolean
constructor(props: any, monaco: any) {
this.props = props
this.monaco = monaco
this.completionEnabled = true
this.isGeneratingContract = false
}

async provideInlineCompletions(model: monacoTypes.editor.ITextModel, position: monacoTypes.Position, context: monacoTypes.languages.InlineCompletionContext, token: monacoTypes.CancellationToken): Promise<monacoTypes.languages.InlineCompletions<monacoTypes.languages.InlineCompletion>> {
Expand All @@ -44,6 +46,28 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
endColumn: getTextAtLine(model.getLineCount()).length + 1,
});

if (this.isSolidityComment(word) &&
word.includes("sol-gen") &&
this.isGeneratingContract===false &&
word.split('\n')[word.split('\n').length-1].trim()===""){

this.isGeneratingContract = true
console.log("new contract generation")
const output = await this.props.plugin.call('solcoder', 'contract_generation', word)
_paq.push(['trackEvent', 'ai', 'solcoder', 'contract_generation'])
const handleCompletionTimer = new CompletionTimer(5000, () => { this.isGeneratingContract = false });
handleCompletionTimer.start()

const item: monacoTypes.languages.InlineCompletion = {
insertText: output[0]
};

return {
items: [item],
enableForwardStability: true
}
}

if (!word.endsWith(' ') &&
!word.endsWith('.') &&
!word.endsWith('(')) {
Expand All @@ -60,6 +84,7 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
try {
const split = word.split('\n')
if (split.length < 2) return

const ask = split[split.length - 2].trimStart()
if (split[split.length - 1].trim() === '' && ask.startsWith('///')) {
// use the code generation model, only take max 1000 word as context
Expand Down Expand Up @@ -158,6 +183,20 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
}
}

isSolidityComment(text: string): boolean {
const joinedText = text.trim().replace(/\n\s*/g, ' ');

const singleLineComment = /^\/\/.*$/;
const multiLineComment = /^\/\*[\s\S]*\*\/$/;
const singleLineNatSpec = /^\/\/\/.*$/;
const multiLineNatSpec = /^\/\*\*[\s\S]*\*\/$/;

return singleLineComment.test(joinedText) ||
multiLineComment.test(joinedText) ||
singleLineNatSpec.test(joinedText) ||
multiLineNatSpec.test(joinedText);
}

process_completion(data: any) {
let clean = data.split('\n')[0].startsWith('\n') ? [data.split('\n')[0], data.split('\n')[1]].join('\n'): data.split('\n')[0]

Expand Down
Loading