Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass message history with tool use to anthropic models #5385

Closed
5 tasks done
ronneldavis opened this issue May 15, 2024 · 6 comments
Closed
5 tasks done

Unable to pass message history with tool use to anthropic models #5385

ronneldavis opened this issue May 15, 2024 · 6 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@ronneldavis
Copy link

ronneldavis commented May 15, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import { ChatAnthropic } from "@langchain/anthropic";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
  AIMessage,
  HumanMessage,
  SystemMessage,
  ToolMessage,
} from "@langchain/core/messages";

const tools = [
    new DynamicStructuredTool({
      name: "create_chart",
      description: "Call this to create a chart based on a prompt",
      schema: z.object({
        prompt: z
          .string()
          .describe(
            "A detailed string that describes the chart you want to create"
          ),
        type: z
          .enum([
            "metric",
            "table",
            "bar chart",
            "line chart",
            "area chart",
            "pie chart",
            "line chart",
            "radar chart",
            "scatter plot chart",
            "funnel",
            "separator",
          ])
          .describe("A type of chart to create"),
      }),
      func: async ({ prompt, type }: { prompt: string; type: string }) => {
        let lastCodeMessage = [
          ...messages.filter((a) => a.role === "tool"),
        ].pop();
        let existingCode: undefined | string = undefined;
        if (lastCodeMessage && lastCodeMessage.content) {
          existingCode = lastCodeMessage.content
            .split("```json")[0]
            .replace("```python", "")
            .replace("```", "");
        }
        let { data, code } = await writeCode({
          params: params,
          datasources: datasources,
          team: team,
          prompt,
          type,
          existingCode,
        });
        return (
          "```python\n" +
          code +
          "\n```\n```json\n" +
          JSON.stringify(data) +
          "\n```\n"
        );
      },
    }),
  ];

const anthropic = new ChatAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  model: "claude-3-haiku-20240307",
  temperature: 0.1,
});

  const prompt = ChatPromptTemplate.fromMessages([
    new SystemMessage(getAgentPrompt(datasources)),
    ["placeholder", "{chat_history}"],
    ["placeholder", "{agent_scratchpad}"],
  ]);

  const agent = await createToolCallingAgent({
    llm: anthropic,
    tools: tools,
    prompt,
  });

  const executor = new AgentExecutor({
    agent,
    tools,
    returnIntermediateSteps: true,
  });
  
  const messages = [
  {
    "role": "user",
    "content": "Create a bar chart showing the total orders month over month. Show the month on the x axis and the number of orders on the y-axis."
  },
  {
    "role": "assistant",
    "content": null,
    "tool_calls": [
      {
        "id": "toolu_01Fm68gufznApq1SRjauLM7n",
        "type": "function",
        "function": {
          "name": "create_chart",
          "arguments": "{\"prompt\":\"Create a bar chart showing the total orders month over month. Show the month on the x axis and the number of orders on the y-axis.\",\"type\":\"bar chart\"}"
        }
      }
    ]
  },
  {
    "role": "tool",
    "content": "```python\nimport pandas as pd\nimport numpy as np\n\ndef convertNum(x):\n    try:\n        return pd.to_numeric(x)\n    except:\n        return np.nan\n\ndef main():\n    orders_df = pd.read_csv('/tmp/b5c61910-b91c-4d37-a6a8-219e35f15833.csv', usecols=['Order ID', 'Order Date'])\n    orders_df['Order Date'] = pd.to_datetime(orders_df['Order Date'], format='%Y-%m-%d')\n    orders_df['Month'] = orders_df['Order Date'].dt.to_period('M')\n    \n    monthly_orders = orders_df.groupby('Month').size().reset_index(name='Total Orders')\n    monthly_orders['Month'] = monthly_orders['Month'].astype(str)\n    \n    chart = {\n        'type': 'bar',\n        'data': {\n            'labels': monthly_orders['Month'].tolist(),\n            'datasets': [{\n                'label': 'Total Orders',\n                'data': monthly_orders['Total Orders'].tolist(),\n                'backgroundColor': 'rgba(75, 192, 192, 0.2)',\n                'borderColor': 'rgba(75, 192, 192, 1)',\n                'borderWidth': 1\n            }]\n        },\n        'options': {\n            'responsive': True,\n            'maintainAspectRatio': False,\n            'plugins': {\n                'title': {\n                    'display': True,\n                    'text': 'Total Orders Month Over Month'\n                },\n                'subtitle': {\n                    'display': False\n                },\n                'datalabels': {\n                    'display': False\n                }\n            },\n            'scales': {\n                'x': {\n                    'stacked': False,\n                    'title': {\n                        'display': True,\n                        'text': 'Month'\n                    }\n                },\n                'y': {\n                    'stacked': False,\n                    'title': {\n                        'display': True,\n                        'text': 'Number of Orders'\n                    }\n                }\n            }\n        }\n    }\n    \n    return chart\n```\n```json\n{\"type\":\"bar\",\"data\":{\"labels\":[\"2018-01\",\"2018-02\",\"2018-03\",\"2018-04\",\"2018-05\",\"2018-06\",\"2018-07\",\"2018-08\",\"2018-09\",\"2018-10\",\"2018-11\",\"2018-12\"],\"datasets\":[{\"label\":\"Total Orders\",\"data\":[155,107,238,203,242,245,226,218,459,298,459,462],\"backgroundColor\":\"rgba(75, 192, 192, 0.2)\",\"borderColor\":\"rgba(75, 192, 192, 1)\",\"borderWidth\":1}]},\"options\":{\"responsive\":true,\"maintainAspectRatio\":false,\"plugins\":{\"title\":{\"display\":true,\"text\":\"Total Orders Month Over Month\"},\"subtitle\":{\"display\":false},\"datalabels\":{\"display\":false}},\"scales\":{\"x\":{\"stacked\":false,\"title\":{\"display\":true,\"text\":\"Month\"}},\"y\":{\"stacked\":false,\"title\":{\"display\":true,\"text\":\"Number of Orders\"}}}}}\n```\n",
    "tool_call_id": "toolu_01Fm68gufznApq1SRjauLM7n"
  },
  {
    "role": "assistant",
    "content": "Insights:\n- The chart shows a clear trend of increasing orders over the year, with the highest number of orders in November and December.\n- There is a dip in orders in the early months of the year, likely due to seasonal factors.\n- The chart provides a good visual representation of the monthly order volume, which can help identify peak seasons and plan inventory and staffing accordingly."
  },
  {
    "role": "user",
    "content": "Could you add a trendline?"
  },
]

  const chat_history = messages.map((a) => {
    if (a.role === "user") return new HumanMessage(a.content);
    else if (a.role === "assistant") {
      if (a.tool_calls && a.tool_calls.length > 0) {
        return new AIMessage({
          content: a.content,
          additional_kwargs: {
            tool_calls: [
              {
                id: a.tool_calls[0].id,
                type: "function",
                function: {
                  name: a.tool_calls[0].function.name,
                  arguments: JSON.parse(a.tool_calls[0].function.arguments),
                },
              },
            ],
          },
        });
      } else {
        return new AIMessage(a.content);
      }
    } else if (a.role === "tool") {
      return new ToolMessage(a);
    }
  });

  const result = await executor.invoke({
    chat_history,
  });

Error Message and Stack Trace (if applicable)

TypeError: Cannot read properties of null (reading 'map')
at _formatContent (/app/node_modules/@langchain/anthropic/dist/chat_models.cjs:122:39)
at /app/node_modules/@langchain/anthropic/dist/chat_models.cjs:223:26
at Array.map (<anonymous>)
at _formatMessagesForAnthropic (/app/node_modules/@langchain/anthropic/dist/chat_models.cjs:173:52)
at ChatAnthropic._streamResponseChunks (/app/node_modules/@langchain/anthropic/dist/chat_models.cjs:460:35)
at _streamResponseChunks.next (<anonymous>)
at ChatAnthropic._streamIterator (/app/node_modules/@langchain/core/dist/language_models/chat_models.cjs:82:34)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ChatAnthropic.transform (/app/node_modules/@langchain/core/dist/runnables/base.cjs:382:9)
at async RunnableBinding.transform (/app/node_modules/@langchain/core/dist/runnables/base.cjs:769:9)
at async ToolCallingAgentOutputParser.transform (/app/node_modules/@langchain/core/dist/runnables/base.cjs:371:26)
at async RunnableSequence._streamIterator (/app/node_modules/@langchain/core/dist/runnables/base.cjs:1132:30)

Description

I am trying to use langchain for tool use. I save the previous run data to a database so that it can be used for future sessions. The code I have used works perfectly with openai models, however switching to anthropic fails everytime.

When creating a session for the first time and running it, it works fine, calls the tool and get the output. However with subsequent runs, I believe there is an issue when passing the history to the models, specifically when constructing an AIMessage() or a ToolMessage() and I am unable to find any documentation referring to any differences in arguments to pass for OpenAI vs Anthropic models.

System Info

yarn info langchain
yarn info v1.22.19
{
time: {
created: '2023-02-14T18:06:57.074Z',
'1.0.0': '2023-01-20T03:45:29.634Z',
modified: '2024-05-10T21:40:01.431Z',
'1.0.9': '2023-01-22T02:02:57.467Z',
'0.0.1': '2023-01-26T21:22:58.657Z',
'0.0.4': '2023-02-14T18:06:57.434Z',
'0.0.5': '2023-02-17T05:51:37.227Z',
'0.0.6': '2023-02-17T08:53:32.946Z',
'0.0.7': '2023-02-18T00:31:00.386Z',
'0.0.8': '2023-02-20T04:56:45.683Z',
'0.0.9': '2023-02-21T07:17:14.208Z',
'0.0.10': '2023-02-22T18:48:35.631Z',
'0.0.11': '2023-02-23T16:12:41.455Z',
'0.0.12': '2023-02-26T18:49:26.071Z',
'0.0.13': '2023-02-27T16:03:50.432Z',
'0.0.14': '2023-02-27T16:59:17.082Z',
'0.0.15': '2023-02-27T17:59:53.741Z',
'0.0.16': '2023-02-28T16:41:21.705Z',
'0.0.17': '2023-03-01T15:28:09.338Z',
'0.0.18': '2023-03-01T20:00:24.554Z',
'0.0.19': '2023-03-01T20:58:49.811Z',
'0.0.20': '2023-03-02T17:11:30.882Z',
'0.0.21': '2023-03-03T19:23:04.354Z',
'0.0.22': '2023-03-04T16:00:04.873Z',
'0.0.23': '2023-03-06T16:42:44.996Z',
'0.0.24': '2023-03-07T18:10:28.933Z',
'0.0.25': '2023-03-08T17:42:24.706Z',
'0.0.26': '2023-03-09T19:36:23.799Z',
'0.0.27': '2023-03-10T16:24:31.279Z',
'0.0.28': '2023-03-11T19:41:29.182Z',
'0.0.29': '2023-03-12T07:41:25.588Z',
'0.0.30': '2023-03-13T19:27:19.085Z',
'0.0.31': '2023-03-13T22:48:33.902Z',
'0.0.32': '2023-03-14T08:48:30.479Z',
'0.0.33': '2023-03-14T18:36:54.970Z',
'0.0.34': '2023-03-15T18:20:43.233Z',
'0.0.35': '2023-03-15T18:59:13.048Z',
'0.0.36': '2023-03-17T18:55:33.715Z',
'0.0.37': '2023-03-20T20:10:36.444Z',
'0.0.38': '2023-03-21T18:31:14.819Z',
'0.0.39': '2023-03-22T15:47:46.529Z',
'0.0.40': '2023-03-24T15:13:53.947Z',
'0.0.41': '2023-03-24T19:33:29.565Z',
'0.0.42': '2023-03-27T18:47:07.410Z',
'0.0.43': '2023-03-28T18:43:21.068Z',
'0.0.44': '2023-03-29T18:45:08.636Z',
'0.0.45': '2023-03-31T15:55:26.705Z',
'0.0.46': '2023-04-03T15:46:07.061Z',
'0.0.47': '2023-04-03T15:55:06.384Z',
'0.0.48': '2023-04-04T17:25:41.531Z',
'0.0.49-0': '2023-04-05T12:56:44.659Z',
'0.0.49-1': '2023-04-05T13:07:31.197Z',
'0.0.49': '2023-04-06T10:31:23.786Z',
'0.0.50': '2023-04-07T21:46:20.961Z',
'0.0.51': '2023-04-08T17:07:19.121Z',
'0.0.52-0': '2023-04-09T17:48:55.378Z',
'0.0.52': '2023-04-10T18:57:02.678Z',
'0.0.53': '2023-04-11T15:51:36.258Z',
'0.0.54': '2023-04-12T16:59:09.975Z',
'0.0.55': '2023-04-13T16:22:58.352Z',
'0.0.56': '2023-04-14T17:47:49.152Z',
'0.0.57': '2023-04-17T17:01:13.362Z',
'0.0.58': '2023-04-18T18:51:48.339Z',
'0.0.59': '2023-04-18T20:23:09.912Z',
'0.0.60': '2023-04-21T18:45:14.498Z',
'0.0.61': '2023-04-21T20:58:46.669Z',
'0.0.62': '2023-04-22T20:27:37.499Z',
'0.0.63': '2023-04-23T15:27:57.932Z',
'0.0.64': '2023-04-25T15:58:01.314Z',
'0.0.65': '2023-04-28T11:29:13.611Z',
'0.0.66': '2023-04-28T12:48:12.798Z',
'0.0.67': '2023-05-01T14:55:22.206Z',
'0.0.68': '2023-05-02T19:51:47.329Z',
'0.0.69': '2023-05-04T17:35:32.223Z',
'0.0.70': '2023-05-05T16:40:01.904Z',
'0.0.71': '2023-05-06T15:19:04.361Z',
'0.0.72': '2023-05-08T07:03:31.569Z',
'0.0.73': '2023-05-09T18:50:58.586Z',
'0.0.74': '2023-05-10T15:50:49.597Z',
'0.0.75': '2023-05-11T18:54:42.166Z',
'0.0.76': '2023-05-17T19:35:53.355Z',
'0.0.77': '2023-05-18T15:25:50.965Z',
'0.0.78': '2023-05-19T15:18:20.002Z',
'0.0.79': '2023-05-23T16:30:38.510Z',
'0.0.80': '2023-05-23T18:18:28.751Z',
'0.0.81': '2023-05-23T18:57:35.910Z',
'0.0.82': '2023-05-25T00:15:56.768Z',
'0.0.83': '2023-05-25T16:48:27.312Z',
'0.0.84': '2023-05-26T20:43:14.443Z',
'0.0.85': '2023-05-30T19:31:33.866Z',
'0.0.86': '2023-05-30T23:26:55.520Z',
'0.0.87': '2023-05-31T15:23:33.608Z',
'0.0.88': '2023-06-01T02:10:23.366Z',
'0.0.89': '2023-06-02T16:10:24.873Z',
'0.0.90': '2023-06-03T01:29:12.535Z',
'0.0.91': '2023-06-04T23:00:33.336Z',
'0.0.92': '2023-06-07T20:40:58.171Z',
'0.0.93': '2023-06-13T17:32:25.870Z',
'0.0.94': '2023-06-13T21:41:36.359Z',
'0.0.95': '2023-06-14T14:25:53.017Z',
'0.0.96': '2023-06-19T18:24:31.016Z',
'0.0.97': '2023-06-26T20:08:09.043Z',
'0.0.98': '2023-06-28T05:15:53.569Z',
'0.0.99': '2023-06-29T10:00:34.210Z',
'0.0.100': '2023-06-29T16:19:24.235Z',
'0.0.101': '2023-06-30T19:51:23.760Z',
'0.0.102': '2023-07-01T19:12:19.594Z',
'0.0.103': '2023-07-05T23:21:04.630Z',
'0.0.104': '2023-07-07T01:35:39.991Z',
'0.0.105': '2023-07-09T00:55:47.724Z',
'0.0.106': '2023-07-10T17:32:39.936Z',
'0.0.107': '2023-07-12T06:09:52.155Z',
'0.0.108': '2023-07-13T02:24:55.332Z',
'0.0.109': '2023-07-14T01:37:31.791Z',
'0.0.110': '2023-07-15T17:39:11.281Z',
'0.0.111': '2023-07-18T04:22:39.858Z',
'0.0.112': '2023-07-18T18:22:02.588Z',
'0.0.113': '2023-07-19T23:56:22.261Z',
'0.0.114': '2023-07-21T03:00:26.247Z',
'0.0.115': '2023-07-24T01:01:00.307Z',
'0.0.116': '2023-07-24T05:19:14.838Z',
'0.0.117': '2023-07-25T00:26:59.388Z',
'0.0.118': '2023-07-26T16:09:04.672Z',
'0.0.119': '2023-07-28T22:08:02.687Z',
'0.0.120': '2023-07-30T17:08:46.417Z',
'0.0.121': '2023-08-01T21:15:07.740Z',
'0.0.122': '2023-08-03T03:25:11.530Z',
'0.0.123': '2023-08-05T06:09:18.474Z',
'0.0.124': '2023-08-07T05:38:39.271Z',
'0.0.125': '2023-08-09T03:48:44.885Z',
'0.0.126': '2023-08-11T01:19:19.011Z',
'0.0.127': '2023-08-12T01:03:28.743Z',
'0.0.128': '2023-08-15T06:55:07.574Z',
'0.0.129': '2023-08-16T02:39:45.923Z',
'0.0.130': '2023-08-18T01:17:54.954Z',
'0.0.131': '2023-08-19T00:48:09.017Z',
'0.0.132': '2023-08-21T23:24:17.628Z',
'0.0.133': '2023-08-24T02:13:40.841Z',
'0.0.134': '2023-08-25T04:45:19.565Z',
'0.0.135': '2023-08-26T00:44:11.083Z',
'0.0.136': '2023-08-29T01:36:44.900Z',
'0.0.137': '2023-08-30T01:34:15.475Z',
'0.0.138': '2023-08-31T02:22:36.630Z',
'0.0.139': '2023-09-01T07:20:17.993Z',
'0.0.140': '2023-09-02T00:37:02.261Z',
'0.0.141': '2023-09-03T07:34:21.546Z',
'0.0.142': '2023-09-04T20:15:53.914Z',
'0.0.143': '2023-09-05T14:41:59.202Z',
'0.0.144': '2023-09-06T01:21:44.338Z',
'0.0.145': '2023-09-07T01:35:35.892Z',
'0.0.146': '2023-09-08T15:27:22.355Z',
'0.0.147': '2023-09-12T06:22:04.926Z',
'0.0.148': '2023-09-13T03:03:39.760Z',
'0.0.149': '2023-09-14T01:40:17.567Z',
'0.0.150': '2023-09-15T06:28:14.560Z',
'0.0.151': '2023-09-19T03:53:15.800Z',
'0.0.152': '2023-09-20T00:08:57.506Z',
'0.0.153': '2023-09-21T03:10:39.410Z',
'0.0.154': '2023-09-26T01:47:45.016Z',
'0.0.155': '2023-09-28T01:35:14.504Z',
'0.0.156': '2023-09-29T05:55:44.312Z',
'0.0.157': '2023-10-02T01:10:49.355Z',
'0.0.158': '2023-10-03T16:33:07.960Z',
'0.0.159': '2023-10-04T05:07:58.638Z',
'0.0.160': '2023-10-04T22:07:49.157Z',
'0.0.161': '2023-10-06T02:07:05.756Z',
'0.0.162': '2023-10-07T01:37:20.312Z',
'0.0.163': '2023-10-08T01:32:36.536Z',
'0.0.164': '2023-10-10T01:43:32.276Z',
'0.0.165': '2023-10-10T20:57:49.443Z',
'0.0.166': '2023-10-12T01:40:20.109Z',
'0.0.167': '2023-10-14T00:32:54.607Z',
'0.0.168': '2023-10-18T02:53:34.953Z',
'0.0.169': '2023-10-19T06:32:45.493Z',
'0.0.170': '2023-10-20T22:38:06.659Z',
'0.0.171': '2023-10-24T00:17:00.928Z',
'0.0.172': '2023-10-24T20:19:09.813Z',
'0.0.173': '2023-10-26T00:58:15.045Z',
'0.0.174': '2023-10-26T23:11:53.499Z',
'0.0.175': '2023-10-28T17:04:03.349Z',
'0.0.176': '2023-10-31T00:57:13.953Z',
'0.0.177': '2023-11-01T00:20:00.276Z',
'0.0.178': '2023-11-01T22:33:03.571Z',
'0.0.179': '2023-11-03T05:49:56.666Z',
'0.0.180': '2023-11-03T22:15:10.842Z',
'0.0.181': '2023-11-06T09:59:26.380Z',
'0.0.182-rc.0': '2023-11-06T22:23:48.947Z',
'0.0.182-rc.1': '2023-11-07T03:41:06.816Z',
'0.0.182': '2023-11-08T04:04:31.241Z',
'0.0.183': '2023-11-08T05:25:05.808Z',
'0.0.184': '2023-11-08T23:56:28.690Z',
'0.0.185': '2023-11-10T02:35:22.582Z',
'0.0.186': '2023-11-11T02:24:26.776Z',
'0.0.187': '2023-11-14T04:35:18.096Z',
'0.0.188': '2023-11-15T05:34:58.759Z',
'0.0.189': '2023-11-15T06:30:57.181Z',
'0.0.190': '2023-11-16T15:23:59.223Z',
'0.0.191': '2023-11-16T16:31:07.507Z',
'0.0.192': '2023-11-17T02:06:54.439Z',
'0.0.193': '2023-11-19T11:06:54.623Z',
'0.0.194': '2023-11-20T02:15:31.205Z',
'0.0.195': '2023-11-20T20:25:34.666Z',
'0.0.196': '2023-11-22T02:19:47.812Z',
'0.0.197-rc.0': '2023-11-22T22:16:48.971Z',
'0.0.197-rc.1': '2023-11-23T05:27:13.906Z',
'0.0.197': '2023-11-27T17:42:53.476Z',
'0.0.198': '2023-11-28T02:22:55.183Z',
'0.0.199': '2023-12-01T01:28:26.144Z',
'0.0.200': '2023-12-02T04:43:25.313Z',
'0.0.201': '2023-12-05T04:22:22.540Z',
'0.0.202': '2023-12-06T01:34:55.388Z',
'0.0.203': '2023-12-07T06:15:39.619Z',
'0.0.204-rc.0': '2023-12-08T02:34:03.010Z',
'0.0.204-rc.1': '2023-12-09T03:54:40.336Z',
'0.0.204-rc.2': '2023-12-11T20:15:35.207Z',
'0.0.204': '2023-12-11T22:28:49.455Z',
'0.0.205': '2023-12-12T18:27:07.427Z',
'0.0.206': '2023-12-13T04:51:42.993Z',
'0.0.207': '2023-12-14T03:34:38.535Z',
'0.0.208': '2023-12-15T10:05:40.181Z',
'0.0.209-rc.0': '2023-12-17T03:47:20.439Z',
'0.0.209': '2023-12-17T06:10:40.303Z',
'0.0.210': '2023-12-20T02:53:42.769Z',
'0.0.211': '2023-12-21T07:29:16.193Z',
'0.0.212': '2023-12-23T01:22:18.040Z',
'0.0.213': '2023-12-29T15:33:05.323Z',
'0.0.214': '2024-01-03T03:24:13.994Z',
'0.1.0-rc.0': '2024-01-05T22:33:51.804Z',
'0.1.0-rc.1': '2024-01-06T03:41:30.131Z',
'0.1.0': '2024-01-06T03:58:20.185Z',
'0.1.1': '2024-01-08T00:25:32.620Z',
'0.1.2': '2024-01-09T22:35:21.328Z',
'0.1.3': '2024-01-15T17:18:03.822Z',
'0.1.4': '2024-01-18T05:58:46.640Z',
'0.1.5': '2024-01-21T17:57:55.335Z',
'0.1.6': '2024-01-24T07:58:31.013Z',
'0.1.7': '2024-01-25T07:27:13.252Z',
'0.1.8': '2024-01-27T01:11:52.718Z',
'0.1.9-rc.0': '2024-01-27T06:14:56.103Z',
'0.1.9': '2024-01-27T06:25:12.844Z',
'0.1.10': '2024-01-30T09:54:07.013Z',
'0.1.11': '2024-01-31T03:31:39.843Z',
'0.1.12': '2024-02-01T23:57:54.094Z',
'0.1.13': '2024-02-05T03:24:29.383Z',
'0.1.14': '2024-02-07T01:03:54.355Z',
'0.1.15': '2024-02-07T19:22:37.134Z',
'0.1.16-rc.0': '2024-02-07T23:34:28.951Z',
'0.1.16': '2024-02-07T23:45:35.740Z',
'0.1.17': '2024-02-08T23:22:04.844Z',
'0.1.18': '2024-02-13T23:15:14.011Z',
'0.1.19-rc.0': '2024-02-14T23:59:15.835Z',
'0.1.19-rc.1': '2024-02-16T00:20:22.801Z',
'0.1.19': '2024-02-16T04:48:07.787Z',
'0.1.19-rc.2': '2024-02-16T18:47:07.688Z',
'0.1.20': '2024-02-16T22:03:07.378Z',
'0.1.21': '2024-02-21T22:48:04.422Z',
'0.1.22': '2024-02-26T20:27:53.383Z',
'0.1.23': '2024-02-27T04:07:47.477Z',
'0.1.24': '2024-02-29T00:52:15.516Z',
'0.1.25': '2024-03-01T18:49:55.617Z',
'0.1.26': '2024-03-10T01:36:01.782Z',
'0.1.27': '2024-03-13T01:01:08.908Z',
'0.1.28': '2024-03-15T00:06:05.523Z',
'0.1.29-rc.0': '2024-03-15T03:31:14.345Z',
'0.1.29': '2024-03-25T04:19:39.067Z',
'0.1.30': '2024-03-26T06:27:42.909Z',
'0.1.31': '2024-04-04T08:23:54.517Z',
'0.1.32': '2024-04-09T01:22:19.683Z',
'0.1.33': '2024-04-11T09:30:55.275Z',
'0.1.34': '2024-04-18T03:01:59.519Z',
'0.1.35': '2024-04-22T23:30:56.916Z',
'0.1.36': '2024-04-24T21:36:22.008Z',
'0.2.0-rc.0': '2024-04-26T23:56:59.053Z',
'0.2.0-rc.1': '2024-05-02T22:00:18.752Z',
'0.1.37': '2024-05-10T21:40:01.263Z'
},
name: 'langchain',
'dist-tags': {
latest: '0.1.37',
next: '0.2.0-rc.1'
},
versions: [
'0.0.4',
'0.0.5',
'0.0.6',
'0.0.7',
'0.0.8',
'0.0.9',
'0.0.10',
'0.0.11',
'0.0.12',
'0.0.13',
'0.0.14',
'0.0.15',
'0.0.16',
'0.0.17',
'0.0.18',
'0.0.19',
'0.0.20',
'0.0.21',
'0.0.22',
'0.0.23',
'0.0.24',
'0.0.25',
'0.0.26',
'0.0.27',
'0.0.28',
'0.0.29',
'0.0.30',
'0.0.31',
'0.0.32',
'0.0.33',
'0.0.34',
'0.0.35',
'0.0.36',
'0.0.37',
'0.0.38',
'0.0.39',
'0.0.40',
'0.0.41',
'0.0.42',
'0.0.43',
'0.0.44',
'0.0.45',
'0.0.46',
'0.0.47',
'0.0.48',
'0.0.49-0',
'0.0.49-1',
'0.0.49',
'0.0.50',
'0.0.51',
'0.0.52-0',
'0.0.52',
'0.0.53',
'0.0.54',
'0.0.55',
'0.0.56',
'0.0.57',
'0.0.58',
'0.0.59',
'0.0.60',
'0.0.61',
'0.0.62',
'0.0.63',
'0.0.64',
'0.0.65',
'0.0.66',
'0.0.67',
'0.0.68',
'0.0.69',
'0.0.70',
'0.0.71',
'0.0.72',
'0.0.73',
'0.0.74',
'0.0.75',
'0.0.76',
'0.0.77',
'0.0.78',
'0.0.79',
'0.0.80',
'0.0.81',
'0.0.82',
'0.0.83',
'0.0.84',
'0.0.85',
'0.0.86',
'0.0.87',
'0.0.88',
'0.0.89',
'0.0.90',
'0.0.91',
'0.0.92',
'0.0.93',
'0.0.94',
'0.0.95',
'0.0.96',
'0.0.97',
'0.0.98',
'0.0.99',
'0.0.100',
'0.0.101',
'0.0.102',
'0.0.103',
'0.0.104',
'0.0.105',
'0.0.106',
'0.0.107',
'0.0.108',
'0.0.109',
'0.0.110',
'0.0.111',
'0.0.112',
'0.0.113',
'0.0.114',
'0.0.115',
'0.0.116',
'0.0.117',
'0.0.118',
'0.0.119',
'0.0.120',
'0.0.121',
'0.0.122',
'0.0.123',
'0.0.124',
'0.0.125',
'0.0.126',
'0.0.127',
'0.0.128',
'0.0.129',
'0.0.130',
'0.0.131',
'0.0.132',
'0.0.133',
'0.0.134',
'0.0.135',
'0.0.136',
'0.0.137',
'0.0.138',
'0.0.139',
'0.0.140',
'0.0.141',
'0.0.142',
'0.0.143',
'0.0.144',
'0.0.145',
'0.0.146',
'0.0.147',
'0.0.148',
'0.0.149',
'0.0.150',
'0.0.151',
'0.0.152',
'0.0.153',
'0.0.154',
'0.0.155',
'0.0.156',
'0.0.157',
'0.0.158',
'0.0.159',
'0.0.160',
'0.0.161',
'0.0.162',
'0.0.163',
'0.0.164',
'0.0.165',
'0.0.166',
'0.0.167',
'0.0.168',
'0.0.169',
'0.0.170',
'0.0.171',
'0.0.172',
'0.0.173',
'0.0.174',
'0.0.175',
'0.0.176',
'0.0.177',
'0.0.178',
'0.0.179',
'0.0.180',
'0.0.181',
'0.0.182-rc.0',
'0.0.182-rc.1',
'0.0.182',
'0.0.183',
'0.0.184',
'0.0.185',
'0.0.186',
'0.0.187',
'0.0.188',
'0.0.189',
'0.0.190',
'0.0.191',
'0.0.192',
'0.0.193',
'0.0.194',
'0.0.195',
'0.0.196',
'0.0.197-rc.0',
'0.0.197-rc.1',
'0.0.197',
'0.0.198',
'0.0.199',
'0.0.200',
'0.0.201',
'0.0.202',
'0.0.203',
'0.0.204-rc.0',
'0.0.204-rc.1',
'0.0.204-rc.2',
'0.0.204',
'0.0.205',
'0.0.206',
'0.0.207',
'0.0.208',
'0.0.209-rc.0',
'0.0.209',
'0.0.210',
'0.0.211',
'0.0.212',
'0.0.213',
'0.0.214',
'0.1.0-rc.0',
'0.1.0-rc.1',
'0.1.0',
'0.1.1',
'0.1.2',
'0.1.3',
'0.1.4',
'0.1.5',
'0.1.6',
'0.1.7',
'0.1.8',
'0.1.9-rc.0',
'0.1.9',
'0.1.10',
'0.1.11',
'0.1.12',
'0.1.13',
'0.1.14',
'0.1.15',
'0.1.16-rc.0',
'0.1.16',
'0.1.17',
'0.1.18',
'0.1.19-rc.0',
'0.1.19-rc.1',
'0.1.19-rc.2',
'0.1.19',
'0.1.20',
'0.1.21',
'0.1.22',
'0.1.23',
'0.1.24',
'0.1.25',
'0.1.26',
'0.1.27',
'0.1.28',
'0.1.29-rc.0',
'0.1.29',
'0.1.30',
'0.1.31',
'0.1.32',
'0.1.33',
'0.1.34',
'0.1.35',
'0.1.36',
'0.1.37',
'0.2.0-rc.0',
'0.2.0-rc.1'
],
maintainers: [
{
name: 'nfcampos',
email: 'nuno@boringbits.io'
},
{
name: 'jacoblee93',
email: 'jacoblee93@gmail.com'
},
{
name: 'andrewnguonly',
email: 'andrewnguonly@gmail.com'
},
{
name: 'davidduong',
email: 'david@duong.cz'
},
{
name: 'hwchase17',
email: 'hw.chase.17@gmail.com'
},
{
name: 'basproul',
email: 'braceasproul@gmail.com'
}
],
description: 'Typescript bindings for langchain',
author: {
name: 'LangChain'
},
license: 'MIT',
readmeFilename: 'README.md',
keywords: [
'llm',
'ai',
'gpt3',
'chain',
'prompt',
'prompt engineering',
'chatgpt',
'machine learning',
'ml',
'openai',
'embeddings',
'vectorstores'
],
repository: {
type: 'git',
url: 'git+ssh://git@github.com/langchain-ai/langchainjs.git'
},
homepage: 'https://github.com/langchain-ai/langchainjs/tree/main/langchain/',
bugs: {
url: 'https://github.com/langchain-ai/langchainjs/issues'
},
version: '0.1.37',
type: 'module',
engines: {
node: '>=18'
},
main: './index.js',
types: './index.d.ts',
scripts: {
build: 'yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts',
'build:deps': 'yarn run turbo:command build --filter=@langchain/openai --filter=@langchain/community --filter=@langchain/textsplitters --concurrency=1',
'build:esm': 'NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist//tests',
'build:cjs': 'NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs',
'build:watch': 'yarn create-entrypoints && tsc --outDir dist/ --watch',
'build:scripts': 'yarn create-entrypoints && yarn check-tree-shaking',
'lint:eslint': 'NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/',
'lint:dpdm': 'dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/
/.ts',
lint: 'yarn lint:eslint && yarn lint:dpdm',
'lint:fix': 'yarn lint:eslint --fix && yarn lint:dpdm',
precommit: 'lint-staged',
clean: 'rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre --gen-maps',
prepack: 'yarn build',
release: 'release-it --only-version --config .release-it.json',
test: 'yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\.int\.test.ts --testTimeout 30000 --maxWorkers=50%',
'test:watch': 'yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\.int\.test.ts',
'test:integration': 'yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\.int\.test.ts --testTimeout 100000 --maxWorkers=50%',
'test:single': 'yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000',
format: 'prettier --config .prettierrc --write "src"',
'format:check': 'prettier --config .prettierrc --check "src"',
'move-cjs-to-dist': 'yarn lc-build --config ./langchain.config.js --move-cjs-dist',
'create-entrypoints': 'yarn lc-build --config ./langchain.config.js --create-entrypoints',
'check-tree-shaking': 'yarn lc-build --config ./langchain.config.js --tree-shaking'
},
devDependencies: {
'@aws-sdk/client-s3': '^3.310.0',
'@aws-sdk/client-sagemaker-runtime': '^3.414.0',
'@aws-sdk/client-sfn': '^3.362.0',
'@aws-sdk/credential-provider-node': '^3.388.0',
'@aws-sdk/types': '^3.357.0',
'@azure/storage-blob': '^12.15.0',
'@browserbasehq/sdk': '^1.0.0',
'@cloudflare/workers-types': '^4.20230922.0',
'@faker-js/faker': '^7.6.0',
'@gomomento/sdk': '^1.51.1',
'@gomomento/sdk-core': '^1.51.1',
'@google-ai/generativelanguage': '^0.2.1',
'@google-cloud/storage': '^7.7.0',
'@jest/globals': '^29.5.0',
'@langchain/scripts': '~0.0',
'@mendable/firecrawl-js': '^0.0.13',
'@notionhq/client': '^2.2.10',
'@pinecone-database/pinecone': '^1.1.0',
'@supabase/supabase-js': '^2.10.0',
'@swc/core': '^1.3.90',
'@swc/jest': '^0.2.29',
'@tsconfig/recommended': '^1.0.2',
'@types/d3-dsv': '^2',
'@types/decamelize': '^1.2.0',
'@types/handlebars': '^4.1.0',
'@types/html-to-text': '^9',
'@types/js-yaml': '^4',
'@types/jsdom': '^21.1.1',
'@types/pdf-parse': '^1.1.1',
'@types/uuid': '^9',
'@types/ws': '^8',
'@typescript-eslint/eslint-plugin': '^5.58.0',
'@typescript-eslint/parser': '^5.58.0',
'@vercel/kv': '^0.2.3',
'@xata.io/client': '^0.28.0',
'apify-client': '^2.7.1',
assemblyai: '^4.0.0',
axios: '^0.26.0',
cheerio: '^1.0.0-rc.12',
chromadb: '^1.5.3',
convex: '^1.3.1',
couchbase: '^4.3.0',
'd3-dsv': '^2.0.0',
dotenv: '^16.0.3',
dpdm: '^3.12.0',
epub2: '^3.0.1',
eslint: '^8.33.0',
'eslint-config-airbnb-base': '^15.0.0',
'eslint-config-prettier': '^8.6.0',
'eslint-plugin-import': '^2.27.5',
'eslint-plugin-jest': '^27.6.0',
'eslint-plugin-no-instanceof': '^1.0.1',
'eslint-plugin-prettier': '^4.2.1',
'fast-xml-parser': '^4.2.7',
'google-auth-library': '^8.9.0',
handlebars: '^4.7.8',
'html-to-text': '^9.0.5',
ignore: '^5.2.0',
ioredis: '^5.3.2',
jest: '^29.5.0',
'jest-environment-node': '^29.6.4',
jsdom: '^22.1.0',
mammoth: '^1.5.1',
mongodb: '^5.2.0',
'node-llama-cpp': '2.7.3',
'notion-to-md': '^3.1.0',
officeparser: '^4.0.4',
openai: '^4.32.1',
'pdf-parse': '1.1.1',
peggy: '^3.0.2',
playwright: '^1.32.1',
prettier: '^2.8.3',
puppeteer: '^19.7.2',
pyodide: '^0.24.1',
redis: '^4.6.6',
'release-it': '^15.10.1',
rimraf: '^5.0.1',
rollup: '^3.19.1',
'sonix-speech-recognition': '^2.1.1',
'srt-parser-2': '^1.2.3',
'ts-jest': '^29.1.0',
typeorm: '^0.3.12',
typescript: '~5.1.6',
'weaviate-ts-client': '^2.0.0',
'web-auth-library': '^1.0.3',
wikipedia: '^2.1.2',
'youtube-transcript': '^1.0.6',
'youtubei.js': '^9.1.0'
},
peerDependencies: {
'@aws-sdk/client-s3': '^3.310.0',
'@aws-sdk/client-sagemaker-runtime': '^3.310.0',
'@aws-sdk/client-sfn': '^3.310.0',
'@aws-sdk/credential-provider-node': '^3.388.0',
'@azure/storage-blob': '^12.15.0',
'@browserbasehq/sdk': '
',
'@gomomento/sdk': '^1.51.1',
'@gomomento/sdk-core': '^1.51.1',
'@gomomento/sdk-web': '^1.51.1',
'@google-ai/generativelanguage': '^0.2.1',
'@google-cloud/storage': '^6.10.1 || ^7.7.0',
'@mendable/firecrawl-js': '^0.0.13',
'@notionhq/client': '^2.2.10',
'@pinecone-database/pinecone': '',
'@supabase/supabase-js': '^2.10.0',
'@vercel/kv': '^0.2.3',
'@xata.io/client': '^0.28.0',
'apify-client': '^2.7.1',
assemblyai: '^4.0.0',
axios: '
',
cheerio: '^1.0.0-rc.12',
chromadb: '',
convex: '^1.3.1',
couchbase: '^4.3.0',
'd3-dsv': '^2.0.0',
epub2: '^3.0.1',
'fast-xml-parser': '
',
'google-auth-library': '^8.9.0',
handlebars: '^4.7.8',
'html-to-text': '^9.0.5',
ignore: '^5.2.0',
ioredis: '^5.3.2',
jsdom: '',
mammoth: '^1.6.0',
mongodb: '>=5.2.0',
'node-llama-cpp': '
',
'notion-to-md': '^3.1.0',
officeparser: '^4.0.4',
'pdf-parse': '1.1.1',
peggy: '^3.0.2',
playwright: '^1.32.1',
puppeteer: '^19.7.2',
pyodide: '^0.24.1',
redis: '^4.6.4',
'sonix-speech-recognition': '^2.1.1',
'srt-parser-2': '^1.2.3',
typeorm: '^0.3.12',
'weaviate-ts-client': '*',
'web-auth-library': '^1.0.3',
ws: '^8.14.2',
'youtube-transcript': '^1.0.6',
'youtubei.js': '^9.1.0'
}
}

Platform: linux alpine in docker
Node version: 20.11.1
Yarn version: 1.22.19

Copy link

dosubot bot commented May 15, 2024

Hello @ronneldavis,

I'm Dosu, a bot here to help you solve bugs, answer questions, and guide you on becoming a contributor to LangChain JS. While you're waiting for a human maintainer, I'm here to provide assistance. Let's take a closer look at the issue you're facing with passing message history to anthropic models, and I'll get back to you with a thorough response shortly.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label May 15, 2024
@bracesproul
Copy link
Collaborator

hey @ronneldavis could you send just your langchain, @langchain/core and @langchain/anthropic versions?

Copy link

dosubot bot commented May 15, 2024

The versions of langchain, @langchain/core, and @langchain/anthropic I'm using are as follows:

  • langchain: 0.1.37
  • @langchain/core and @langchain/anthropic: These specific versions are not listed in the system info provided. Typically, the versions of these packages align with the langchain version unless specified otherwise in your project's package.json.

For the most accurate versions, please check your package.json file or run yarn list | grep "@langchain" in your project directory to see the exact versions installed.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@ronneldavis
Copy link
Author

@bracesproul These are the versions I'm using:

"langchain": "^0.1.37",
"@langchain/anthropic": "^0.1.18",
"@langchain/core": "^0.1.63",

@jeasonnow
Copy link
Contributor

jeasonnow commented May 19, 2024

if (typeof content === "string") {
return content;
} else {
const contentBlocks = content.map((contentPart) => {
if (contentPart.type === "image_url") {
let source;
if (typeof contentPart.image_url === "string") {
source = _formatImage(contentPart.image_url);
} else {
source = _formatImage(contentPart.image_url.url);
}
return {
type: "image" as const, // Explicitly setting the type as "image"
source,
};
} else if (contentPart.type === "text") {
// Assuming contentPart is of type MessageContentText here
return {
type: "text" as const, // Explicitly setting the type as "text"
text: contentPart.text,
};
} else if (
contentPart.type === "tool_use" ||
contentPart.type === "tool_result"
) {
// TODO: Fix when SDK types are fixed
return {
...contentPart,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
} else {
throw new Error("Unsupported message content format");
}
});
return contentBlocks;
}

This is because anthropic assumes that all message objects have a content attribute without considering the possibility that message.content might be null. Perhaps setting the content of your AIMessage to '' could help you resolve this issue.

if (isAIMessage(message) && !!message.tool_calls?.length) {
if (typeof message.content === "string") {
if (message.content === "") {
return {
role,
content: message.tool_calls.map(
_convertLangChainToolCallToAnthropic
),
};
} else {
return {
role,
content: [
{ type: "text", text: message.content },
...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
],
};
}
} else {

Additionally, the latest version of AIMessage now supports direct parsing of tool_calls, so you no longer need to handle this manually.

@ronneldavis

@ronneldavis
Copy link
Author

Awesome that worked, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants