Skip to content

Commit

Permalink
bump version and other suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfrenken committed Sep 16, 2024
1 parent a62dba0 commit 2d41b66
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"dependencies": {
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-ai-sdk/foundation-models": "workspace:^",
"@langchain/core": "^0.2.30",
"@langchain/core": "0.3.1",
"zod-to-json-schema": "^3.23.2",
"@sap-cloud-sdk/util": "^3.20.0"
},
"peerDependencies": {
"@langchain/openai": "^0.2.8"
"@langchain/openai": "^0.3.0"
},
"peerDependenciesMeta": {
"@langchain/openai": {
Expand Down
5 changes: 4 additions & 1 deletion packages/langchain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { AzureOpenAiChatClient, AzureOpenAiEmbeddingClient } from './openai/index.js';
export {
AzureOpenAiChatClient,
AzureOpenAiEmbeddingClient
} from './openai/index.js';
export type {
OpenAiChatModelInput,
OpenAiEmbeddingInput,
Expand Down
6 changes: 5 additions & 1 deletion packages/langchain/src/openai/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { BaseMessage } from '@langchain/core/messages';
import type { ChatResult } from '@langchain/core/outputs';
import { AzureChatOpenAI, AzureOpenAI } from '@langchain/openai';
import { OpenAiChatClient as OpenAiChatClientBase } from '@sap-ai-sdk/foundation-models';
import { mapLangchainToAiClient, mapResponseToChatResult, toArrayOrUndefined } from './util.js';
import {
mapLangchainToAiClient,
mapResponseToChatResult,
toArrayOrUndefined
} from './util.js';
import type { OpenAiChatModelInput, OpenAiChatCallOptions } from './types.js';

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/langchain/src/openai/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { OpenAiEmbeddingInput } from './types.js';
* OpenAI GPT Language Model Wrapper to embed texts.
*/
export class AzureOpenAiEmbeddingClient extends AzureOpenAIEmbeddings {
private btpOpenAIClient: OpenAiEmbeddingClientBase;
private btpOpenAiClient: OpenAiEmbeddingClientBase;

constructor(fields: OpenAiEmbeddingInput) {
// overrides the apikey value as it is not applicable in BTP
super({ ...fields, apiKey: 'dummy', azureOpenAIApiKey: undefined });

this.btpOpenAIClient = new OpenAiEmbeddingClientBase({ ...fields });
this.btpOpenAiClient = new OpenAiEmbeddingClientBase({ ...fields });
}

override async embedDocuments(documents: string[]): Promise<number[][]> {
Expand Down Expand Up @@ -46,7 +46,7 @@ export class AzureOpenAiEmbeddingClient extends AzureOpenAIEmbeddings {
query: OpenAiEmbeddingParameters
): Promise<OpenAiEmbeddingOutput> {
return this.caller.callWithOptions({}, () =>
this.btpOpenAIClient.run(query)
this.btpOpenAiClient.run(query)
);
}
}
39 changes: 17 additions & 22 deletions packages/langchain/src/openai/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { OpenAiChatCallOptions } from './types.js';

/**
* Maps a LangChain {@link StructuredTool} to {@link OpenAiChatCompletionFunction}.
* @param tool - Base class for Tools that accept input of any shape defined by a Zod schema.
* @returns The OpenAI Chat Completion Function.
* @param tool - Base class for tools that accept input of any shape defined by a Zod schema.
* @returns The OpenAI chat completion function.
* @internal
*/
export function mapToolToOpenAiFunction(
Expand All @@ -36,27 +36,23 @@ export function mapToolToOpenAiFunction(

/**
* Maps a LangChain {@link StructuredTool} to {@link OpenAiChatCompletionTool}.
* @param tool - Base class for Tools that accept input of any shape defined by a Zod schema.
* @returns The OpenAI Chat Completion Tool.
* @param tool - Base class for tools that accept input of any shape defined by a Zod schema.
* @returns The OpenAI chat completion tool.
* @internal
*/
export function mapToolToOpenAiTool(
tool: StructuredTool
): OpenAiChatCompletionTool {
return {
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: zodToJsonSchema(tool.schema)
}
function: mapToolToOpenAiFunction(tool)
};
}

/**
* Maps a {@link BaseMessage} to OpenAI's Message Role.
* Maps a {@link BaseMessage} to OpenAI's message role.
* @param message - The message to map.
* @returns The OpenAI Message Role.
* @returns The OpenAI meessage Role.
* @internal
*/
export function mapBaseMessageToRole(
Expand All @@ -82,8 +78,8 @@ export function mapBaseMessageToRole(

/**
* Maps OpenAI messages to LangChain's {@link ChatResult}.
* @param res - The OpenAI Chat Completion Output.
* @returns The LangChain Chat Result.
* @param res - The OpenAI chat completion output.
* @returns The LangChain chat result.
* @internal
*/
export function mapResponseToChatResult(
Expand Down Expand Up @@ -124,9 +120,9 @@ export function mapResponseToChatResult(
}

/**
* Maps {@link BaseMessage} to OpenAI Messages.
* Maps {@link BaseMessage} to OpenAI messages.
* @param message - The message to map.
* @returns The OpenAI Chat Message.
* @returns The OpenAI chat Message.
* @internal
*/
export function mapBaseMessageToOpenAiChatMessage(
Expand All @@ -149,7 +145,7 @@ export function mapBaseMessageToOpenAiChatMessage(
* @returns The value as an array, undefined if the input is falsy, or the original array if input is already an array.
*/
export function toArrayOrUndefined<T>(value?: T | T[]): T[] | undefined {
if(value === undefined) {
if (value === undefined) {
return undefined;
}
return Array.isArray(value) ? value : [value];
Expand All @@ -164,16 +160,15 @@ export function toArrayOrUndefined<T>(value?: T | T[]): T[] | undefined {
export function isStructuredToolArray(
tools?: unknown[]
): tools is StructuredTool[] {
return (
tools !== undefined &&
tools.every(tool => Array.isArray((tool as StructuredTool).lc_namespace))
return !!tools?.every(tool =>
Array.isArray((tool as StructuredTool).lc_namespace)
);
}

/**
* Maps Langchain's input interface to our own client's input interface
* @param client The Langchain OpenAI client
* @param options The Langchain call options
* Maps LangChain's input interface to our own client's input interface
* @param client The LangChain OpenAI client
* @param options The LangChain call options
* @param messages The messages to be send
* @returns An AI SDK compatibile request
* @internal
Expand Down
84 changes: 63 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@sap-ai-sdk/foundation-models": "workspace:^",
"@sap-ai-sdk/orchestration": "workspace:^",
"@sap-ai-sdk/langchain": "workspace:^",
"@langchain/core": "^0.2.30",
"@langchain/core": "0.3.1",
"@types/express": "^4.17.21",
"express": "^4.21.0"
}
Expand Down
6 changes: 3 additions & 3 deletions sample-code/src/langchain-openai.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HumanMessage } from '@langchain/core/messages';
import { OpenAiChatClient, OpenAiEmbeddingClient } from '@sap-ai-sdk/langchain';
import { AzureOpenAiChatClient, AzureOpenAiEmbeddingClient } from '@sap-ai-sdk/langchain';

/**
* Ask GPT about the capital of France.
* @returns The answer from GPT.
*/
export async function generate(): Promise<string> {
const client = new OpenAiChatClient({ modelName: 'gpt-35-turbo' });
const client = new AzureOpenAiChatClient({ modelName: 'gpt-35-turbo' });
const response = await client.generate([
[new HumanMessage('What is the capital of France?')]
]);
Expand All @@ -18,7 +18,7 @@ export async function generate(): Promise<string> {
* @returns An embedding vector.
*/
export async function embedQuery(): Promise<number[]> {
const client = new OpenAiEmbeddingClient({
const client = new AzureOpenAiEmbeddingClient({
modelName: 'text-embedding-ada-002'
});
return client.embedQuery('Hello, world!');
Expand Down
4 changes: 2 additions & 2 deletions sample-code/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ app.get('/llm', async (req, res) => {
app.get('/embedding', async (req, res) => {
try {
const result = await computeEmbedding();
if (result.length === 0) {
if (!result.length) {
throw new Error('No embedding vector returned');
}
res.send('Number crunching success, got a nice vector.');
Expand Down Expand Up @@ -77,7 +77,7 @@ app.get('/langchain/chat', async (req, res) => {
app.get('/langchain/embedding', async (req, res) => {
try {
const result = await embedQuery();
if (result.length === 0) {
if (!result.length) {
throw new Error('No embedding vector returned');
}
res.send('Number crunching success, got a nice vector.');
Expand Down

0 comments on commit 2d41b66

Please sign in to comment.