Giter Club home page Giter Club logo

Comments (6)

hughcrt avatar hughcrt commented on June 12, 2024 1

Hi,

We need to update our SDK and examples to explain how to make it work with Next.js.
In the meantine, here is an example that works well with Next.js App router and Lunary:

lunary.init({ appId: "..." });
const openai = new OpenAI({
  apiKey: "sk-...",
});

monitorOpenAI(openai);
export const runtime = "edge";

export async function GET() {
  const result = await openai.chat.completions.create({
    model: "gpt-3.5-turbo",
    temperature: 0.9,
    stream: true,
    messages: [
      { role: "system", content: "You are an helpful assistant" },
      { role: "user", content: "Print a random string" },
    ],
  });

  const stream = iteratorToStream(result);
  return new Response(stream);
}


function iteratorToStream(iterator: any) {
  const encoder = new TextEncoder();
  return new ReadableStream({
    async pull(controller) {
      try {
        const { value, done } = await iterator.next();
        if (done) return controller.close();
        const bytes = encoder.encode(JSON.stringify(value) + "\n");
        controller.enqueue(bytes);
      } catch (error) {
        controller.error(error);
      }
    },
  });
}

from lunary.

gardner avatar gardner commented on June 12, 2024

This is still an issue:

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
})
const res = await openai.chat.completions.create({
  model: 'gpt-4-0125-preview',
  user: userId,
  messages,
  temperature: 0.7,
  stream: true
})

res is of type Stream<OpenAI.Chat.Completions.ChatCompletionChunk>

const openai = monitorOpenAI(
  new OpenAI({
    apiKey: process.env.OPENAI_API_KEY
  })
)
const res = await openai.chat.completions.create({
  model: 'gpt-4-0125-preview',
  user: userId,
  messages,
  temperature: 0.7,
  stream: true
})

res is of type OpenAI.Chat.Completions.ChatCompletion

from lunary.

gardner avatar gardner commented on June 12, 2024

As a workaround:

It seems to work in spite of the type error. Adding @ts-ignore allows the build stage to pass and then the code operates as expected. e.g.

// @ts-ignore
const stream = OpenAIStream(res, {
  async onCompletion(completion) {

It looks like there are some pretty gnarly type-gymnastics being done inside the openai npm module.

from lunary.

linear avatar linear commented on June 12, 2024

LLM-769 Server-side error with vercel 'ai' npm package

from lunary.

BigStar-2024 avatar BigStar-2024 commented on June 12, 2024

Can I contribute to this?

from lunary.

hughcrt avatar hughcrt commented on June 12, 2024

Hi @BigStar-2024, yes you can

from lunary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.