0

I have a simple form on my website with text input. We want to make a call to the OpenAI API to ask ChatGPT to find some similar companies based on a job description that a user pastes in the text box.

So far, we haven't been able to get the return data to work. It is correctly sending the job description data, but it is not able to list a list of companies. How can we fix it?

const form = document.querySelector('form');
const generateButton = document.querySelector('#generate-button');
const companiesOutput = document.querySelector('#output-companies');

function generateCampaign(event) {
  event.preventDefault();
  const jobDescription = document.querySelector('#job-description').value;

  fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apiKey}`
    },
    body: JSON.stringify({
      prompt: `Give me 20 top-tier VC backed startup companies in the same space as the company in this job description:\n\n ${jobDescription}`,
      max_tokens: 50,
      temperature: 0.7
    })
  })
  .then(response => response.json())
  .then(data => {
    const companiesList = data.choices[0].text;
    companiesOutput.innerHTML = `<li>${companiesList}</li>`;
  })
  .catch(error => console.error(error));
};

form.addEventListener('submit', generateCampaign);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Does this answer your question? [OpenAI API error: "Cannot specify both model and engine"](https://stackoverflow.com/questions/75176667/openai-api-error-cannot-specify-both-model-and-engine) All Engines endpoints are deprecated, see the answer to the question I commented. – Rok Benko Mar 13 '23 at 09:21
  • Nope im using javaascript – user21386362 Mar 13 '23 at 15:12

2 Answers2

0

You need to split the response text into separate company names and create a list item element for each one. So you need to change this line:

companiesOutput.innerHTML = `<li>${companiesList}</li>`;

and add the following code block instead:

const companies = companiesList.split(/\r?\n/);
companiesOutput.innerHTML = '';
companies.forEach(company => {
companiesOutput.innerHTML += `<li>${company.trim()}</li>`;
});
  • Doesnt seem to work, maybe its something else? Maybe im using incorrect API calls or the wrong library? I got it working so far as it will return a random bunch of sentences. I also made my project even simpler and added an input for company name, so im just trying to get it to print 20 companies similar to a user inputted company. So far still not getting accurate results. – user21386362 Mar 13 '23 at 21:31
0

I changed it to a try block like this and it worked:

try {
    // Construct the request body for company list generation
    const companyListRequestBody = {
      prompt: `List 20 top-tier US VC-backed startups in a similar industry as: ${companyName}.`,
      temperature: 0,
      top_p: 1,
      max_tokens: 150,
      model: "text-davinci-003"
    };

    console.log("Request Body:", companyListRequestBody);

    // Make the request to the OpenAI API to generate company list
    const companyListResponse = await fetch(endpointUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${apiKey}`
      },
      body: JSON.stringify(companyListRequestBody)
    });

    console.log("API Response:", companyListResponse);

    if (!companyListResponse.ok) {
      throw new Error(`API error: ${companyListResponse.status} ${companyListResponse.statusText}`);
    }

    // Parse the response and extract the generated company list
    const companyListJson = await companyListResponse.json();
    const companyList = companyListJson.choices[0].text;

    console.log("Parsed Response:", companyListJson);

    // Output the generated company list to the page
    companiesOutput.innerHTML = companyList || "No results found.";

  }