From 99af64d2580c406a3089b0b576732db241286d0b Mon Sep 17 00:00:00 2001 From: Darren Zal Date: Fri, 29 Dec 2023 16:53:21 -0700 Subject: [PATCH] changes for OriginTrail --- src/components/chat/chat.svelte | 209 ++++++++------------------------ 1 file changed, 48 insertions(+), 161 deletions(-) diff --git a/src/components/chat/chat.svelte b/src/components/chat/chat.svelte index 010811f..0492586 100644 --- a/src/components/chat/chat.svelte +++ b/src/components/chat/chat.svelte @@ -2,152 +2,19 @@ import { writable } from 'svelte/store'; import axios from 'axios'; - const jsonData = { - "OtherCompanies": [ - { - "@context": "http://schema.org/", - "@type": "Organization", - "@id": "http://example.org/companies/BrightFutures", - "name": "Bright Futures Ltd.", - "industry": "wind energy", - "investor": {"@id": "http://example.org/investors/PioneerInvestments"}, - "foundedYear": 2008, - "location": "Hamburg, Germany" - }, - { - "@context": "http://schema.org/", - "@type": "Organization", - "@id": "http://example.org/companies/SunPowerTech", - "name": "Sun Power Technologies", - "industry": "photovoltaic energy", - "investor": {"@id": "http://example.org/investors/NatureCapital"}, - "foundedYear": 2011, - "location": "Zurich, Switzerland" - }, - { - "@context": "http://schema.org/", - "@type": "Organization", - "@id": "http://example.org/companies/WaveEnergySolutions", - "name": "Wave Energy Solutions", - "industry": "tidal energy", - "investor": {"@id": "http://example.org/investors/InnovativeFunds"}, - "foundedYear": 2014, - "location": "Dublin, Ireland" - }, - { - "@context": "http://schema.org/", - "@type": "Organization", - "@id": "http://example.org/companies/CleanTech", - "name": "Clean Tech Innovations", - "industry": "biomass energy", - "products": [ - {"@id": "http://example.org/products/EcoFriendlyGadget1"}, - {"@id": "http://example.org/products/SustainableWidget1"} - ], - "impactScore": 90, - "issuedBy": {"@id": "http://example.org/certifiers/EcoCertGlobal"}, - "investor": {"@id": "http://example.org/investors/GreenFunding"}, - "foundedYear": 2009, - "location": "Vienna, Austria" - } - ], - "Investors": [ - { - "@context": "http://schema.org/", - "@type": "Person", - "@id": "http://example.org/investors/PioneerInvestments", - "name": "Pioneer Investments", - "investedIn": [ - {"@id": "http://example.org/companies/BrightFutures"}, - {"@id": "http://example.org/companies/CleanTech"} - ], - "location": "London, UK" - }, - { - "@context": "http://schema.org/", - "@type": "Person", - "@id": "http://example.org/investors/NatureCapital", - "name": "Nature Capital Group", - "investedIn": [ - {"@id": "http://example.org/companies/SunPowerTech"} - ], - "location": "Paris, France" - }, - { - "@context": "http://schema.org/", - "@type": "Person", - "@id": "http://example.org/investors/InnovativeFunds", - "name": "Innovative Investment Funds", - "investedIn": [ - {"@id": "http://example.org/companies/WaveEnergySolutions"} - ], - "location": "New York, USA" - }, - { - "@context": "http://schema.org/", - "@type": "Person", - "@id": "http://example.org/investors/GreenFunding", - "name": "Green Funding LLC", - "investedIn": [ - {"@id": "http://example.org/companies/CleanTech"}, - {"@id": "http://example.org/companies/BrightFutures"} - ], - "location": "San Francisco, USA" - } - ], - "Products": [ - { - "@context": "http://schema.org/", - "@type": "Product", - "@id": "http://example.org/products/EcoFriendlyGadget1", - "name": "Eco-Friendly Gadget 1", - "launchYear": 2020, - "category": "Eco-Friendly Home", - "manufacturer": {"@id": "http://example.org/companies/CleanTech"} - }, - { - "@context": "http://schema.org/", - "@type": "Product", - "@id": "http://example.org/products/SustainableWidget1", - "name": "Sustainable Widget 1", - "launchYear": 2021, - "category": "Sustainable Tech", - "manufacturer": {"@id": "http://example.org/companies/CleanTech"} - } - ], - "CertifyingOrganizations": [ - { - "@context": "http://schema.org/", - "@type": "Organization", - "@id": "http://example.org/certifiers/EcoCertGlobal", - "name": "Eco Certification Global", - "industry": "Sustainability Certification", - "location": "Brussels, Belgium" - } - ] - - }; - - const jsonDataString = JSON.stringify(jsonData, null, 2); // Indented with 2 spaces for formatting - - const messages = writable([]); let newMessage = ''; let isLoading = writable(false); // To track loading state - async function processSparqlResponse() { + async function processResponse() { try { - const sparqlResponse = await axios.post('https://myseelia.life/generate_query', { question: newMessage }); - console.log("SPARQL Response:", sparqlResponse.data); + const response = await axios.post('https://myseelia.life/query', { question: newMessage }); + console.log("Response:", response.data); - const sparqlResult = sparqlResponse.data.sparql_query; - const formattedSparqlResult = sparqlResult.map(item => { - return Object.entries(item).map(([key, value]) => `${key}: ${value}`).join(', '); - }).join('; '); - messages.update(m => [...m, `SPARQL Result: ${formattedSparqlResult}`]); + messages.update(m => [...m, `Result: ${response.data.result}`]); } catch (error) { - console.error("Error processing SPARQL query:", error); - messages.update(m => [...m, `Error: Could not process SPARQL query.`]); + console.error("Error processing prompt:", error); + messages.update(m => [...m, `Error: Could not process prompt.`]); } } @@ -165,17 +32,25 @@ } async function sendMessage() { - if (newMessage.trim() !== '') { - messages.update(m => [...m, `Query: ${newMessage}`]); - isLoading.set(true); + if (newMessage.trim() !== '') { + messages.update(m => [...m, `Query: ${newMessage}`]); + isLoading.set(true); - processSparqlResponse(); // Process SPARQL response independently - processEntityMatchResponse(); // Process Entity Match response independently + // Extract chat history for sending to the backend + let chatHistory = $messages.map(msg => { + return { role: msg.startsWith('Query:') ? 'user' : 'assistant', content: msg }; + }); - newMessage = ''; - isLoading.set(false); - } + // Send both the new message and the chat history + const response = await axios.post('https://myseelia.life/query', { question: newMessage, history: chatHistory }); + console.log("Response:", response.data); + + messages.update(m => [...m, `Result: ${response.data.result}`]); + + newMessage = ''; + isLoading.set(false); } +} function formatResultItem(item) { let formattedItem = []; @@ -194,16 +69,19 @@ sendMessage(); } } + + function getMessageType(message) { + if (message.startsWith('Query:')) return 'query'; + if (message.startsWith('SPARQL Result:')) return 'sparql-result'; + if (message.startsWith('Entity Matching Result:')) return 'entity-matching-result'; + return 'other'; + } -
-

Indexed DKG Data:

-
{jsonDataString}
-
{#each $messages as message} -
{message}
+
{message}
{/each}
@@ -212,18 +90,20 @@ bind:value={newMessage} on:keydown={handleKeydown} rows="3" - disabled={$isLoading} + disabled={$isLoading} > - + {#if $isLoading} -
Loading...
+
Processing...
{/if}