gdb-p2p
Version:
Distributed graph database (GraphDB/GDB) for real-time data modeling, scalable storage, and efficient querying of complex relationships.
13 lines (10 loc) • 1.61 kB
JavaScript
function G(w){const O=w.map;return w.map=async function(...I){const y=await O.call(this,...I),j=I.find((b)=>b&&typeof b==="object")||{},{prompt:k}=j;if(k)y.results=await E(y.results,k),console.log(y.results);return y},w}async function E(w,O,I=!1){const y=w.map((b)=>b.value),j=`
${O}
Input Data:
${JSON.stringify(y)}
Instructions:
- Process the input data as specified in the prompt.
- Modify only the 'body' field of each object and return the same object structure.
- Do not create new fields or objects.
- Return the modified data as a JSON array of objects with the same structure as the input.
`,k=await F(j);if(Array.isArray(k)&&k.length===w.length){const b=w.map((z,C)=>({...z,value:k[C]}));if(I)await saveData(b);return b}else return console.warn("The AI response does not match the expected format. Returning original results."),w}async function F(w){try{const I=await fetch("https://text.pollinations.ai/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({messages:[{role:"system",content:"You are an AI assistant specialized in processing structured data objects."},{role:"user",content:w}],model:"openai",seed:Math.floor(Math.random()*1000),jsonMode:!0})});if(!I.ok)throw new Error(`Error calling the Pollinations API: ${I.statusText}`);const y=await I.json(),j=y.data||y;if(!Array.isArray(j))throw console.error("The API response does not contain a valid array:",y),new Error("Invalid response format.");return j}catch(O){throw console.error("Error processing the prompt with the Pollinations API:",O.message),O}}export{G as withAI};