@langchain/openai
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -253,3 +253,3 @@ import { OpenAI as OpenAIClient } from "openai"; | ||
getEnvironmentVariable("OPENAI_ORGANIZATION"); | ||
this.modelName = fields?.modelName ?? this.modelName; | ||
this.modelName = fields?.model ?? fields?.modelName ?? this.modelName; | ||
this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages; | ||
@@ -256,0 +256,0 @@ this.modelKwargs = fields?.modelKwargs ?? {}; |
@@ -75,2 +75,11 @@ import { OpenAI as OpenAIClient } from "openai"; | ||
!model?.includes("-instruct")) { | ||
console.warn([ | ||
`Your chosen OpenAI model, "${model}", is a chat model and not a text-in/text-out LLM.`, | ||
`Passing it into the "OpenAI" class is deprecated and only permitted for backwards-compatibility. You may experience odd behavior.`, | ||
`Please use the "ChatOpenAI" class instead.`, | ||
"", | ||
`See this page for more information:`, | ||
"|", | ||
`└> https://js.langchain.com/v0.2/docs/integrations/chat/openai`, | ||
].join("\n")); | ||
// eslint-disable-next-line no-constructor-return | ||
@@ -77,0 +86,0 @@ return new OpenAIChat(fields, configuration); |
import { OpenAI as OpenAIClient } from "openai"; | ||
import { Tool, ToolParams } from "@langchain/core/tools"; | ||
import { MessageContentComplex } from "@langchain/core/messages"; | ||
/** | ||
@@ -93,4 +94,15 @@ * An interface for the Dall-E API Wrapper. | ||
constructor(fields?: DallEAPIWrapperParams); | ||
/** | ||
* Processes the API response if multiple images are generated. | ||
* Returns a list of MessageContentImageUrl objects. If the response | ||
* format is `url`, then the `image_url` field will contain the URL. | ||
* If it is `b64_json`, then the `image_url` field will contain an object | ||
* with a `url` field with the base64 encoded image. | ||
* | ||
* @param {OpenAIClient.Images.ImagesResponse[]} response The API response | ||
* @returns {MessageContentImageUrl[]} | ||
*/ | ||
private processMultipleGeneratedUrls; | ||
/** @ignore */ | ||
_call(input: string): Promise<string>; | ||
_call(input: string): Promise<string | MessageContentComplex[]>; | ||
} |
@@ -100,8 +100,60 @@ /* eslint-disable no-param-reassign */ | ||
} | ||
/** | ||
* Processes the API response if multiple images are generated. | ||
* Returns a list of MessageContentImageUrl objects. If the response | ||
* format is `url`, then the `image_url` field will contain the URL. | ||
* If it is `b64_json`, then the `image_url` field will contain an object | ||
* with a `url` field with the base64 encoded image. | ||
* | ||
* @param {OpenAIClient.Images.ImagesResponse[]} response The API response | ||
* @returns {MessageContentImageUrl[]} | ||
*/ | ||
processMultipleGeneratedUrls(response) { | ||
if (this.dallEResponseFormat === "url") { | ||
return response.flatMap((res) => { | ||
const imageUrlContent = res.data | ||
.flatMap((item) => { | ||
if (!item.url) | ||
return []; | ||
return { | ||
type: "image_url", | ||
image_url: item.url, | ||
}; | ||
}) | ||
.filter((item) => item !== undefined && | ||
item.type === "image_url" && | ||
typeof item.image_url === "string" && | ||
item.image_url !== undefined); | ||
return imageUrlContent; | ||
}); | ||
} | ||
else { | ||
return response.flatMap((res) => { | ||
const b64Content = res.data | ||
.flatMap((item) => { | ||
if (!item.b64_json) | ||
return []; | ||
return { | ||
type: "image_url", | ||
image_url: { | ||
url: item.b64_json, | ||
}, | ||
}; | ||
}) | ||
.filter((item) => item !== undefined && | ||
item.type === "image_url" && | ||
typeof item.image_url === "object" && | ||
"url" in item.image_url && | ||
typeof item.image_url.url === "string" && | ||
item.image_url.url !== undefined); | ||
return b64Content; | ||
}); | ||
} | ||
} | ||
/** @ignore */ | ||
async _call(input) { | ||
const response = await this.client.images.generate({ | ||
const generateImageFields = { | ||
model: this.model, | ||
prompt: input, | ||
n: this.n, | ||
n: 1, | ||
size: this.size, | ||
@@ -112,3 +164,8 @@ response_format: this.dallEResponseFormat, | ||
user: this.user, | ||
}); | ||
}; | ||
if (this.n > 1) { | ||
const results = await Promise.all(Array.from({ length: this.n }).map(() => this.client.images.generate(generateImageFields))); | ||
return this.processMultipleGeneratedUrls(results); | ||
} | ||
const response = await this.client.images.generate(generateImageFields); | ||
let data = ""; | ||
@@ -115,0 +172,0 @@ if (this.dallEResponseFormat === "url") { |
@@ -17,1 +17,42 @@ import { test, expect } from "@jest/globals"; | ||
}); | ||
test.skip("Dalle returns multiple image URLs if n > 1", async () => { | ||
const dalle = new DallEAPIWrapper({ | ||
n: 2, | ||
}); | ||
const res = await dalle.invoke("A painting of a cat"); | ||
expect(res).toBeDefined(); | ||
expect(res).toBeInstanceOf(Array); | ||
if (!Array.isArray(res)) | ||
return; | ||
expect(res).toHaveLength(2); | ||
// The types for each should be `image_url` with an `image_url` field containing the URL | ||
expect(res[0].type).toBe("image_url"); | ||
expect(res[1].type).toBe("image_url"); | ||
expect(res[0]).toHaveProperty("image_url"); | ||
expect(res[1]).toHaveProperty("image_url"); | ||
expect(res[0].image_url.startsWith("https://")).toBe(true); | ||
expect(res[1].image_url.startsWith("https://")).toBe(true); | ||
}); | ||
test.skip("Dalle returns multiple base64 image strings if n > 1", async () => { | ||
const dalle = new DallEAPIWrapper({ | ||
n: 2, | ||
dallEResponseFormat: "b64_json", | ||
}); | ||
const res = await dalle.invoke("A painting of a cat"); | ||
expect(res).toBeDefined(); | ||
expect(res).toBeInstanceOf(Array); | ||
if (!Array.isArray(res)) | ||
return; | ||
expect(res).toHaveLength(2); | ||
// The types for each should be `b64_json` with an `b64_json` field containing the URL | ||
expect(res[0].type).toBe("image_url"); | ||
expect(res[1].type).toBe("image_url"); | ||
expect(res[0]).toHaveProperty("image_url"); | ||
expect(res[1]).toHaveProperty("image_url"); | ||
expect(res[0].image_url).toHaveProperty("url"); | ||
expect(res[1].image_url).toHaveProperty("url"); | ||
expect(res[0].image_url.url).toBeDefined(); | ||
expect(res[1].image_url.url).toBeDefined(); | ||
expect(res[0].image_url.url).not.toBe(""); | ||
expect(res[1].image_url.url).not.toBe(""); | ||
}); |
{ | ||
"name": "@langchain/openai", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "OpenAI integrations for LangChain.js", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280297
7079