Skip to content

Commit

Permalink
Merge pull request #31 from animo/feat/ai-route
Browse files Browse the repository at this point in the history
feat: added ai analyzing route
  • Loading branch information
janrtvld authored Nov 26, 2024
2 parents f3c4881 + 7749dee commit 3df2d9f
Show file tree
Hide file tree
Showing 8 changed files with 1,553 additions and 826 deletions.
3 changes: 2 additions & 1 deletion agent/.env.development
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
AGENT_WALLET_KEY=secret-wallet-key
ED25519_SEED=5473a3e4c5ae3fd5fb3ad089563596e3
P256_SEED=e5f18b10cd15cdb76818bc6ae8b71eb475e6eac76875ed085d3962239bbcf42f
P256_SEED=e5f18b10cd15cdb76818bc6ae8b71eb475e6eac76875ed085d3962239bbcf42f
ANTHROPIC_API_KEY=
3 changes: 2 additions & 1 deletion agent/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ DID_INDY_INDICIO_TESTNET_PUBLIC_DID_SEED=2543786a945a27258087ccfe95ff62df
CHEQD_TESTNET_COSMOS_PAYER_SEED=robust across amount corn curve panther opera wish toe ring bleak empower wreck party abstract glad average muffin picnic jar squeeze annual long aunt
ED25519_SEED=5473a3e4c5ae3fd5fb3ad089563596e3
P256_SEED=e5f18b10cd15cdb76818bc6ae8b71eb475e6eac76875ed085d3962239bbcf42f
X509_CERTIFICATE=MIIBGDCBv6ADAgECAhBaok2RHkURQndkcWUiNtn/MAoGCCqGSM49BAMCMAAwIBcNNzAwMTAxMDAwMDAwWhgPMjI4NjExMjAxNzQ2NDBaMAAwOTATBgcqhkjOPQIBBggqhkjOPQMBBwMiAALcD1XzKepFxWMAOqV+ln1fybBt7DRO5CV0f9A6mRp2xaM5MDcwNQYDVR0RBC4wLIYqaHR0cHM6Ly82ZjY4LTEwOS0zNy0xNDctMTI0Lm5ncm9rLWZyZWUuYXBwMAoGCCqGSM49BAMCA0gAMEUCIFH0dqgKmKnF3tXGddd4yfgr5zjXqcmzUUqXvJDWwzyKAiEA0ix1qrMYdQS+mCYW/C+/xJ+2Hbns3LT+vE9gwvE/VKM=
X509_CERTIFICATE=MIIBGDCBv6ADAgECAhBaok2RHkURQndkcWUiNtn/MAoGCCqGSM49BAMCMAAwIBcNNzAwMTAxMDAwMDAwWhgPMjI4NjExMjAxNzQ2NDBaMAAwOTATBgcqhkjOPQIBBggqhkjOPQMBBwMiAALcD1XzKepFxWMAOqV+ln1fybBt7DRO5CV0f9A6mRp2xaM5MDcwNQYDVR0RBC4wLIYqaHR0cHM6Ly82ZjY4LTEwOS0zNy0xNDctMTI0Lm5ncm9rLWZyZWUuYXBwMAoGCCqGSM49BAMCA0gAMEUCIFH0dqgKmKnF3tXGddd4yfgr5zjXqcmzUUqXvJDWwzyKAiEA0ix1qrMYdQS+mCYW/C+/xJ+2Hbns3LT+vE9gwvE/VKM=
ANTHROPIC_API_KEY=
4 changes: 3 additions & 1 deletion agent/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "agent",
"dependencies": {
"@ai-sdk/anthropic": "^1.0.2",
"@animo-id/mdoc": "^0.2.39",
"@credo-ts/askar": "https://gitpkg.vercel.app/animo/aries-framework-javascript/packages/askar?funke",
"@credo-ts/core": "https://gitpkg.vercel.app/animo/aries-framework-javascript/packages/core?funke",
"@credo-ts/node": "https://gitpkg.vercel.app/animo/aries-framework-javascript/packages/node?funke",
"@credo-ts/openid4vc": "https://gitpkg.vercel.app/animo/aries-framework-javascript/packages/openid4vc?funke",
"@hyperledger/aries-askar-nodejs": "^0.2.3",
"@animo-id/mdoc": "^0.2.39",
"ai": "^4.0.4",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
Expand Down
101 changes: 101 additions & 0 deletions agent/src/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { anthropic } from '@ai-sdk/anthropic'
import { generateObject } from 'ai'
import { z } from 'zod'

export const zValidateVerificationRequestSchema = z.object({
verifier: z.object({
name: z.string(),
domain: z.string(),
}),
name: z.string(),
purpose: z.string(),
cards: z.array(
z.object({
name: z.string(),
subtitle: z.string(),
requestedAttributes: z.array(z.string()),
})
),
})

const zResponseSchema = z.object({
reason: z.string(),
validRequest: z.enum(['yes', 'no', 'could_not_determine']),
})

export const validateVerificationRequest = async ({
verifier,
name,
purpose,
cards,
}: z.infer<typeof zValidateVerificationRequestSchema>) => {
const rc = cards
.map(
(credential) =>
`${credential.name} - ${credential.subtitle}. Requested attributes: ${credential.requestedAttributes.join(', ')}`
)
.join('\n')

// Can be improved by adding a reasoning step, but that makes it quite slow
const { object } = await generateObject({
model: anthropic('claude-3-5-sonnet-20240620'),
schema: zResponseSchema,
prompt: BASE_PROMPT(verifier, name, purpose, rc),
}).catch(() => ({ object: { validRequest: 'could_not_determine', reason: 'AI request failed' } }))

return object
}

const BASE_PROMPT = (
verifier: { name: string; domain: string },
requestName: string,
requestPurpose: string,
requestedCards: string
) => `
You are an AI assistant tasked with analyzing data verification requests to identify potential overasking of information. Your goal is to determine if the requested data is appropriate and necessary for the stated purpose of the request.
You will be provided with the following information:
1. Verifier Name:
<verifier_name>
${verifier.name}
</verifier_name>
2. Verifier Domain:
<verifier_domain>
${verifier.domain}
</verifier_domain>
3. Request Name:
<request_name>
${requestName}
</request_name>
4. Request Purpose:
<request_purpose>
${requestPurpose}
</request_purpose>
5. Requested Cards:
<requested_cards>
${requestedCards}
</requested_cards>
Analyze the request by following these steps:
1. Carefully review the verifier and the request name and purpose.
2. Examine each requested card, including its name, subtitle, and attributes.
3. For each piece of requested information, consider whether it is necessary and appropriate for the stated purpose.
4. Identify any instances of overasking, where the requested information exceeds what is reasonably required for the purpose.
Guidelines for identifying overasking:
- Consider the sensitivity of the requested information.
- Evaluate whether less sensitive alternatives could serve the same purpose.
- Assess if the quantity of requested information is excessive for the stated purpose.
- Determine if the information request aligns with common practices for similar purposes.
- If you cannot determine whether the request is overasking, respond with "could_not_determine".
Briefly summarize whether the request appears appropriate or if there is evidence of overasking. Remember to be thorough in your analysis and provide clear, concise explanations for your assessments. If you find no evidence of overasking, state this clearly and explain why the requested information appears appropriate for the purpose.
Keep your response concise and to the point.
`
22 changes: 22 additions & 0 deletions agent/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { OpenId4VcVerificationSessionState } from '@credo-ts/openid4vc'
import express, { type NextFunction, type Request, type Response } from 'express'
import z from 'zod'
import { agent } from './agent'
import { validateVerificationRequest, zValidateVerificationRequestSchema } from './ai'
import { AGENT_HOST } from './constants'
import { getIssuerIdForCredentialConfigurationId } from './issuer'
import { issuers, issuersCredentialsData } from './issuers'
Expand Down Expand Up @@ -465,3 +466,24 @@ apiRouter.use((error: Error, _request: Request, response: Response, _next: NextF
error: error.message,
})
})

apiRouter.post('/validate-verification-request', async (request: Request, response: Response) => {
try {
const validateVerificationRequestBody = zValidateVerificationRequestSchema.parse(request.body)
const result = await validateVerificationRequest(validateVerificationRequestBody)
return response.json(result)
} catch (error) {
if (error instanceof z.ZodError) {
return response.status(400).json({
error: 'Invalid request body',
details: error.errors,
})
}

console.error('Error validating verification request:', error)
return response.status(500).json({
error: 'Internal server error during verification validation',
message: error instanceof Error ? error.message : 'Unknown error',
})
}
})
Binary file modified app/public/assets/verifiers/animo/verifier.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
P256_SEED: ${P256_SEED}
X509_CERTIFICATE: ${X509_CERTIFICATE}
AGENT_HOST: "https://funke.animo.id"
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}

networks:
- traefik
Expand Down
Loading

0 comments on commit 3df2d9f

Please sign in to comment.