-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Remixai chat #5241
Remixai chat #5241
Changes from 5 commits
edfb3bd
1e68ae0
118d66e
abeac7d
4c843e9
ad70e5e
39e89ab
52408f3
1348d1f
2695aaf
1743146
8c11a35
2b0f7a2
0d0dbd1
5b69872
b6eeacf
917f562
9d782ea
d2d68ce
bfa5eda
6e5ed32
707699b
b237be8
e89e938
dd061e5
2d4fbb5
8968ce6
51c4fbd
f964671
e33392f
5f0dc4d
0f0809e
d52765b
a18460d
09db2ad
f5e116e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
import * as packageJson from '../../../../../package.json' | ||
import { ViewPlugin } from '@remixproject/engine-web' | ||
import { Plugin } from '@remixproject/engine'; | ||
import { RemixAITab } from '@remix-ui/remix-ai' | ||
import React from 'react'; | ||
import { ICompletions, IModel, RemoteInferencer, IRemoteModel } from '@remix/remix-ai-core'; | ||
import { RemixAITab, ChatApi } from '@remix-ui/remix-ai' | ||
import React, { useCallback } from 'react'; | ||
import { ICompletions, IModel, RemoteInferencer, IRemoteModel, IParams, GenerationParams, HandleStreamResponse } from '@remix/remix-ai-core'; | ||
|
||
type chatRequestBufferT<T> = { | ||
[key in keyof T]: T[key] | ||
} | ||
|
||
const profile = { | ||
name: 'remixAI', | ||
displayName: 'Remix AI', | ||
methods: ['code_generation', 'code_completion', | ||
"solidity_answer", "code_explaining", | ||
"code_insertion", "error_explaining", | ||
"initialize"], | ||
"initialize", 'chatPipe', 'ProcessChatRequestBuffer', 'isChatRequestPending'], | ||
events: [], | ||
icon: 'assets/img/remix-logo-blue.png', | ||
description: 'RemixAI provides AI services to Remix IDE.', | ||
kind: '', | ||
// location: 'sidePanel', | ||
location: 'sidePanel', | ||
documentation: 'https://remix-ide.readthedocs.io/en/latest/remixai.html', | ||
version: packageJson.version, | ||
maintainedBy: 'Remix' | ||
} | ||
|
||
export class RemixAIPlugin extends Plugin { | ||
export class RemixAIPlugin extends ViewPlugin { | ||
isOnDesktop:boolean = false | ||
aiIsActivated:boolean = false | ||
readonly remixDesktopPluginName = 'remixAID' | ||
remoteInferencer:RemoteInferencer = null | ||
isInferencing: boolean = false | ||
chatRequestBuffer: chatRequestBufferT<any> = null | ||
|
||
constructor(inDesktop:boolean) { | ||
super(profile) | ||
|
@@ -68,7 +73,6 @@ export class RemixAIPlugin extends Plugin { | |
} | ||
|
||
} else { | ||
// on browser | ||
this.remoteInferencer = new RemoteInferencer(remoteModel?.apiUrl, remoteModel?.completionUrl) | ||
this.remoteInferencer.event.on('onInference', () => { | ||
this.isInferencing = true | ||
|
@@ -103,61 +107,52 @@ export class RemixAIPlugin extends Plugin { | |
} | ||
} | ||
|
||
async solidity_answer(prompt: string): Promise<any> { | ||
async solidity_answer(prompt: string, params: IParams=GenerationParams): Promise<any> { | ||
if (this.isInferencing) { | ||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI is already busy!" }) | ||
return | ||
} | ||
|
||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: `\n\nWaiting for RemixAI answer...` }) | ||
|
||
let result | ||
if (this.isOnDesktop) { | ||
result = await this.call(this.remixDesktopPluginName, 'solidity_answer', prompt) | ||
} else { | ||
result = await this.remoteInferencer.solidity_answer(prompt) | ||
} | ||
if (result) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
// this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI Done" }) | ||
if (result && params.terminal_output) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
return result | ||
} | ||
|
||
async code_explaining(prompt: string): Promise<any> { | ||
async code_explaining(prompt: string, context: string, params: IParams=GenerationParams): Promise<any> { | ||
if (this.isInferencing) { | ||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI is already busy!" }) | ||
return | ||
} | ||
|
||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: `\n\nWaiting for RemixAI answer...` }) | ||
|
||
|
||
let result | ||
if (this.isOnDesktop) { | ||
result = await this.call(this.remixDesktopPluginName, 'code_explaining', prompt) | ||
result = await this.call(this.remixDesktopPluginName, 'code_explaining', prompt, context, params) | ||
|
||
} else { | ||
result = await this.remoteInferencer.code_explaining(prompt) | ||
result = await this.remoteInferencer.code_explaining(prompt, context, params) | ||
} | ||
if (result) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
// this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI Done" }) | ||
if (result && params.terminal_output) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
return result | ||
} | ||
|
||
async error_explaining(prompt: string): Promise<any> { | ||
async error_explaining(prompt: string, context: string="", params: IParams=GenerationParams): Promise<any> { | ||
if (this.isInferencing) { | ||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI is already busy!" }) | ||
return | ||
} | ||
|
||
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: `\n\nWaiting for RemixAI answer...` }) | ||
|
||
let result | ||
if (this.isOnDesktop) { | ||
result = await this.call(this.remixDesktopPluginName, 'error_explaining', prompt) | ||
} else { | ||
result = await this.remoteInferencer.error_explaining(prompt) | ||
result = await this.remoteInferencer.error_explaining(prompt, params) | ||
} | ||
if (result) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
// this.call('terminal', 'log', { type: 'aitypewriterwarning', value: "RemixAI Done" }) | ||
if (result && params.terminal_output) this.call('terminal', 'log', { type: 'aitypewriterwarning', value: result }) | ||
return result | ||
} | ||
|
||
|
@@ -169,9 +164,46 @@ export class RemixAIPlugin extends Plugin { | |
} | ||
} | ||
|
||
// render() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need those? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This goes out from this PR |
||
// return ( | ||
// <RemixAITab plugin={this}></RemixAITab> | ||
// ) | ||
// } | ||
chatPipe(fn, prompt: string, context?: string, pipeMessage?: string){ | ||
if (this.chatRequestBuffer == null){ | ||
this.chatRequestBuffer = { | ||
fn_name: fn, | ||
prompt: prompt, | ||
context: context | ||
} | ||
console.log('pipe message', pipeMessage) | ||
if (pipeMessage) ChatApi.composer.send(pipeMessage) | ||
else { | ||
if (fn === "code_explaining") ChatApi.composer.send("Explain the current code") | ||
STetsing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else if (fn === "error_explaining") ChatApi.composer.send("Explain the error") | ||
else if (fn === "solidity_answer") ChatApi.composer.send("Answer the following question") | ||
else console.log("chatRequestBuffer is not empty. First process the last request.") | ||
} | ||
} | ||
else{ | ||
console.log("chatRequestBuffer is not empty. First process the last request.") | ||
} | ||
} | ||
|
||
|
||
async ProcessChatRequestBuffer(params:IParams=GenerationParams){ | ||
if (this.chatRequestBuffer != null){ | ||
const result = this[this.chatRequestBuffer.fn_name](this.chatRequestBuffer.prompt, this.chatRequestBuffer.context, params) | ||
this.chatRequestBuffer = null | ||
return result | ||
} | ||
else{ | ||
console.log("chatRequestBuffer is empty.") | ||
return "" | ||
} | ||
} | ||
isChatRequestPending(){ | ||
return this.chatRequestBuffer != null | ||
} | ||
|
||
render() { | ||
return ( | ||
<RemixAITab plugin={this}></RemixAITab> | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ChatHistory } from '../prompts/chat'; | ||
import { JsonStreamParser} from '../types/types' | ||
|
||
export const HandleSimpleResponse = async (response, | ||
cb?: (streamText: string) => void) => { | ||
let resultText = '' | ||
const parser = new JsonStreamParser(); | ||
|
||
const chunk = parser.safeJsonParse<{ generatedText: string; isGenerating: boolean }>(response); | ||
for (const parsedData of chunk) { | ||
if (parsedData.isGenerating) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are not using right setup for linter |
||
resultText += parsedData.generatedText | ||
cb(parsedData.generatedText) | ||
} else { | ||
resultText += parsedData.generatedText | ||
cb(parsedData.generatedText) | ||
} | ||
} | ||
} | ||
|
||
export const HandleStreamResponse = async (streamResponse, | ||
cb?: (streamText: string) => void, | ||
done_cb?: (result: string) => void) => { | ||
try { | ||
let resultText = '' | ||
const parser = new JsonStreamParser(); | ||
const reader = streamResponse.body!.getReader(); | ||
const decoder = new TextDecoder(); | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
|
||
try { | ||
const chunk = parser.safeJsonParse<{ generatedText: string; isGenerating: boolean }>(decoder.decode(value, { stream: true })); | ||
for (const parsedData of chunk) { | ||
if (parsedData.isGenerating) { | ||
resultText += parsedData.generatedText | ||
cb(parsedData.generatedText) | ||
} else { | ||
resultText += parsedData.generatedText | ||
cb(parsedData.generatedText) | ||
} | ||
} | ||
} | ||
catch (error) { | ||
console.error('Error parsing JSON:', error); | ||
} | ||
} | ||
if (done_cb) { | ||
done_cb(resultText) | ||
} | ||
} | ||
catch (error) { | ||
console.error('Error parsing JSON:', error); | ||
} | ||
} | ||
|
||
export const UpdtateChatHistory = (userPromptprompt: string, AIAnswer: string) => { | ||
ChatHistory.pushHistory(userPromptprompt, AIAnswer) | ||
} |
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.
could you remove all the comments?