HARPA.AI
LIBRARYAPIGUIDESAI COMMANDSBLOG

👤  4o-search REQUEST test/demo (public)

Replace YOUR-API-KEY in REQUEST headers field with actual API key; adjust web_search_options (context size, location) in body content field

Created by get_back_to_work! C
Updated on Mar 22, 12:53
Installed 1 time
RUNS JS CODESENDS HTTP REQUESTS

How to Use

IMPORT COMMAND

Content

- type: clear
- type: ask
  param: query
  message: 'Please provide query:'
  options: null
  default: ''
  vision:
    enabled: false
    mode: area
    send: true
    hint: ''
- type: request
  url: api.openai.com/v1/chat/completions
  method: post
  headers:
    Authorization: Bearer YOUR-API-KEY
    Content-Type: application/json
  body:
    model: gpt-4o-search-preview
    messages:
      - role: user
        content:
          - type: text
            text: '{{query}}'
    response_format:
      type: text
    web_search_options:
      search_context_size: high
      user_location:
        type: approximate
        approximate:
          country: US
    store: true
  auth:
    enabled: false
    username: ''
    password: ''
  param: response
  bodyInvalid: false
  headersInvalid: false
- type: say
  message: |-
    RAW response below

    ---

    {{response}}
- code: >-
    // Access the response object passed in via args

    const { response, citations } = args;

    console.log("Full response:", response);


    // Parse the JSON body from the response

    const data = response.body;

    console.log("Parsed data:", data);


    // Extract the assistant's message text

    const assistantMessage = data?.choices?.[0]?.message?.content || "No content
    found";

    console.log("Assistant says:", assistantMessage);


    // First try using citations passed via args; if empty, extract from the
    response

    let urlCitations = citations && citations.length ? citations : (
      data?.choices?.[0]?.message?.annotations
        ?.filter(a => a.type === "url_citation")
        .map(a => {
          const { start_index, end_index, title, url } = a.url_citation;
          return { start_index, end_index, title, url };
        }) || []
    );


    // Function to clean up and normalize Markdown text

    const cleanMarkdown = (text) => {
      let cleaned = text.trim();
      // Normalize multiple blank lines to a single double newline
      cleaned = cleaned.replace(/(\n\s*\n)+/g, "\n\n");
      return cleaned;
    };


    const assistantMessageMarkdown = cleanMarkdown(assistantMessage);


    // Function to generate a Markdown citation list

    const generateCitationList = (citationsArr) => {
      if (!citationsArr || citationsArr.length === 0) return '';
      return citationsArr
        .map(c => `- [${c.title}](${c.url})`)
        .join('\n');
    };


    const citationsMarkdown = generateCitationList(urlCitations);


    // Combine the cleaned assistant message with a references section in
    Markdown

    const finalMarkdown = `${assistantMessageMarkdown}\n\n##
    References\n\n${citationsMarkdown}`;


    // Generate an HTML version of the response

    const finalHtml = `
      <div>
        <p>${assistantMessage.replace(/\n/g, "<br>")}</p>
        <h4>References</h4>
        <ul>
          ${urlCitations.map(c => `<li><a href="${c.url}" target="_blank">${c.title}</a></li>`).join("\n")}
        </ul>
      </div>
    `;


    // Return all useful components for further processing or display

    return {
      fullData: data,
      text: assistantMessage,
      markdown: finalMarkdown,
      citations: urlCitations,
      html: finalHtml
    };
  label: JS_PARSED
  type: js
  args: response
  param: JS_parsed
  timeout: 15000
  onFailure: ''
- message: |-
    JS_parsed.markdown 

    ---

    {{JS_parsed.markdown}}
  type: say
- message: |-
    JS_parsed.citations 

    ---

    {{JS_parsed.citations}}
  type: say
- message: |-
    JS_parsed.html 

    ---

    {{JS_parsed.html}}
  type: say
Notice: Please read before using

This automation command is created by a community member. HARPA AI team does not audit community commands.

Please review the command carefully and only install if you trust the creator.

Contact us
HomeUse CasesGuidesPrivacy PolicyTerms of Service
CAN WE STORE COOKIES?
Our website uses cookies for the purposes of accessibility and security. They also allow us to gather statistics in order to improve the website for you. More info: Privacy Policy