MCPcopy Index your code
hub / github.com/microsoft/TypeChat / createFetchLanguageModel

Function createFetchLanguageModel

typescript/src/model.ts:162–203  ·  view source on GitHub ↗

* Common OpenAI REST API endpoint encapsulation using the fetch API.

(url: string, headers: object, defaultParams: object)

Source from the content-addressed store, hash-verified

160 * Common OpenAI REST API endpoint encapsulation using the fetch API.
161 */
162function createFetchLanguageModel(url: string, headers: object, defaultParams: object) {
163 const model: TypeChatLanguageModel = {
164 complete
165 };
166 return model;
167
168 async function complete(prompt: string | PromptSection[]) {
169 let retryCount = 0;
170 const retryMaxAttempts = model.retryMaxAttempts ?? 3;
171 const retryPauseMs = model.retryPauseMs ?? 1000;
172 const messages = typeof prompt === "string" ? [{ role: "user", content: prompt }] : prompt;
173 while (true) {
174 const options = {
175 method: "POST",
176 body: JSON.stringify({
177 ...defaultParams,
178 messages,
179 temperature: 0,
180 n: 1
181 }),
182 headers: {
183 "content-type": "application/json",
184 ...headers
185 }
186 }
187 const response = await fetch(url, options);
188 if (response.ok) {
189 const json = await response.json() as { choices: { message: PromptSection }[] };
190 if (typeof json.choices[0].message.content === "string") {
191 return success(json.choices[0].message.content ?? "");
192 } else {
193 return error(`REST API unexpected response format: ${JSON.stringify(json.choices[0].message.content)}`);
194 }
195 }
196 if (!isTransientHttpError(response.status) || retryCount >= retryMaxAttempts) {
197 return error(`REST API error ${response.status}: ${response.statusText}`);
198 }
199 await sleep(getRetryDelayMs(response, retryPauseMs, retryPauseMs * retryMaxAttempts));
200 retryCount++;
201 }
202 }
203}
204
205/**
206 * OpenAI Responses API endpoint encapsulation using the fetch API.

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected