-
Notifications
You must be signed in to change notification settings - Fork 113
[1/3] Add AI Assistant Service - AI Flag #2808
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
16 commits
Select commit
Hold shift + click to select a range
d85670b
all
IamtherealBrian 0243d16
miss a file
IamtherealBrian b4fcb23
fmt
IamtherealBrian 817c9b4
fix review comments
IamtherealBrian f48698a
Merge branch 'master' into minchongWu-flag-pr
xudongwu-0 09c9132
gui
IamtherealBrian fb2648f
Merge branch 'minchongWu-flag-pr' of https://github.com/Texera/texera…
IamtherealBrian 54789e9
gui
IamtherealBrian cfe231f
gui
IamtherealBrian 2a8ee30
gui
IamtherealBrian 232ada7
logger to frontend
IamtherealBrian e55837f
frontend fmt
IamtherealBrian 038be99
fix comment
IamtherealBrian 52f0eb8
fix comment
IamtherealBrian 5769815
fmt
IamtherealBrian f52d87f
error handling
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
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
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
51 changes: 51 additions & 0 deletions
51
...amber/src/main/scala/edu/uci/ics/texera/web/resource/aiassistant/AiAssistantManager.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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package edu.uci.ics.texera.web.resource.aiassistant | ||
| import edu.uci.ics.amber.engine.common.AmberConfig | ||
| import java.net.{HttpURLConnection, URL} | ||
|
|
||
| object AiAssistantManager { | ||
| private val aiAssistantConfig = AmberConfig.aiAssistantConfig.getOrElse( | ||
| throw new Exception("ai-assistant-server configuration is missing in application.conf") | ||
| ) | ||
| val assistantType: String = aiAssistantConfig.getString("assistant") | ||
| // The accountKey is the OpenAI authentication key used to authenticate API requests and obtain responses from the OpenAI service. | ||
|
|
||
| val accountKey: String = aiAssistantConfig.getString("ai-service-key") | ||
| val sharedUrl: String = aiAssistantConfig.getString("ai-service-url") | ||
|
|
||
| private def initOpenAI(): String = { | ||
| var connection: HttpURLConnection = null | ||
| try { | ||
| val url = new URL(s"${sharedUrl}/models") | ||
| connection = url.openConnection().asInstanceOf[HttpURLConnection] | ||
| connection.setRequestMethod("GET") | ||
| connection.setRequestProperty( | ||
| "Authorization", | ||
| s"Bearer ${accountKey.trim.replaceAll("^\"|\"$", "")}" | ||
| ) | ||
| val responseCode = connection.getResponseCode | ||
| if (responseCode == 200) { | ||
| "OpenAI" | ||
| } else { | ||
| "NoAiAssistant" | ||
| } | ||
| } catch { | ||
| case e: Exception => | ||
| "NoAiAssistant" | ||
| } finally { | ||
| if (connection != null) { | ||
| connection.disconnect() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val validAIAssistant: String = assistantType match { | ||
| case "none" => | ||
IamtherealBrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "NoAiAssistant" | ||
|
|
||
| case "openai" => | ||
IamtherealBrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| initOpenAI() | ||
|
|
||
| case _ => | ||
| "NoAiAssistant" | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
...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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package edu.uci.ics.texera.web.resource | ||
| import edu.uci.ics.texera.web.resource.aiassistant.AiAssistantManager | ||
| import javax.annotation.security.RolesAllowed | ||
| import javax.ws.rs._ | ||
|
|
||
| @Path("/aiassistant") | ||
| class AiAssistantResource { | ||
| final private lazy val isEnabled = AiAssistantManager.validAIAssistant | ||
| @GET | ||
| @RolesAllowed(Array("REGULAR", "ADMIN")) | ||
| @Path("/isenabled") | ||
| def isAiAssistantEnable: String = isEnabled | ||
| } |
30 changes: 30 additions & 0 deletions
30
core/gui/src/app/dashboard/service/user/ai-assistant/ai-assistant.service.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,30 @@ | ||
| import { Injectable } from "@angular/core"; | ||
| import { firstValueFrom } from "rxjs"; | ||
| import { AppSettings } from "../../../../common/app-setting"; | ||
| import { HttpClient } from "@angular/common/http"; | ||
|
|
||
| export const AI_ASSISTANT_API_BASE_URL = `${AppSettings.getApiEndpoint()}/aiassistant`; | ||
|
|
||
| @Injectable({ | ||
| providedIn: "root", | ||
| }) | ||
| export class AiAssistantService { | ||
| constructor(private http: HttpClient) {} | ||
|
|
||
| public checkAiAssistantEnabled(): Promise<string> { | ||
| const apiUrl = `${AI_ASSISTANT_API_BASE_URL}/isenabled`; | ||
| return firstValueFrom(this.http.get(apiUrl, { responseType: "text" })) | ||
| .then(response => { | ||
| const isEnabled = response !== undefined ? response : "NoAiAssistant"; | ||
| console.log( | ||
| isEnabled === "OpenAI" | ||
| ? "AI Assistant successfully started" | ||
| : "No AI Assistant or OpenAI authentication key error" | ||
| ); | ||
| return isEnabled; | ||
| }) | ||
| .catch(() => { | ||
| return "NoAiAssistant"; | ||
| }); | ||
| } | ||
| } |
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.