diff options
author | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-05-17 02:31:22 -0400 |
---|---|---|
committer | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-05-17 02:31:22 -0400 |
commit | 825463b4f7bb609082d16b302d40c6af56c98b26 (patch) | |
tree | dc4d6077dd3f951f3c78973e04621c879b8b2ee0 /src | |
parent | d56ce09de98d843cf46b0d6eda5f8c82446f7849 (diff) |
we did it joe
Diffstat (limited to 'src')
-rw-r--r-- | src/client/apis/gpt/GPT.ts | 2 | ||||
-rw-r--r-- | src/client/views/collections/CollectionCardDeckView.tsx | 188 |
2 files changed, 126 insertions, 64 deletions
diff --git a/src/client/apis/gpt/GPT.ts b/src/client/apis/gpt/GPT.ts index 7c53ee83e..9a24c808c 100644 --- a/src/client/apis/gpt/GPT.ts +++ b/src/client/apis/gpt/GPT.ts @@ -20,7 +20,7 @@ const callTypeMap: { [type: string]: GPTCallOpts } = { summary: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: 'Summarize this text in simpler terms: ' }, edit: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: 'Reword this: ' }, completion: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: '' }, - sort:{model:'gpt-4-turbo',maxTokens:2048,temp:0.5,prompt:"I'm going to give you a list of strings. Sort them into lists by shared content. The format of should be: [string1, string2], [string 3, string4], [string5, string6] where the actual numbers of strings in each category / total categorys is arbritray and up to your interpretation"}, + sort:{model:'gpt-4o',maxTokens:2048,temp:0.5,prompt:"I'm going to give you a list of descriptions. Each one is seperated by ====== on either side. They will vary in length, so make sure to only seperate when you see ======. Sort them into lists by shared content. MAKE SURE EACH DESCRIPTOR IS IN ONLY ONE LIST. Generate only the list with each list seperated by ====== with the elements seperated by ~~~~~~"}, describe:{model:'gpt-4-vision-preview',maxTokens:2048,temp:0,prompt:"Describe these images in 3-5 words"}, diff --git a/src/client/views/collections/CollectionCardDeckView.tsx b/src/client/views/collections/CollectionCardDeckView.tsx index e7e2cba4a..e64c013f1 100644 --- a/src/client/views/collections/CollectionCardDeckView.tsx +++ b/src/client/views/collections/CollectionCardDeckView.tsx @@ -24,7 +24,10 @@ import { ImageField, PdfField, URLField } from '../../../fields/URLField'; @observer export class CollectionCardView extends CollectionSubView() { - @observable selectedNodeIndex = -1; + + //TODO: for saving custom groups instead of string field hashmap nonsense should instead just set teh index for each child document per group as a fie;d + // so like, group1Ibndex = 1 etc etc as they change. That way, if the documents change it doesnt matter since the idnexes are tied to the + // documents themselves @observable hoveredNodeIndex = -1; @@ -223,40 +226,34 @@ export class CollectionCardView extends CollectionSubView() { // return 0; }; + @observable sortedDocs = [] as { layout: Doc; data: Doc }[]; + + @computed get sortedDocsType() { + if (this._props.Document.cardSort === 'chat' && this.sortedDocs.length === 0) { + this.smartSort(); // Trigger the sorting if it hasn't been done yet + return { docs: [] }; // Return an empty array or a loading state + } + const desc = BoolCast(this.layoutDoc.sortDesc); let sorted = []; let docs = []; - + for (let i = 0; i < this.myChildLayoutPairs.length; i++) { //copying everything in childlayout pairs to sorted so that i can use the sort function without altering the original list sorted[i] = this.myChildLayoutPairs[i]; } - + switch (this._props.Document.cardSort) { case 'type': // Copy and sort documents by type return this.sort(sorted, 'type', desc); case 'color': return this.sort(sorted, 'color', desc); - // case "links": - // console.log("hi") - - // let links = LinkManager.Instance.getAllRelatedLinks(this.myChildLayoutPairs[0].layout) - - // console.log(links) - - // } - case 'custom': - // console.log('hiiiii'); return this.sort(sorted, 'custom', desc); - case 'chat': - this.getImageDesc() - docs = this.myChildLayoutPairs; - return {docs} - + return { docs: this.sortedDocs }; // Use the sorted docs from the observable default: docs = this.myChildLayoutPairs; return { docs }; @@ -314,7 +311,21 @@ export class CollectionCardView extends CollectionSubView() { // console.log(typeA + "A") typeB = this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)].get(docs.indexOf(docB)) ?? '9999'; // console.log(typeB + 'b') - break; + break; + + case 'gpt': + + // console.log(this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)]) + typeA = this.gptGroups.get(docA.layout) ?? '9999'; + console.log(typeA + "typea") + typeB = this.gptGroups.get(docB.layout) ?? '9999'; + console.log(typeB + "typeB") + + + // console.log(typeA + "A") + // typeB = this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)].get(docs.indexOf(docB)) ?? '9999'; + // console.log(typeB + 'b') + break; default: typeA = docA.layout.type ?? ''; // If docA.type is undefined, use an empty string @@ -488,6 +499,8 @@ export class CollectionCardView extends CollectionSubView() { // Cast(selected.Document.data, WebField, null)?.url?.href) + //note richtext + // StrCast(selected.Document.data, Cast(selected.Document.data, WebField, null)?.url?.href) @@ -506,23 +519,42 @@ export class CollectionCardView extends CollectionSubView() { // const pdf = (StrCast(this.myChildLayoutPairs[0].layout.text)); //pdf - // const queryText = (RTFCast(this.myChildLayoutPairs[0].layout.text)); //everything else + const queryText = this.myChildLayoutPairs[0].layout.type; //everything else // const queryText = RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text; - console.log(perchance ?? "sad") + console.log(queryText ?? "sad") } - childPairStringList () { - let string = "" - for (let i=0; i < this.myChildLayoutPairs.length; i++){ - - - let text = StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text) - string += StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text) + ", " - this.textToDoc.has() + async childPairStringList(): Promise<string> { + let string = ""; + for (let i = 0; i < this.childDocs.length; i++) { + switch (this.childDocs[i].type) { + case DocumentType.IMG: + string += `======${await this.getImageDesc(this.childDocs[i])}======`; + break; + + case DocumentType.PDF: + let pdfText = StrCast(this.childDocs[i].text); + let words = pdfText.split(/\s+/); + let first50Words = words.slice(0, 50); + string += `======${first50Words.join(' ')}======`; + + this.textToDoc.set(first50Words.join(' ').trim(), this.childDocs[i]); + break; + + case DocumentType.RTF: + let rtfText = StrCast((RTFCast(this.childDocs[i].text)).Text); + string += `======${StrCast((RTFCast(this.childDocs[i].text)).Text)}======`; + this.textToDoc.set(rtfText.trim(), this.childDocs[i]); + break; + + default: + string += `======${StrCast(this.childDocs[i].title)}======`; + this.textToDoc.set(StrCast(this.childDocs[i].title).trim(), this.childDocs[i]); + } } - return string + return string; } @observable isLoading = false @@ -547,21 +579,20 @@ export class CollectionCardView extends CollectionSubView() { textToDoc = new Map<string, Doc>() gptProccessedImages = new Set<Doc>() - @action async getImageDesc(){ - const images = this.childDocs.filter(d => d.type == DocumentType.IMG).filter(d=> !this.gptProccessedImages.has(d)); - images.map(doc => { - let href = (doc['data'] as URLField).url.href; + @action async getImageDesc(image: Doc){ + let href = (image['data'] as URLField).url.href; let hrefParts = href.split('.'); let hrefComplete = `${hrefParts[0]}_o.${hrefParts[1]}`; - return this.imageUrlToBase64(hrefComplete).then(hrefBase64 => { - return gptImageLabel(hrefBase64).then(response => { - this.textToDoc.set(response, doc) - this.gptProccessedImages.add(doc) - console.log(response); - - }); - }); - }); + try { + let hrefBase64 = await this.imageUrlToBase64(hrefComplete); + let response = await gptImageLabel(hrefBase64); + this.textToDoc.set(response.trim(), image); + this.gptProccessedImages.add(image); + console.log(response); + return response; // Return the response from gptImageLabel + } catch (error) { + console.log("bad things have happened"); + } } @@ -572,29 +603,60 @@ export class CollectionCardView extends CollectionSubView() { @action async smartSort() { this.isLoading = true; - console.log("loading") - let prompt= "" - - if (this.childPairStringList() == ""){ - console.log("no child pairs :(") + console.log("loading"); + + // Store the result of childPairStringList in a variable + const childPairStrings = await this.childPairStringList(); + + if (childPairStrings === "") { + console.log("no child pairs :("); + } else { + console.log(childPairStrings + " og list"); + let prompt = childPairStrings; + + let res = await gptAPICall(prompt, GPTCallType.SORT); + this.isLoading = false; + if (res == 'Error connecting with API.') { + // If GPT call failed + console.error('GPT call failed'); + } else if (res != null) { + console.log(res); + this.processGptOutput(res); + // Update the observable with the sorted documents + this.sortedDocs = this.sort(this.myChildLayoutPairs, 'gpt', BoolCast(this.layoutDoc.sortDesc)).docs; + } + this.isLoading = false; } + } - else{ - console.log(this.childPairStringList() + "og list") - prompt = "Sort this list of strings by shared content " + this.childPairStringList() - } - + gptGroups = new Map<Doc, number> + + // Method to convert the GPT-produced string into a map + processGptOutput(gptOutput: string) { + + // Split the string into individual list items + const listItems = gptOutput.split('======').filter(item => item.trim() !== ''); + + listItems.forEach((item, index) => { + // Split the item by '~~~~~~' to get all descriptors + const parts = item.split('~~~~~~').map(part => part.trim()); + + parts.forEach(part => { + console.log(part + "part") + // Find the corresponding Doc in the textToDoc map + if (this.textToDoc.has(part)) { + const doc = this.textToDoc.get(part); + console.log("hewoooooo") + if (doc) { + console.log('hi') + this.gptGroups.set(doc, index); + } + } + }); + }); + } - let res = await gptAPICall(prompt, GPTCallType.SORT); - this.isLoading = false; - if (res == 'Error connecting with API.') { - // If GPT call failed - console.error('GPT call failed'); - } else if (res != null) { - console.log(res) - } - this.isLoading = false; - }; + renderButtons(childPairIndex: number) { const buttons = []; // Array to hold the button elements |