cfortuner / promptable

Build LLM apps in Typescript/Javascript. 🧑‍💻 🧑‍💻 🧑‍💻 🚀 🚀 🚀
https://docs-promptable.vercel.app
MIT License
1.77k stars 118 forks source link

generate() doesn't resolve promise #34

Closed lenniecottrell closed 1 year ago

lenniecottrell commented 1 year ago

I'm pretty sure this code is correct syntactically, but the promise never resolves and the console prints Promise { <pending> }

const text = "Write a haiku about sunflowers"
const getResponse = async (prompt: string) => {
    try {
        const response = await provider.generate(prompt);
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}

getResponse(text);

I've also tried to change the bottom to

const answer = await getResponse(text);

console.log(answer);

I've also tried the exact example from the Quickstart and it still just prints Promise { <pending> }

const provider = new p.OpenAI(apiKey);

const writePoemPrompt = new p.Prompt(
    "Write a poem about {{topic}}:".trim(),
    [
      "topic",
    ]
);

const text = writePoemPrompt.format({
    topic: "hi",
});

const response = await provider.generate(text);
console.log(response)
cfortuner commented 1 year ago

hey! let me try this and get back to you

cfortuner commented 1 year ago

This code works

const dotenv = require("dotenv");
dotenv.config();
const promptable = require("promptable");

const apiKey = process.env.OPENAI_API_KEY || "missing";

const provider = new promptable.OpenAI(apiKey);

const text = "Write a haiku about sunflowers";
const getResponse = async (prompt) => {
  try {
    const response = await provider.generate(prompt);
    console.log(response);
  } catch (error) {
    console.error(error);
  }
};

getResponse(text);

Assuming

  1. you have this in a index.js file
  2. you've installed the dependencies (promptable and dotenv)
  3. you have a valid .env file with OPENAI_API_KEY

node index.js

outputs something like this

colin@MacBook-Pro test % node index.js

Golden petals sway
In a summer's gentle breeze
A field of sunflowers
cfortuner commented 1 year ago

Closing for now, feel free to open it again if you think there is something wrong!

cfortuner commented 1 year ago

Another example that works

dotenv.config();
const promptable = require("promptable");

const apiKey = process.env.OPENAI_API_KEY || "missing";

const provider = new promptable.OpenAI(apiKey);

const getResponse = async () => {
  const writePoemPrompt = new promptable.Prompt(
    "Write a poem about {{topic}}:".trim(),
    ["topic"]
  );

  const text = writePoemPrompt.format({
    topic: "hi",
  });

  const response = await provider.generate(text);
  console.log(response);
};

getResponse();
lenniecottrell commented 1 year ago

Thanks for the response, your sample code worked for me in a vanilla JS file - I have some typescript stuff to work out it seems...