Edits history of script submission #15831 for ' Generate an answer from search results (exa)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Exa = {
      apiKey: string;
    };
    /**
     * Generate an answer from search results
     * Performs a search based on the query and generates either a direct answer or a detailed summary with citations, depending on the query type. 
    
     */
    export async function main(
      auth: Exa,
      body: {
        query: string;
        stream?: false | true;
        text?: false | true;
        model?: "exa" | "exa-pro";
      },
    ) {
      const url = new URL(`https://api.exa.ai/answer`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "X-API-Key": auth.apiKey,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago