Skip to content

Commit 8bbdafb

Browse files
authored
Merge pull request elizaOS#1673 from RonTuretzky/plugin-opacity
Opacity Verifiable Interference zkTLS Plugin
2 parents 8a4b42b + 25cd8ba commit 8bbdafb

File tree

11 files changed

+424
-8
lines changed

11 files changed

+424
-8
lines changed

.env.example

+9
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,12 @@ OPEN_WEATHER_API_KEY= # OpenWeather API key
436436
# Allora
437437
ALLORA_API_KEY= # Allora API key, format: UP-f8db7d6558ab432ca0d92716
438438
ALLORA_CHAIN_SLUG= # must be one of mainnet, testnet. If not specified, it will use testnet by default
439+
440+
# Opacity zkTLS
441+
OPACITY_TEAM_ID=f309ac8ae8a9a14a7e62cd1a521b1c5f
442+
OPACITY_CLOUDFLARE_NAME=eigen-test
443+
OPACITY_PROVER_URL=https://opacity-ai-zktls-demo.vercel.app
444+
445+
# Verifiable Inference Configuration
446+
VERIFIABLE_INFERENCE_ENABLED=false # Set to false to disable verifiable inference
447+
VERIFIABLE_INFERENCE_PROVIDER=opacity # Options: opacity

agent/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"@elizaos/plugin-obsidian": "workspace:*",
7979
"@elizaos/plugin-arthera": "workspace:*",
8080
"@elizaos/plugin-allora": "workspace:*",
81+
"@elizaos/plugin-opacity": "workspace:*",
8182
"readline": "1.3.0",
8283
"ws": "8.18.0",
8384
"yargs": "17.7.2"

agent/src/index.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import net from "net";
8686
import path from "path";
8787
import { fileURLToPath } from "url";
8888
import yargs from "yargs";
89+
import { OpacityAdapter } from "@elizaos/plugin-opacity";
8990

9091
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
9192
const __dirname = path.dirname(__filename); // get the name of the directory
@@ -554,6 +555,28 @@ export async function createAgent(
554555
// });
555556
// elizaLogger.log("Verifiable inference adapter initialized");
556557
// }
558+
// Initialize Opacity adapter if environment variables are present
559+
let verifiableInferenceAdapter;
560+
if (
561+
process.env.OPACITY_TEAM_ID &&
562+
process.env.OPACITY_CLOUDFLARE_NAME &&
563+
process.env.OPACITY_PROVER_URL &&
564+
process.env.VERIFIABLE_INFERENCE_ENABLED === "true"
565+
) {
566+
verifiableInferenceAdapter = new OpacityAdapter({
567+
teamId: process.env.OPACITY_TEAM_ID,
568+
teamName: process.env.OPACITY_CLOUDFLARE_NAME,
569+
opacityProverUrl: process.env.OPACITY_PROVER_URL,
570+
modelProvider: character.modelProvider,
571+
token: token,
572+
});
573+
elizaLogger.log("Verifiable inference adapter initialized");
574+
elizaLogger.log("teamId", process.env.OPACITY_TEAM_ID);
575+
elizaLogger.log("teamName", process.env.OPACITY_CLOUDFLARE_NAME);
576+
elizaLogger.log("opacityProverUrl", process.env.OPACITY_PROVER_URL);
577+
elizaLogger.log("modelProvider", character.modelProvider);
578+
elizaLogger.log("token", token);
579+
}
557580

558581
return new AgentRuntime({
559582
databaseAdapter: db,
@@ -683,7 +706,7 @@ export async function createAgent(
683706
managers: [],
684707
cacheManager: cache,
685708
fetch: logFetch,
686-
// verifiableInferenceAdapter,
709+
verifiableInferenceAdapter,
687710
});
688711
}
689712

packages/core/src/generation.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,18 @@ export async function generateText({
212212
model: modelClass,
213213
verifiableInference,
214214
});
215-
215+
elizaLogger.log("Using provider:", runtime.modelProvider);
216216
// If verifiable inference is requested and adapter is provided, use it
217217
if (verifiableInference && runtime.verifiableInferenceAdapter) {
218+
elizaLogger.log("Using verifiable inference adapter:", runtime.verifiableInferenceAdapter);
218219
try {
219-
const result =
220+
const result: VerifiableInferenceResult =
220221
await runtime.verifiableInferenceAdapter.generateText(
221222
context,
222223
modelClass,
223224
verifiableInferenceOptions
224225
);
225-
226+
elizaLogger.log("Verifiable inference result:", result);
226227
// Verify the proof
227228
const isValid =
228229
await runtime.verifiableInferenceAdapter.verifyProof(result);

packages/core/src/types.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ export interface ISlackService extends Service {
14151415
* Available verifiable inference providers
14161416
*/
14171417
export enum VerifiableInferenceProvider {
1418-
RECLAIM = "reclaim",
1418+
OPACITY = "opacity",
14191419
}
14201420

14211421
/**
@@ -1436,8 +1436,10 @@ export interface VerifiableInferenceOptions {
14361436
export interface VerifiableInferenceResult {
14371437
/** Generated text */
14381438
text: string;
1439-
/** Proof data */
1440-
proof: unknown;
1439+
/** Proof */
1440+
proof: any;
1441+
/** Proof id */
1442+
id?: string;
14411443
/** Provider information */
14421444
provider: VerifiableInferenceProvider;
14431445
/** Timestamp */
@@ -1448,6 +1450,7 @@ export interface VerifiableInferenceResult {
14481450
* Interface for verifiable inference adapters
14491451
*/
14501452
export interface IVerifiableInferenceAdapter {
1453+
options: any;
14511454
/**
14521455
* Generate text with verifiable proof
14531456
* @param context The input text/prompt

packages/plugin-opacity/README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# @elizaos/adapter-opacity
2+
3+
This adapter integrates Opacity proofs into ElizaOS, enabling verifiable inference results from various AI model providers available through the [CloudFlare AI Gateway](https://developers.cloudflare.com/ai-gateway). It implements the `IVerifiableInferenceAdapter` interface, making it compatible with other verifiable inference solutions.
4+
5+
## Installation
6+
7+
```bash
8+
pnpm add @elizaos/adapter-opacity
9+
```
10+
11+
## Configuration
12+
13+
Add the following environment variables to your `.env` file:
14+
15+
```env
16+
OPACITY_TEAM_ID=f309ac8ae8a9a14a7e62cd1a521b1c5f
17+
OPACITY_CLOUDFLARE_NAME=eigen-test
18+
OPACITY_PROVER_URL=https://opacity-ai-zktls-demo.vercel.app
19+
# Verifiable Inference Configuration
20+
VERIFIABLE_INFERENCE_ENABLED=true # Set to true to enable verifiable inference
21+
VERIFIABLE_INFERENCE_PROVIDER=opacity # Options: opacity
22+
```
23+
(make sure to VERIFIABLE_INFERENCE_ENABLED to true!)
24+
25+
## Usage
26+
27+
```typescript
28+
import { OpacityAdapter } from "@elizaos/adapter-opacity";
29+
import { VerifiableInferenceOptions } from "@elizaos/core";
30+
31+
// Initialize the adapter
32+
const opacityAdapter = new OpacityAdapter(runtime, {
33+
teamId: process.env.OPACITY_TEAM_ID,
34+
teamName: process.env.OPACITY_CLOUDFLARE_NAME,
35+
baseUrl: process.env.OPACITY_PROVER_URL
36+
});
37+
38+
// Generate text with verifiable results
39+
const options: VerifiableInferenceOptions = {
40+
// Optional: Override the default endpoint
41+
endpoint: "https://custom-api.example.com",
42+
// Optional: Add custom headers
43+
headers: {
44+
"X-Custom-Header": "value",
45+
},
46+
// Optional: Provider-specific options
47+
providerOptions: {
48+
temperature: 0.7,
49+
},
50+
};
51+
52+
const result = await opacityAdapter.generateText(
53+
"What is Rust?",
54+
"gpt-4",
55+
options
56+
);
57+
58+
console.log("Response:", result.text);
59+
console.log("Proof:", result.proof);
60+
61+
// Verify the proof
62+
const isValid = await opacityAdapter.verifyProof(result);
63+
console.log("Proof is valid:", isValid);
64+
```
65+
66+
## Features
67+
68+
- Implements `IVerifiableInferenceAdapter` interface for standardized verifiable inference
69+
- Support for multiple AI model provider, in the future may be expanded to accomdate for all gateways [supported by CloudFlare](https://developers.cloudflare.com/ai-gateway/providers/)
70+
- Customizable options for each request
71+
- Built-in proof verification
72+
73+
## Response Format
74+
75+
The adapter returns a `VerifiableInferenceResult` object containing:
76+
77+
```typescript
78+
{
79+
text: string; // The generated text response
80+
proof: unknown; // The proof data
81+
provider: string; // The provider name (e.g., "opacity")
82+
timestamp: number; // Generation timestamp
83+
metadata?: { // Optional metadata
84+
modelProvider: string;
85+
modelClass: string;
86+
endpoint: string;
87+
}
88+
}
89+
```
90+
91+
## How it Works
92+
93+
The Opacity adapter wraps AI model API calls to CloudFlare, then performs MPC-TLS on the logged responses.
94+
95+
96+
This allows you to:
97+
1. Make verifiable API calls to AI model providers
98+
2. Generate proofs of the responses
99+
3. Verify the authenticity of the responses
100+
4. Ensure the responses haven't been tampered with
101+
102+
## Step By Step
103+
```mermaid
104+
sequenceDiagram
105+
autonumber
106+
participant Eliza
107+
participant Cloudflare
108+
participant OpenAI
109+
Eliza ->> Cloudflare : Prompt Req
110+
Cloudflare ->> OpenAI : Prompt Req
111+
OpenAI ->> Cloudflare : Prompt Response
112+
Cloudflare ->> Cloudflare : Log Prompt Response
113+
Cloudflare ->> Eliza : Prompt Response
114+
create participant Opacity
115+
Eliza ->> Opacity : Generate Proof for Prompt Response
116+
Opacity ->> Cloudflare : Fetch Prompt Response Log in MPC-TLS
117+
Cloudflare ->> Opacity : Respond with Prompt Response Log
118+
Opacity ->> Eliza : Return Proof of Prompt Response Log
119+
```
120+
121+
## License
122+
123+
MIT

packages/plugin-opacity/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@elizaos/plugin-opacity",
3+
"version": "0.1.7-alpha.2",
4+
"description": "Opacity Protocol adapter for ElizaOS",
5+
"main": "dist/index.js",
6+
"type": "module",
7+
"types": "dist/index.d.ts",
8+
"scripts": {
9+
"build": "tsup src/index.ts --format esm --dts",
10+
"lint": "eslint --fix --cache .",
11+
"watch": "tsc --watch",
12+
"dev": "tsup src/index.ts --format esm --dts --watch"
13+
},
14+
"dependencies": {
15+
"@elizaos/core": "workspace:*",
16+
"dotenv": "^16.4.5"
17+
},
18+
"devDependencies": {
19+
"tsup": "^8.3.5"
20+
}
21+
}

0 commit comments

Comments
 (0)