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

moving openai classes from lib to openai #345

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
4 changes: 2 additions & 2 deletions JS/edgechains/lib/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
"presets": ["@babel/preset-env"]
}
15 changes: 7 additions & 8 deletions JS/edgechains/lib/create-edgechains/__common/htmljs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ export const rootLayout = (layoutToApply) => {
export const layout = (layoutToApply) => {
return async (c, next) => {
await next();
if (
(c.req.header("HX-Request") === "true" &&
(c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) ||
c.req.header("HX-Request") !== "true"
) {
if ((c.req.header("HX-Request") === "true" &&
(c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) ||
c.req.header("HX-Request") !== "true") {
// Req is regular req or boosted link, so we apply layouts
const curBody = await c.res.text();
c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
Expand All @@ -38,7 +36,7 @@ export const layout = (layoutToApply) => {
};
export const Link = ({ to, "hx-target": hxTarget, class: className, children }) => {
if (hxTarget) {
return html`<a
return html `<a
href="${to}"
class="${className}"
hx-get="${to}"
Expand All @@ -47,7 +45,8 @@ export const Link = ({ to, "hx-target": hxTarget, class: className, children })
hx-swap="morph"
>${children}</a
>`;
} else {
return html`<a href="${to}" class="${className}" hx-boost="true">${children}</a>`;
}
else {
return html `<a href="${to}" class="${className}" hx-boost="true">${children}</a>`;
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html } from "hono/html";
const ExampleLayout = (props) => html`
const ExampleLayout = (props) => html `
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ HydeSearchRouter.get("/search", async (c) => {
},
orderRRF: query.orderRRF,
};
const answer = await hydeSearchAdaEmbedding(
arkRequest,
process.env.OPENAI_API_KEY,
process.env.OPENAI_ORG_ID
);
const answer = await hydeSearchAdaEmbedding(arkRequest, process.env.OPENAI_API_KEY, process.env.OPENAI_ORG_ID);
const final_answer = answer.finalAnswer;
const responses = answer.wordEmbeddings;
const data = { responses, final_answer };
Expand All @@ -36,36 +32,26 @@ HydeSearchRouter.get("/search", async (c) => {
<div class="card-body">${data.final_answer}</div>
</div>
<ul class="list-unstyled mb-0">
${data.responses.map(
(item) => `
${data.responses.map((item) => `
<li>
<div class="card">
<div class="card-body">
${
item.rawText != null
? `<div class="card card-body">${item.rawText}</div>`
: `<div class="card card-body">${item.metadata}</div>`
}
${
item.filename != null
? `<div class="card card-body" style="color: blue;">${item.filename}</div>`
: ""
}
${
item.titleMetadata != null
? `<div class="card card-body" style="color: blue;">${item.titleMetadata}</div>`
: ""
}
${
item.documentDate != null
? `<div class="card card-body" style="color: blue;">${item.documentDate}</div>`
: ""
}
${item.rawText != null
? `<div class="card card-body">${item.rawText}</div>`
: `<div class="card card-body">${item.metadata}</div>`}
${item.filename != null
? `<div class="card card-body" style="color: blue;">${item.filename}</div>`
: ""}
${item.titleMetadata != null
? `<div class="card card-body" style="color: blue;">${item.titleMetadata}</div>`
: ""}
${item.documentDate != null
? `<div class="card card-body" style="color: blue;">${item.documentDate}</div>`
: ""}
</div>
</div>
</li>
`
)}
`)}
</ul>
</html>
`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { ArkRequest } from "@arakoodev/edgechains.js";
declare function hydeSearchAdaEmbedding(
arkRequest: ArkRequest,
apiKey: string,
orgId: string
): Promise<{
declare function hydeSearchAdaEmbedding(arkRequest: ArkRequest, apiKey: string, orgId: string): Promise<{
wordEmbeddings: any;
finalAnswer: any;
}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ var PostgresDistanceMetric;
})(PostgresDistanceMetric || (PostgresDistanceMetric = {}));
async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) {
try {
const gpt3endpoint = new OpenAiEndpoint(
"https://api.openai.com/v1/chat/completions",
apiKey,
orgId,
"gpt-3.5-turbo",
"user",
parseInt("0.7")
);
const gpt3endpoint = new OpenAiEndpoint("https://api.openai.com/v1/chat/completions", apiKey, orgId, "gpt-3.5-turbo", "user", parseInt("0.7"));
// Get required params from API...
const table = "ada_hyde_prod";
const namespace = "360_docs";
Expand All @@ -44,30 +37,17 @@ async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) {
// Chain 1 ==> Get Gpt3Response & split
const gpt3Responses = gptResponse.split("\n");
// Chain 2 ==> Get Embeddings from OpenAI using Each Response
const embeddingsListChain = Promise.all(
gpt3Responses.map(async (resp) => {
const embedding = await gpt3endpoint.embeddings(resp);
return embedding;
})
);
const embeddingsListChain = Promise.all(gpt3Responses.map(async (resp) => {
const embedding = await gpt3endpoint.embeddings(resp);
return embedding;
}));
// Chain 5 ==> Query via EmbeddingChain
const dbClient = new PostgresClient(
await embeddingsListChain,
PostgresDistanceMetric.IP,
topK,
20,
table,
namespace,
arkRequest,
15
);
const dbClient = new PostgresClient(await embeddingsListChain, PostgresDistanceMetric.IP, topK, 20, table, namespace, arkRequest, 15);
const queryResult = await dbClient.dbQuery();
// Chain 6 ==> Create Prompt using Embeddings
const retrievedDocs = [];
for (const embeddings of queryResult) {
retrievedDocs.push(
`${embeddings.raw_text}\n score:${embeddings.score}\n filename:${embeddings.filename}\n`
);
retrievedDocs.push(`${embeddings.raw_text}\n score:${embeddings.score}\n filename:${embeddings.filename}\n`);
}
if (retrievedDocs.join("").length > 4096) {
retrievedDocs.length = 4096;
Expand Down Expand Up @@ -99,7 +79,8 @@ async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) {
finalAnswer: finalAnswer,
};
return response;
} catch (error) {
}
catch (error) {
// Handle errors here
console.error(error);
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ describe("Hyde Search", () => {
},
orderRRF: "default",
};
expect(
(
await hydeSearchAdaEmbedding(
arkRequest,
process.env.OPENAI_API_KEY,
process.env.OPENAI_ORG_ID
)
).finalAnswer
).toContain("Java");
expect((await hydeSearchAdaEmbedding(arkRequest, process.env.OPENAI_API_KEY, process.env.OPENAI_ORG_ID)).finalAnswer).toContain("Java");
}, 30000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@ import { OpenAiEndpoint } from "@arakoodev/edgechains.js";
const jsonnet = new Jsonnet();
const promptPath = path.join(process.cwd(), "./src/testGeneration/prompts.jsonnet");
const testGeneratorPath = path.join(process.cwd(), "./src/testGeneration/testGenerator.jsonnet");
const gpt3endpoint = new OpenAiEndpoint(
"https://api.openai.com/v1/chat/completions",
"",
"",
"gpt-3.5-turbo",
"user",
0.7
);
const classText =
"public class ChatMessage {\n" +
const gpt3endpoint = new OpenAiEndpoint("https://api.openai.com/v1/chat/completions", "", "", "gpt-3.5-turbo", "user", 0.7);
const classText = "public class ChatMessage {\n" +
" String role;\n" +
" String content;\n\n" +
" public ChatMessage(String role, String content) {\n" +
Expand Down Expand Up @@ -50,7 +42,8 @@ export async function getContent() {
finalResponse += await gpt3endpoint.gptFnTestGenerator(responce);
console.log("Final Response.......\n\n");
return finalResponse;
} catch (error) {
}
catch (error) {
console.log(error);
}
}
Loading
Loading