-
Notifications
You must be signed in to change notification settings - Fork 113
[2/3] Add AI Assistant Service - add the “Add Type Annotation" Python UDF action #2811
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
34f6878
all
IamtherealBrian e0f51d3
amber
IamtherealBrian 86b3663
fmt
IamtherealBrian bbcc2a3
frontend logger
IamtherealBrian 980c6b7
logger to frontend
IamtherealBrian d5a3ded
yarn fmt
IamtherealBrian dca5a22
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 9c316b9
fmt
IamtherealBrian c04d498
add action
IamtherealBrian 081113f
debug
IamtherealBrian 37f9573
debug
IamtherealBrian 388325a
fmt
IamtherealBrian b788554
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian acf6a05
change "Ai" to "AI"
IamtherealBrian 4e0b697
move prompt from frontend to backend
IamtherealBrian 240b068
Start pyright language server with ts file and deprecate the mjs file…
IamtherealBrian 7bf2483
resolve conflict?
IamtherealBrian ed156d7
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 844e942
resolve conflict
IamtherealBrian 109d173
fix comments
IamtherealBrian b51bc81
fmt
IamtherealBrian dfa885c
fix comments.
IamtherealBrian f5e7c8f
fmt
IamtherealBrian c39860a
remove import typing modules
IamtherealBrian 6200909
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 9c997f2
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 1219868
fix part of the comments
IamtherealBrian eefc603
fix part of the comments
IamtherealBrian 2cde080
add annotation-suggestion.component
IamtherealBrian ee50fd7
remove async and change promise to observable
IamtherealBrian a6e9d1d
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 0b108c5
fmt gui
IamtherealBrian c5759ec
fix gui comments
IamtherealBrian 73c4fcc
Merge branch 'master' into minchongWu-aiFeatures1-pr
IamtherealBrian 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 hidden or 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
80 changes: 78 additions & 2 deletions
80
...mber/src/main/scala/edu/uci/ics/texera/web/resource/aiassistant/AiAssistantResource.scala
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,89 @@ | ||
| package edu.uci.ics.texera.web.resource | ||
| import edu.uci.ics.texera.web.auth.SessionUser | ||
| import edu.uci.ics.texera.web.resource.aiassistant.AiAssistantManager | ||
| import io.dropwizard.auth.Auth | ||
| import javax.annotation.security.RolesAllowed | ||
| import javax.ws.rs._ | ||
| import javax.ws.rs.core.Response | ||
| import javax.ws.rs.Consumes | ||
| import javax.ws.rs.core.MediaType | ||
| import play.api.libs.json.Json | ||
| import kong.unirest.Unirest | ||
|
|
||
| case class AIAssistantRequest(code: String, lineNumber: Int, allcode: String) | ||
|
|
||
| @Path("/aiassistant") | ||
| class AiAssistantResource { | ||
| class AIAssistantResource { | ||
| final private lazy val isEnabled = AiAssistantManager.validAIAssistant | ||
| @GET | ||
| @RolesAllowed(Array("REGULAR", "ADMIN")) | ||
| @Path("/isenabled") | ||
| def isAiAssistantEnable: String = isEnabled | ||
| def isAIAssistantEnable: String = isEnabled | ||
|
|
||
| /** | ||
| * To get the type annotation suggestion from OpenAI | ||
| */ | ||
| @POST | ||
| @RolesAllowed(Array("REGULAR", "ADMIN")) | ||
| @Path("/annotationresult") | ||
| @Consumes(Array(MediaType.APPLICATION_JSON)) | ||
| def getAnnotation( | ||
| request: AIAssistantRequest, | ||
| @Auth user: SessionUser | ||
| ): Response = { | ||
| val finalPrompt = generatePrompt(request.code, request.lineNumber, request.allcode) | ||
| val requestBodyJson = Json.obj( | ||
| "model" -> "gpt-4", | ||
| "messages" -> Json.arr( | ||
| Json.obj( | ||
| "role" -> "user", | ||
| "content" -> finalPrompt | ||
| ) | ||
| ), | ||
| "max_tokens" -> 15 | ||
| ) | ||
|
|
||
| val response = Unirest | ||
| .post(s"${AiAssistantManager.sharedUrl}/chat/completions") | ||
| .header("Authorization", s"Bearer ${AiAssistantManager.accountKey}") | ||
| .header("Content-Type", "application/json") | ||
| .body(Json.stringify(requestBodyJson)) | ||
| .asString() | ||
| if (response.getStatus >= 400) { | ||
| throw new RuntimeException(s"getAnnotation error: ${response.getStatus}: ${response.getBody}") | ||
| } | ||
| Response.status(response.getStatus).entity(response.getBody).build() | ||
| } | ||
|
|
||
| // Helper function to get the type annotation | ||
| def generatePrompt(code: String, lineNumber: Int, allcode: String): String = { | ||
| s""" | ||
| |Your task is to analyze the given Python code and provide only the type annotation as stated in the instructions. | ||
| |Instructions: | ||
| |- The provided code will only be one of the 2 situations below: | ||
| |- First situation: The input is not start with "def". If the provided code only contains variable, output the result in the format ":type". | ||
| |- Second situation: The input is start with "def". If the provided code starts with "def" (a longer line than just a variable, indicative of a function or method), output the result in the format " -> type". | ||
| |- The type should only be one word, such as "str", "int", etc. | ||
| |Examples: | ||
| |- First situation: | ||
| | - Provided code is "name", then the output may be : str | ||
| | - Provided code is "age", then the output may be : int | ||
| | - Provided code is "data", then the output may be : Tuple[int, str] | ||
| | - Provided code is "new_user", then the output may be : User | ||
| | - A special case: provided code is "self" and the context is something like "def __init__(self, username :str , age :int)", if the user requires the type annotation for the first parameter "self", then you should generate nothing. | ||
| |- Second situation: (actual output depends on the complete code content) | ||
| | - Provided code is "process_data(data: List[Tuple[int, str]], config: Dict[str, Union[int, str]])", then the output may be -> Optional[str] | ||
| | - Provided code is "def add(a: int, b: int)", then the output may be -> int | ||
| |Counterexamples: | ||
| | - Provided code is "def __init__(self, username: str, age: int)" and you generate the result: | ||
| | The result is The provided code is "def __init__(self, username: str, age: int)", so it fits the second situation, which means the result should be in " -> type" format. However, the __init__ method in Python doesn't return anything or in other words, it implicitly returns None. Hence the correct type hint would be: -> None. | ||
| |Details: | ||
| |- Provided code: $code | ||
| |- Line number of the provided code in the complete code context: $lineNumber | ||
| |- Complete code context: $allcode | ||
| |Important: (you must follow!!) | ||
| |- For the first situation: you must return strictly according to the format ": type", without adding any extra characters. No need for an explanation, just the result : type is enough! | ||
| |- For the second situation: you return strictly according to the format " -> type", without adding any extra characters. No need for an explanation, just the result -> type is enough! | ||
| """.stripMargin | ||
| } | ||
| } | ||
This file contains hidden or 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
30 changes: 0 additions & 30 deletions
30
core/gui/src/app/dashboard/service/user/ai-assistant/ai-assistant.service.ts
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
core/gui/src/app/workspace/component/code-editor-dialog/annotation-suggestion.component.html
This file contains hidden or 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,18 @@ | ||
| <div | ||
| class="annotation-suggestion" | ||
| [style.top.px]="top" | ||
| [style.left.px]="left"> | ||
| <p>Do you agree with the type annotation suggestion?</p> | ||
| <pre>Adding annotation for code: {{ code }}</pre> | ||
| <p>Given suggestion: <strong>{{ suggestion }}</strong></p> | ||
| <button | ||
| class="accept-button" | ||
| (click)="onAccept()"> | ||
| Accept | ||
| </button> | ||
| <button | ||
| class="decline-button" | ||
| (click)="onDecline()"> | ||
| Decline | ||
| </button> | ||
| </div> |
39 changes: 39 additions & 0 deletions
39
core/gui/src/app/workspace/component/code-editor-dialog/annotation-suggestion.component.scss
This file contains hidden or 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,39 @@ | ||
| .annotation-suggestion { | ||
| position: absolute; | ||
| background: #222; | ||
| color: #fff; | ||
| padding: 20px; | ||
| border-radius: 8px; | ||
| box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); | ||
| z-index: 1000; | ||
| } | ||
|
|
||
| .annotation-suggestion button { | ||
| margin-right: 10px; | ||
| } | ||
|
|
||
| .annotation-suggestion button.accept-button { | ||
| background-color: #28a745; | ||
| color: #000; | ||
| border: none; | ||
| padding: 10px 20px; | ||
| border-radius: 5px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .annotation-suggestion button.accept-button:hover { | ||
| background-color: #218838; | ||
| } | ||
|
|
||
| .annotation-suggestion button.decline-button { | ||
| background-color: #dc3545; | ||
| color: #000; | ||
| border: none; | ||
| padding: 10px 20px; | ||
| border-radius: 5px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .annotation-suggestion button.decline-button:hover { | ||
| background-color: #c82333; | ||
| } |
23 changes: 23 additions & 0 deletions
23
core/gui/src/app/workspace/component/code-editor-dialog/annotation-suggestion.component.ts
This file contains hidden or 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,23 @@ | ||
| import { Component, Input, Output, EventEmitter } from "@angular/core"; | ||
|
|
||
| @Component({ | ||
| selector: "texera-annotation-suggestion", | ||
| templateUrl: "./annotation-suggestion.component.html", | ||
| styleUrls: ["./annotation-suggestion.component.scss"], | ||
| }) | ||
| export class AnnotationSuggestionComponent { | ||
| @Input() code: string = ""; | ||
| @Input() suggestion: string = ""; | ||
| @Input() top: number = 0; | ||
| @Input() left: number = 0; | ||
| @Output() accept = new EventEmitter<void>(); | ||
| @Output() decline = new EventEmitter<void>(); | ||
|
|
||
| onAccept() { | ||
| this.accept.emit(); | ||
| } | ||
|
|
||
| onDecline() { | ||
| this.decline.emit(); | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.