Skip to content
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

convert return type from json to html #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion secrets/default.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"apiKey": "sk-qVBlJkO3e99t81623PsB0zHookSQJxU360gDMooLenN01gv2"
"apiKey": "sk-oMNgY7zegK6wUDgVA3mtYr1TmENUjOJFzgw6nM4genvAq4lK"
}
60 changes: 34 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,60 @@ async function getChatCompletion(apiKey: string, model: string, chatQuery: strin
return result
}

app.get('/', async (c) => {
let vault: Record<string, string> = {}
let queries = c.req.queries() || {}
app.get("/", async (c) => {
let vault: Record<string, string> = {};
let queries = c.req.queries() || {};
try {
vault = JSON.parse(process.env.secret || '')
vault = JSON.parse(process.env.secret || "");
} catch (e) {
console.error(e)
return c.json({ error: "Failed to parse secrets" })
console.error(e);
return c.json({ error: "Failed to parse secrets" });
}
const apiKey = (vault.apiKey) ? vault.apiKey : 'sk-qVBlJkO3e99t81623PsB0zHookSQJxU360gDMooLenN01gv2'
const apiKey = vault.apiKey
? vault.apiKey
: "sk-oMNgY7zegK6wUDgVA3mtYr1TmENUjOJFzgw6nM4genvAq4lK";
// Choose from any model listed here https://docs.red-pill.ai/get-started/supported-models
const model = (queries.model) ? queries.model[0] : 'gpt-4o'
const chatQuery = (queries.chatQuery) ? queries.chatQuery[0] : 'Who are you?'
const model = queries.model ? queries.model[0] : "gpt-4o";
const chatQuery = queries.chatQuery ? queries.chatQuery[0] : "Who are you?";
let result = {
model,
chatQuery: chatQuery,
message: ''
message: "",
};

result.message = await getChatCompletion(apiKey, model, chatQuery)
result.message = await getChatCompletion(apiKey, model, chatQuery);

return c.json(result)
})
// return c.json(result);
return c.html(
`<p>Model: ${result.model} </p> <p>Query: ${result?.chatQuery}</p> <p>Response: ${result.message}</p>`
);
});

app.post('/', async (c) => {
let vault: Record<string, string> = {}
const data = await c.req.json()
console.log('user payload in JSON:', data)
app.post("/", async (c) => {
let vault: Record<string, string> = {};
const data = await c.req.json();
console.log("user payload in JSON:", data);
try {
vault = JSON.parse(process.env.secret || '')
vault = JSON.parse(process.env.secret || "");
} catch (e) {
console.error(e)
return c.json({ error: "Failed to parse secrets" })
console.error(e);
return c.json({ error: "Failed to parse secrets" });
}
const apiKey = (vault.apiKey) ? vault.apiKey : 'sk-qVBlJkO3e99t81623PsB0zHookSQJxU360gDMooLenN01gv2'
const model = (data.model) ? data.model : 'gpt-4o'
const chatQuery = (data.chatQuery) ? data.chatQuery : 'Who are you?'
const apiKey = vault.apiKey
? vault.apiKey
: "sk-oMNgY7zegK6wUDgVA3mtYr1TmENUjOJFzgw6nM4genvAq4lK";
const model = data.model ? data.model : "gpt-4o";
const chatQuery = data.chatQuery ? data.chatQuery : "Who are you?";
let result = {
model,
chatQuery: chatQuery,
message: ''
message: "",
};

result.message = await getChatCompletion(apiKey, model, chatQuery)
result.message = await getChatCompletion(apiKey, model, chatQuery);

return c.json(result)
return c.json(result);
// return c.html(`<p>what the hell? </p>`);
});

export default handle(app)
65 changes: 36 additions & 29 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@ import { afterAll, describe, expect, test, vi } from 'vitest'
import { app } from '../src/'

// Set Testing env secrets
const apiKey = JSON.stringify({apiKey: 'sk-qVBlJkO3e99t81623PsB0zHookSQJxU360gDMooLenN01gv2'})
vi.stubEnv('secret', apiKey)
const apiKey = JSON.stringify({
apiKey: "sk-oMNgY7zegK6wUDgVA3mtYr1TmENUjOJFzgw6nM4genvAq4lK",
});
vi.stubEnv("secret", apiKey);

describe('Test RedPill AI Agent Contract', () => {
test('GET Test: Pass chatQuery through URL Query', async () => {
let chatQuery = 'Who are you?'
let model = 'o1-preview'
const resp = await app.request(encodeURI(`/?chatQuery=${chatQuery}&model=${model}`))
expect(resp.status).toBe(200)
expect(resp.headers.get('content-type')?.toLowerCase()).toBe('application/json; charset=utf-8')
const data = await resp.json()
console.log(data)
expect(data).toHaveProperty('model')
expect(data).toHaveProperty('chatQuery')
expect(data).toHaveProperty('message')
})
describe("Test RedPill AI Agent Contract", () => {
test("GET Test: Pass chatQuery through URL Query", async () => {
let chatQuery = "Who are you?";
let model = "o1-preview";
const resp = await app.request(
encodeURI(`/?chatQuery=${chatQuery}&model=${model}`)
);
expect(resp.status).toBe(200);
expect(resp.headers.get("content-type")?.toLowerCase()).toBe(
"text/html; charset=utf-8"
);
const data = await resp;
const text = await data.text();
console.log("text", text);
expect(data).toHaveProperty("text");
});

test('POST Test: Pass chatQuery and model through body of POST request', async () => {
const input = { chatQuery: 'What is FooBar?', model: 'gpt-4o' }
const resp = await app.request('/', {
method: 'POST',
test("POST Test: Pass chatQuery and model through body of POST request", async () => {
const input = { chatQuery: "What is FooBar?", model: "gpt-4o" };
const resp = await app.request("/", {
method: "POST",
body: JSON.stringify(input),
})
expect(resp.status).toBe(200)
expect(resp.headers.get('content-type')?.toLowerCase()).toBe('application/json; charset=utf-8')
const data = await resp.json()
console.log(data)
expect(data).toHaveProperty('model')
expect(data).toHaveProperty('chatQuery')
expect(data).toHaveProperty('message')
})
})
});
expect(resp.status).toBe(200);
expect(resp.headers.get("content-type")?.toLowerCase()).toBe(
"application/json; charset=utf-8"
);
const data = await resp.json();
console.log(data);
expect(data).toHaveProperty("model");
expect(data).toHaveProperty("chatQuery");
expect(data).toHaveProperty("message");
});
});

afterAll(async () => {
console.log(`\nNow you are ready to publish your agent, add secrets, and interact with your agent in the following steps:\n- Execute: 'npm run publish-agent'\n- Set secrets: 'npm run set-secrets'\n- Go to the url produced by setting the secrets (e.g. https://wapo-testnet.phala.network/ipfs/QmPQJD5zv3cYDRM25uGAVjLvXGNyQf9Vonz7rqkQB52Jae?key=b092532592cbd0cf)`)
Expand Down