Skip to content

Commit

Permalink
docs: disable ChatGPT and Supabase (#6212)
Browse files Browse the repository at this point in the history
docs: dissable ChatGPT and Supabase

Co-authored-by: Jack Shelton <[email protected]>
  • Loading branch information
mhevery and thejackshelton authored May 30, 2024
1 parent 972b44e commit 7c08371
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 306 deletions.
6 changes: 3 additions & 3 deletions packages/docs/src/components/docsearch/result.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Slot, component$, useContext, useSignal, useStore, useTask$ } from '@builder.io/qwik';
import { QwikGPT } from '../qwik-gpt';
// import { QwikGPT } from '../qwik-gpt';
import { SearchContext } from './context';
import { AiResultOpenContext, type DocSearchState } from './doc-search';
import { Snippet } from './snippet';
Expand Down Expand Up @@ -134,7 +134,7 @@ export const AIButton = component$(({ state }: { state: DocSearchState }) => {
}
}}
>
<div class="ai-button">
{/* <div class="ai-button">
<button
onClick$={() => {
gpt.value = state.query;
Expand All @@ -157,7 +157,7 @@ export const AIButton = component$(({ state }: { state: DocSearchState }) => {
<QwikGPT query={gpt.value}></QwikGPT>
</div>
)}
</div>
</div> */}
</li>
)}
</>
Expand Down
64 changes: 32 additions & 32 deletions packages/docs/src/components/qwik-gpt/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { component$, useComputed$, useSignal, useTask$ } from '@builder.io/qwik';
import { qwikGPT, rateResponse } from './search';
import { component$, useComputed$, useSignal } from '@builder.io/qwik';
// import { qwikGPT, rateResponse } from './search';
import { CodeBlock } from '../code-block/code-block';
import { isBrowser } from '@builder.io/qwik/build';
// import { isBrowser } from '@builder.io/qwik/build';
import snarkdown from 'snarkdown';

const snarkdownEnhanced = (md: string) => {
Expand All @@ -18,9 +18,9 @@ const snarkdownEnhanced = (md: string) => {

export const QwikGPT = component$((props: { query: string }) => {
const message = useSignal('');
const done = useSignal(false);
const id = useSignal<string>();
const rated = useSignal(false);
// const done = useSignal(false);
// const id = useSignal<string>();
// const rated = useSignal(false);

const process = useComputed$(() => {
const rawLines = message.value.split('\n');
Expand Down Expand Up @@ -70,30 +70,30 @@ export const QwikGPT = component$((props: { query: string }) => {
return lines;
});

useTask$(({ track }) => {
const query = track(() => props.query);
if (isBrowser) {
message.value = '';
done.value = false;
rated.value = false;
id.value = undefined;
if (props.query !== '') {
const run = async () => {
done.value = false;
const response = await qwikGPT(query);
for await (const value of response) {
if (typeof value === 'string') {
message.value += value;
} else if (value.type === 'id') {
id.value = value.content;
}
}
done.value = true;
};
run();
}
}
});
// useTask$(({ track }) => {
// const query = track(() => props.query);
// if (isBrowser) {
// message.value = '';
// done.value = false;
// rated.value = false;
// id.value = undefined;
// if (props.query !== '') {
// const run = async () => {
// done.value = false;
// const response = await qwikGPT(query);
// for await (const value of response) {
// if (typeof value === 'string') {
// message.value += value;
// } else if (value.type === 'id') {
// id.value = value.content;
// }
// }
// done.value = true;
// };
// run();
// }
// }
// });

if (message.value === '' && props.query !== '') {
return (
Expand All @@ -114,7 +114,7 @@ export const QwikGPT = component$((props: { query: string }) => {
);
})}
</div>
{done.value && (
{/* {done.value && (
<div class="ai-rate">
{rated.value ? (
<>Thank you very much!</>
Expand Down Expand Up @@ -142,7 +142,7 @@ export const QwikGPT = component$((props: { query: string }) => {
</>
)}
</div>
)}
)} */}
</>
);
});
220 changes: 110 additions & 110 deletions packages/docs/src/components/qwik-gpt/search.tsx
Original file line number Diff line number Diff line change
@@ -1,117 +1,117 @@
import { server$ } from '@builder.io/qwik-city';
import { createClient } from '@supabase/supabase-js';
// import { server$ } from '@builder.io/qwik-city';
// import { createClient } from '@supabase/supabase-js';
import gpt from './gpt.md?raw';
import { chatCompletion } from './streaming-gpt';
// import { chatCompletion } from './streaming-gpt';

const files = new Map<string, Promise<string>>();

export const qwikGPT = server$(async function* (query: string) {
const supabase = createClient(this.env.get('SUPABASE_URL')!, this.env.get('SUPABASE_KEY')!);
const normalizedQuery = normalizeLine(query);
const response = await fetch('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.env.get('OPENAI_KEY')}`,
},
body: JSON.stringify({
input: normalizedQuery,
model: 'text-embedding-ada-002',
}),
});
const data = await response.json();
const embeddings = data.data[0].embedding;

const docs = await supabase.rpc('match_docs_10', {
query_text: normalizedQuery.replaceAll(' ', '|'),
query_embedding: embeddings,
match_count: 6,
similarity_threshold: 0.79,
});

const resultsHash = await getResultsHash(docs.data);
const existingQuery = await supabase.rpc('match_query_3', {
query_embedding: embeddings,
similarity_threshold: 0.95,
hash: resultsHash,
});
if (existingQuery.data.length === 1) {
const entry = existingQuery.data[0];
yield {
type: 'id',
content: entry.id,
};
yield entry.output;
return;
}

// Download docs
try {
const docsStr = await resolveContext(docs.data);
let model = 'gpt-4';
if (docsStr.length < 3500 * 3 && Math.random() < 0.5) {
model = 'gpt-3.5-turbo';
}
const insert = supabase
.from('search_queries')
.insert({
query: query,
embedding: embeddings,
results: docs.data,
results_hash: resultsHash,
model,
})
.select('id');

const id = (await insert).data?.[0].id as string;
yield {
type: 'id',
content: id,
};

if (docs.data.length === 0) {
yield 'We could not find any documentation that matches your question. Please try again rephrasing your question to be more factual.';
return;
}

const generator = chatCompletion(this.env.get('OPENAI_KEY')!, {
model: model,
temperature: 0,
messages: [
{
role: 'system',
content:
'You are QwikGPT, your job is to answer questions about Qwik, a new javascript framework focused on instant interactivity and server-side rendering.\nRelevant Qwik documentation and the user question will be provided. Try to answer the question in a short and concise way.',
},
{
role: 'user',
content: docsStr,
},
{
role: 'user',
content: `User question, respond in markdown including links to the sources, if you are not sure about the answer, say that you do not know:\n\n${query}`,
},
],
});

let output = '';
for await (const chunk of generator) {
output += chunk;
yield chunk as string;
}
await supabase.from('search_queries').update({ output }).eq('id', id);
} catch (e) {
console.error(e);
}
});

export const rateResponse = server$(async function (query_id: string, rate: number) {
const supabase = createClient(this.env.get('SUPABASE_URL')!, this.env.get('SUPABASE_KEY')!);
await supabase.from('search_rate').insert({
query_id: query_id,
rate: rate,
});
});
// export const qwikGPT = server$(async function* (query: string) {
// const supabase = createClient(this.env.get('SUPABASE_URL')!, this.env.get('SUPABASE_KEY')!);
// const normalizedQuery = normalizeLine(query);
// const response = await fetch('https://api.openai.com/v1/embeddings', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${this.env.get('OPENAI_KEY')}`,
// },
// body: JSON.stringify({
// input: normalizedQuery,
// model: 'text-embedding-ada-002',
// }),
// });
// const data = await response.json();
// const embeddings = data.data[0].embedding;

// const docs = await supabase.rpc('match_docs_10', {
// query_text: normalizedQuery.replaceAll(' ', '|'),
// query_embedding: embeddings,
// match_count: 6,
// similarity_threshold: 0.79,
// });

// const resultsHash = await getResultsHash(docs.data);
// const existingQuery = await supabase.rpc('match_query_3', {
// query_embedding: embeddings,
// similarity_threshold: 0.95,
// hash: resultsHash,
// });
// if (existingQuery.data.length === 1) {
// const entry = existingQuery.data[0];
// yield {
// type: 'id',
// content: entry.id,
// };
// yield entry.output;
// return;
// }

// // Download docs
// try {
// const docsStr = await resolveContext(docs.data);
// let model = 'gpt-4';
// if (docsStr.length < 3500 * 3 && Math.random() < 0.5) {
// model = 'gpt-3.5-turbo';
// }
// const insert = supabase
// .from('search_queries')
// .insert({
// query: query,
// embedding: embeddings,
// results: docs.data,
// results_hash: resultsHash,
// model,
// })
// .select('id');

// const id = (await insert).data?.[0].id as string;
// yield {
// type: 'id',
// content: id,
// };

// if (docs.data.length === 0) {
// yield 'We could not find any documentation that matches your question. Please try again rephrasing your question to be more factual.';
// return;
// }

// const generator = chatCompletion(this.env.get('OPENAI_KEY')!, {
// model: model,
// temperature: 0,
// messages: [
// {
// role: 'system',
// content:
// 'You are QwikGPT, your job is to answer questions about Qwik, a new javascript framework focused on instant interactivity and server-side rendering.\nRelevant Qwik documentation and the user question will be provided. Try to answer the question in a short and concise way.',
// },
// {
// role: 'user',
// content: docsStr,
// },
// {
// role: 'user',
// content: `User question, respond in markdown including links to the sources, if you are not sure about the answer, say that you do not know:\n\n${query}`,
// },
// ],
// });

// let output = '';
// for await (const chunk of generator) {
// output += chunk;
// yield chunk as string;
// }
// await supabase.from('search_queries').update({ output }).eq('id', id);
// } catch (e) {
// console.error(e);
// }
// });

// export const rateResponse = server$(async function (query_id: string, rate: number) {
// const supabase = createClient(this.env.get('SUPABASE_URL')!, this.env.get('SUPABASE_KEY')!);
// await supabase.from('search_rate').insert({
// query_id: query_id,
// rate: rate,
// });
// });

export function normalizeLine(line: string) {
line = line.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
Expand Down
Loading

0 comments on commit 7c08371

Please sign in to comment.