Send Your First Model Call

At startup, your project now loads the provider you chose, that provider’s key, and an output limit, and model/settings.js checks them all before anything else runs. That’s everything you need for the step the tea importer has been waiting for: asking a model to draft a reply to a real customer email.

In this lesson, you’ll write a function that makes a model call to your provider with fetch(), use it to draft a reply to the late-parcel email from the shop’s inbox, and print the raw response that comes back. Start with what a model call actually sends over the network.

Know What a Model Call Sends

Behind every chat window you’ve typed into, there’s an HTTP request. A model call is an ordinary POST request to the provider’s API. Its body is JSON, and your key goes in a header so the provider knows which account to bill. You’ve sent requests like this with fetch() before, so the only new part is what goes inside.

The request body carries four things:

  • The model ID names the model that should generate the reply. Each provider offers several at different prices.
  • The instructions say who the model writes as and what you want from it. Providers also call them the system prompt.
  • The input is the text to work on: in this case, a customer’s email.
  • The output limit caps how many tokens the model may generate, so one call can’t produce a long, costly reply.

Why keep the instructions apart from the input instead of writing one long prompt? The instructions are your rules, and they stay the same for every email in the inbox. The input is data, and it changes with every call. When you keep them apart, the model can tell your rules from the data it works on, and it gives the instructions more weight.

Keeping them apart also lets your code send a new email without rebuilding the rest. The split doesn’t make the model obey you, though. An email can still talk a model into ignoring your instructions, and a later course deals with that. That’s all you need about it for now.

Remember from the first lesson that a model keeps no memory between calls. Each call sees only what its own request contains, so the shop’s name and the reply’s style go into the instructions every time.

Write the Request

Each provider names these fields differently and has its own endpoint, so the code that builds the request is provider-specific. Like model/settings.js, it belongs in the model/ folder. Create a file named first-call.js there, with a function, callModel(), that takes the instructions and the input, sends them, and returns the response’s JSON.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.