-
Notifications
You must be signed in to change notification settings - Fork 26
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
Expose a general function for agent execution #268
Merged
ruanyl
merged 7 commits into
opensearch-project:main
from
ruanyl:expose-a-general-function-for-agent-execution
Sep 5, 2024
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eb8cf13
feat: add assistant client to public and server
ruanyl aa3e483
tweaks
ruanyl 493d293
fix path
ruanyl 6d7dd87
update changelog
ruanyl 15bdf4d
feat: support both execute agent by name and by id
ruanyl b3d4b22
fix(public): support execute agent by name or id
ruanyl 003da1b
Merge branch 'main' into expose-a-general-function-for-agent-execution
gaobinlong 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,25 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { API_BASE } from '../../common/constants/llm'; | ||
import { HttpSetup } from '../../../../src/core/public'; | ||
|
||
interface Options { | ||
dataSourceId?: string; | ||
} | ||
|
||
export class AssistantClient { | ||
constructor(private http: HttpSetup) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
executeAgent = (agentName: string, parameters: Record<string, any>, options?: Options) => { | ||
return this.http.fetch({ | ||
method: 'POST', | ||
path: `${API_BASE}/agents/${agentName}/_execute`, | ||
body: JSON.stringify(parameters), | ||
query: { dataSourceId: options?.dataSourceId }, | ||
}); | ||
}; | ||
} |
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,26 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { HttpSetup } from '../../../../src/core/public'; | ||
import { AssistantClient } from './assistant_client'; | ||
|
||
export interface AssistantServiceStart { | ||
client: AssistantClient; | ||
} | ||
|
||
export class AssistantService { | ||
constructor() {} | ||
|
||
setup() {} | ||
|
||
start(http: HttpSetup): AssistantServiceStart { | ||
const assistantClient = new AssistantClient(http); | ||
return { | ||
client: assistantClient, | ||
}; | ||
} | ||
|
||
stop() {} | ||
} |
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,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema } from '@osd/config-schema'; | ||
import { IRouter } from '../../../../src/core/server'; | ||
import { AGENT_API } from '../../common/constants/llm'; | ||
import { AssistantServiceSetup } from '../services/assistant_service'; | ||
|
||
export function registerAgentRoutes(router: IRouter, assistantService: AssistantServiceSetup) { | ||
router.post( | ||
{ | ||
path: AGENT_API.EXECUTE, | ||
validate: { | ||
body: schema.any(), | ||
query: schema.object({ | ||
dataSourceId: schema.maybe(schema.string()), | ||
}), | ||
params: schema.object({ | ||
agentId: schema.string(), | ||
}), | ||
}, | ||
}, | ||
router.handleLegacyErrors(async (context, req, res) => { | ||
try { | ||
const assistantClient = assistantService.getScopedClient(req, context); | ||
const response = await assistantClient.executeAgent(req.params.agentId, req.body); | ||
return res.ok({ body: response }); | ||
} catch (e) { | ||
return res.internalError(); | ||
} | ||
}) | ||
); | ||
} |
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,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ApiResponse } from '@opensearch-project/opensearch'; | ||
|
||
import { | ||
OpenSearchClient, | ||
OpenSearchDashboardsRequest, | ||
RequestHandlerContext, | ||
} from '../../../../src/core/server'; | ||
import { ML_COMMONS_BASE_API } from '../utils/constants'; | ||
import { getAgent } from '../routes/get_agent'; | ||
|
||
interface AgentExecuteResponse { | ||
inference_results: Array<{ | ||
output: Array<{ result: string }>; | ||
}>; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const isDataSourceQuery = (query: any): query is { dataSourceId: string } => { | ||
if ('dataSourceId' in query && query.dataSourceId) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
export class AssistantClient { | ||
constructor( | ||
private request: OpenSearchDashboardsRequest, | ||
private context: RequestHandlerContext & { | ||
dataSource?: { | ||
opensearch: { | ||
getClient: (dataSourceId: string) => Promise<OpenSearchClient>; | ||
}; | ||
}; | ||
} | ||
) {} | ||
|
||
executeAgent = async ( | ||
agentName: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
parameters: Record<string, any> | ||
): Promise<ApiResponse<AgentExecuteResponse>> => { | ||
let client = this.context.core.opensearch.client.asCurrentUser; | ||
if (isDataSourceQuery(this.request.query) && this.context.dataSource) { | ||
client = await this.context.dataSource.opensearch.getClient(this.request.query.dataSourceId); | ||
} | ||
|
||
const agentId = await getAgent(agentName, client.transport); | ||
const response = await client.transport.request({ | ||
method: 'POST', | ||
path: `${ML_COMMONS_BASE_API}/agents/${agentId}/_execute`, | ||
body: { | ||
parameters, | ||
}, | ||
}); | ||
|
||
return response as ApiResponse<AgentExecuteResponse>; | ||
}; | ||
} |
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.
do we want to support executeAgent with agent id?
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.
I also considered this, but we don't have use case at the moment which requires to execute agent with id directly, so I decided to not add it for now, we can add it when it's needed.
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.
+1
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.
Knowledge base needs to execute by agent id directly
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.
@qianheng-aws may have a requirement to execute agent id directly
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.
I see, let me add that
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.
@Hailong-am @qianheng-aws Updated, could you take another look?
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.
looks good to me.
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.
LGTM