From ab8bb3430b8123150286c940217e894a5f464c45 Mon Sep 17 00:00:00 2001 From: Ayomide Bajo Date: Wed, 13 Nov 2024 09:31:56 +0100 Subject: [PATCH] convert return type from json to html --- secrets/default.json | 2 +- src/index.ts | 60 ++++++++++++++++++++++------------------ tests/index.test.ts | 65 ++++++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 56 deletions(-) diff --git a/secrets/default.json b/secrets/default.json index 0e58a68..229830d 100644 --- a/secrets/default.json +++ b/secrets/default.json @@ -1,3 +1,3 @@ { - "apiKey": "sk-qVBlJkO3e99t81623PsB0zHookSQJxU360gDMooLenN01gv2" + "apiKey": "sk-oMNgY7zegK6wUDgVA3mtYr1TmENUjOJFzgw6nM4genvAq4lK" } diff --git a/src/index.ts b/src/index.ts index ca93d33..6568a24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,52 +28,60 @@ async function getChatCompletion(apiKey: string, model: string, chatQuery: strin return result } -app.get('/', async (c) => { - let vault: Record = {} - let queries = c.req.queries() || {} +app.get("/", async (c) => { + let vault: Record = {}; + 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( + `

Model: ${result.model}

Query: ${result?.chatQuery}

Response: ${result.message}

` + ); +}); -app.post('/', async (c) => { - let vault: Record = {} - const data = await c.req.json() - console.log('user payload in JSON:', data) +app.post("/", async (c) => { + let vault: Record = {}; + 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(`

what the hell?

`); }); export default handle(app) diff --git a/tests/index.test.ts b/tests/index.test.ts index c7cfda7..c16cdbe 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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)`)