Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseState for updating mainThread pilot #26

Open
gdcho opened this issue May 15, 2023 · 0 comments
Open

UseState for updating mainThread pilot #26

gdcho opened this issue May 15, 2023 · 0 comments

Comments

@gdcho
Copy link
Member

gdcho commented May 15, 2023

  1. Getting form field values using formRef:
const title = formRef.current.title.value;
const genre = formRef.current.genre.value;
const numberOfCharacters = formRef.current.numberOfCharacters.value;
'
'
'
<form ref={formRef} onSubmit={handleMainThreadSubmit} className="w-full">
  1. OpenAI text completion call
const endpoint = "https://api.openai.com/v1/completions";

    const options = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`,
      },
      body: JSON.stringify({
        model: "text-davinci-003",
        prompt: `Given title: ${title} , genre: ${genre} , and number of main characters: ${numberOfCharacters} , write a paragraph of only three sentences to start the story.`,
        temperature: 0.3,
        max_tokens: 100,
      }),
    };    
  1. Using UseState to update mainThread pilot text area
    First, collect the response and the text completion value
const response = await fetch(endpoint, options);

    const { choices, error } = await response.json();

    const body = choices[0].text;

Then update mainThread pilot

if (!error) {
      mainThread.pilot = body;
      setPilot(body);
    }

Make sure:

<textarea
              name="pilot"
              id="message"
              rows="15"
              className="mb-5 block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500"
              placeholder="Write your thoughts here..."
              value={pilot}
              onChange={(e) => setPilot(e.target.value)} // Updates the pilot state when the textarea value changes.
            ></textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant