-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Meru AB] Use LLM to generate instructions suggestions
- Loading branch information
1 parent
65606b1
commit df0e738
Showing
4 changed files
with
285 additions
and
41 deletions.
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
114 changes: 114 additions & 0 deletions
114
front/pages/api/w/[wId]/assistant/builder/suggestions.ts
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,114 @@ | ||
import type { | ||
BuilderSuggestionsType, | ||
WithAPIErrorReponse, | ||
} from "@dust-tt/types"; | ||
import { | ||
BuilderSuggestionsResponseBodySchema, | ||
DustProdActionRegistry, | ||
InternalPostBuilderSuggestionsRequestBodySchema, | ||
} from "@dust-tt/types"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as reporter from "io-ts-reporters"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { runAction } from "@app/lib/actions/server"; | ||
import { Authenticator, getSession } from "@app/lib/auth"; | ||
import { apiError, withLogging } from "@app/logger/withlogging"; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<WithAPIErrorReponse<BuilderSuggestionsType>> | ||
): Promise<void> { | ||
const session = await getSession(req, res); | ||
const auth = await Authenticator.fromSession( | ||
session, | ||
req.query.wId as string | ||
); | ||
const owner = auth.workspace(); | ||
if (!owner) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "workspace_not_found", | ||
message: "The workspace you're trying to modify was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "POST": | ||
if (!auth.isUser()) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "app_auth_error", | ||
message: "Only users of the workspace can use suggestions.", | ||
}, | ||
}); | ||
} | ||
|
||
const bodyValidation = | ||
InternalPostBuilderSuggestionsRequestBodySchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
const suggestionsType = bodyValidation.right.type; | ||
const suggestionsInputs = bodyValidation.right.inputs; | ||
const suggestionsResponse = await runAction( | ||
auth, | ||
`assistant-builder-${suggestionsType}-suggestions`, | ||
DustProdActionRegistry[ | ||
`assistant-builder-${suggestionsType}-suggestions` | ||
].config, | ||
[suggestionsInputs] | ||
); | ||
|
||
if (suggestionsResponse.isErr() || !suggestionsResponse.value.results) { | ||
const message = suggestionsResponse.isErr() | ||
? JSON.stringify(suggestionsResponse.error) | ||
: "No results available"; | ||
return apiError(req, res, { | ||
status_code: 500, | ||
api_error: { | ||
type: "internal_server_error", | ||
message, | ||
}, | ||
}); | ||
} | ||
const responseValidation = BuilderSuggestionsResponseBodySchema.decode( | ||
suggestionsResponse.value.results[0][0].value | ||
); | ||
if (isLeft(responseValidation)) { | ||
const pathError = reporter.formatValidationErrors( | ||
responseValidation.left | ||
); | ||
return apiError(req, res, { | ||
status_code: 500, | ||
api_error: { | ||
type: "internal_server_error", | ||
message: `Invalid response from action: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
const suggestions = responseValidation.right; | ||
return res.status(200).json(suggestions); | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, POST is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |
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
Oops, something went wrong.