langchain 0.0.202 → 0.0.204-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache/cloudflare_kv.cjs +15 -69
- package/dist/cache/cloudflare_kv.d.ts +1 -42
- package/dist/cache/cloudflare_kv.js +1 -67
- package/dist/cache/momento.cjs +15 -135
- package/dist/cache/momento.d.ts +1 -91
- package/dist/cache/momento.js +1 -133
- package/dist/cache/upstash_redis.cjs +15 -72
- package/dist/cache/upstash_redis.d.ts +1 -45
- package/dist/cache/upstash_redis.js +1 -70
- package/dist/callbacks/handlers/llmonitor.cjs +14 -238
- package/dist/callbacks/handlers/llmonitor.d.ts +1 -35
- package/dist/callbacks/handlers/llmonitor.js +1 -233
- package/dist/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.d.ts +4 -4
- package/dist/chat_models/baiduwenxin.cjs +15 -411
- package/dist/chat_models/baiduwenxin.d.ts +1 -150
- package/dist/chat_models/baiduwenxin.js +1 -409
- package/dist/chat_models/bedrock/index.cjs +15 -33
- package/dist/chat_models/bedrock/index.d.ts +1 -23
- package/dist/chat_models/bedrock/index.js +1 -29
- package/dist/chat_models/bedrock/web.cjs +15 -360
- package/dist/chat_models/bedrock/web.d.ts +1 -82
- package/dist/chat_models/bedrock/web.js +1 -356
- package/dist/chat_models/cloudflare_workersai.cjs +15 -216
- package/dist/chat_models/cloudflare_workersai.d.ts +1 -68
- package/dist/chat_models/cloudflare_workersai.js +1 -214
- package/dist/chat_models/fireworks.cjs +15 -89
- package/dist/chat_models/fireworks.d.ts +1 -43
- package/dist/chat_models/fireworks.js +1 -87
- package/dist/chat_models/googlepalm.cjs +15 -235
- package/dist/chat_models/googlepalm.d.ts +1 -108
- package/dist/chat_models/googlepalm.js +1 -233
- package/dist/chat_models/googlevertexai/index.cjs +15 -40
- package/dist/chat_models/googlevertexai/index.d.ts +1 -28
- package/dist/chat_models/googlevertexai/index.js +1 -38
- package/dist/chat_models/googlevertexai/web.cjs +15 -39
- package/dist/chat_models/googlevertexai/web.d.ts +1 -28
- package/dist/chat_models/googlevertexai/web.js +1 -37
- package/dist/chat_models/iflytek_xinghuo/index.cjs +14 -40
- package/dist/chat_models/iflytek_xinghuo/index.d.ts +1 -13
- package/dist/chat_models/iflytek_xinghuo/index.js +1 -36
- package/dist/chat_models/iflytek_xinghuo/web.cjs +15 -36
- package/dist/chat_models/iflytek_xinghuo/web.d.ts +1 -13
- package/dist/chat_models/iflytek_xinghuo/web.js +1 -34
- package/dist/chat_models/llama_cpp.cjs +15 -275
- package/dist/chat_models/llama_cpp.d.ts +1 -71
- package/dist/chat_models/llama_cpp.js +1 -273
- package/dist/chat_models/minimax.cjs +15 -578
- package/dist/chat_models/minimax.d.ts +1 -369
- package/dist/chat_models/minimax.js +1 -576
- package/dist/chat_models/ollama.cjs +15 -394
- package/dist/chat_models/ollama.d.ts +1 -127
- package/dist/chat_models/ollama.js +1 -392
- package/dist/chat_models/openai.cjs +4 -698
- package/dist/chat_models/openai.d.ts +4 -137
- package/dist/chat_models/openai.js +2 -695
- package/dist/chat_models/portkey.cjs +15 -167
- package/dist/chat_models/portkey.d.ts +1 -17
- package/dist/chat_models/portkey.js +1 -165
- package/dist/chat_models/yandex.cjs +15 -132
- package/dist/chat_models/yandex.d.ts +1 -30
- package/dist/chat_models/yandex.js +1 -130
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/document_loaders/fs/pdf.cjs +1 -1
- package/dist/document_loaders/fs/pdf.js +1 -1
- package/dist/document_loaders/web/pdf.cjs +1 -1
- package/dist/document_loaders/web/pdf.js +1 -1
- package/dist/document_transformers/html_to_text.cjs +15 -47
- package/dist/document_transformers/html_to_text.d.ts +1 -30
- package/dist/document_transformers/html_to_text.js +1 -45
- package/dist/document_transformers/mozilla_readability.cjs +15 -53
- package/dist/document_transformers/mozilla_readability.d.ts +1 -31
- package/dist/document_transformers/mozilla_readability.js +1 -51
- package/dist/embeddings/bedrock.cjs +15 -109
- package/dist/embeddings/bedrock.d.ts +1 -71
- package/dist/embeddings/bedrock.js +1 -107
- package/dist/embeddings/cloudflare_workersai.cjs +15 -67
- package/dist/embeddings/cloudflare_workersai.d.ts +1 -28
- package/dist/embeddings/cloudflare_workersai.js +1 -65
- package/dist/embeddings/cohere.cjs +15 -124
- package/dist/embeddings/cohere.d.ts +1 -66
- package/dist/embeddings/cohere.js +1 -122
- package/dist/embeddings/googlepalm.cjs +15 -88
- package/dist/embeddings/googlepalm.d.ts +1 -61
- package/dist/embeddings/googlepalm.js +1 -86
- package/dist/embeddings/googlevertexai.cjs +15 -81
- package/dist/embeddings/googlevertexai.d.ts +1 -54
- package/dist/embeddings/googlevertexai.js +1 -79
- package/dist/embeddings/gradient_ai.cjs +15 -101
- package/dist/embeddings/gradient_ai.d.ts +1 -48
- package/dist/embeddings/gradient_ai.js +1 -99
- package/dist/embeddings/hf.cjs +15 -75
- package/dist/embeddings/hf.d.ts +1 -41
- package/dist/embeddings/hf.js +1 -73
- package/dist/embeddings/hf_transformers.cjs +15 -87
- package/dist/embeddings/hf_transformers.d.ts +1 -47
- package/dist/embeddings/hf_transformers.js +1 -85
- package/dist/embeddings/llama_cpp.cjs +15 -81
- package/dist/embeddings/llama_cpp.d.ts +1 -42
- package/dist/embeddings/llama_cpp.js +1 -79
- package/dist/embeddings/minimax.cjs +15 -165
- package/dist/embeddings/minimax.d.ts +1 -119
- package/dist/embeddings/minimax.js +1 -163
- package/dist/embeddings/ollama.cjs +15 -118
- package/dist/embeddings/ollama.d.ts +1 -34
- package/dist/embeddings/ollama.js +1 -116
- package/dist/embeddings/openai.cjs +2 -240
- package/dist/embeddings/openai.d.ts +1 -82
- package/dist/embeddings/openai.js +1 -239
- package/dist/embeddings/tensorflow.cjs +3 -96
- package/dist/embeddings/tensorflow.d.ts +1 -59
- package/dist/embeddings/tensorflow.js +1 -83
- package/dist/embeddings/voyage.cjs +15 -118
- package/dist/embeddings/voyage.d.ts +1 -66
- package/dist/embeddings/voyage.js +1 -116
- package/dist/experimental/chat_models/ollama_functions.d.ts +1 -1
- package/dist/experimental/masking/index.cjs +9 -0
- package/dist/experimental/masking/index.d.ts +4 -0
- package/dist/experimental/masking/index.js +3 -0
- package/dist/experimental/masking/parser.cjs +134 -0
- package/dist/experimental/masking/parser.d.ts +44 -0
- package/dist/experimental/masking/parser.js +130 -0
- package/dist/experimental/masking/regex_masking_transformer.cjs +145 -0
- package/dist/experimental/masking/regex_masking_transformer.d.ts +48 -0
- package/dist/experimental/masking/regex_masking_transformer.js +141 -0
- package/dist/experimental/masking/transformer.cjs +9 -0
- package/dist/experimental/masking/transformer.d.ts +7 -0
- package/dist/experimental/masking/transformer.js +5 -0
- package/dist/experimental/masking/types.cjs +2 -0
- package/dist/experimental/masking/types.d.ts +25 -0
- package/dist/experimental/masking/types.js +1 -0
- package/dist/experimental/openai_assistant/index.cjs +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.js +1 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/graphs/neo4j_graph.cjs +3 -254
- package/dist/graphs/neo4j_graph.d.ts +1 -63
- package/dist/graphs/neo4j_graph.js +1 -241
- package/dist/llms/ai21.cjs +15 -200
- package/dist/llms/ai21.d.ts +1 -100
- package/dist/llms/ai21.js +1 -198
- package/dist/llms/aleph_alpha.cjs +15 -374
- package/dist/llms/aleph_alpha.d.ts +1 -161
- package/dist/llms/aleph_alpha.js +1 -372
- package/dist/llms/bedrock/web.cjs +15 -310
- package/dist/llms/bedrock/web.d.ts +1 -66
- package/dist/llms/bedrock/web.js +1 -308
- package/dist/llms/cloudflare_workersai.cjs +15 -168
- package/dist/llms/cloudflare_workersai.d.ts +1 -55
- package/dist/llms/cloudflare_workersai.js +1 -166
- package/dist/llms/cohere.cjs +15 -110
- package/dist/llms/cohere.d.ts +1 -54
- package/dist/llms/cohere.js +1 -108
- package/dist/llms/fireworks.cjs +15 -90
- package/dist/llms/fireworks.d.ts +1 -33
- package/dist/llms/fireworks.js +1 -88
- package/dist/llms/googlepalm.cjs +15 -134
- package/dist/llms/googlepalm.d.ts +1 -92
- package/dist/llms/googlepalm.js +1 -132
- package/dist/llms/googlevertexai/index.cjs +15 -45
- package/dist/llms/googlevertexai/index.d.ts +1 -38
- package/dist/llms/googlevertexai/index.js +1 -43
- package/dist/llms/googlevertexai/web.cjs +15 -42
- package/dist/llms/googlevertexai/web.d.ts +1 -36
- package/dist/llms/googlevertexai/web.js +1 -40
- package/dist/llms/gradient_ai.cjs +15 -110
- package/dist/llms/gradient_ai.d.ts +1 -55
- package/dist/llms/gradient_ai.js +1 -108
- package/dist/llms/hf.cjs +15 -136
- package/dist/llms/hf.d.ts +1 -68
- package/dist/llms/hf.js +1 -134
- package/dist/llms/llama_cpp.cjs +15 -111
- package/dist/llms/llama_cpp.d.ts +1 -41
- package/dist/llms/llama_cpp.js +1 -109
- package/dist/llms/ollama.cjs +15 -353
- package/dist/llms/ollama.d.ts +1 -106
- package/dist/llms/ollama.js +1 -351
- package/dist/llms/openai-chat.cjs +3 -445
- package/dist/llms/openai-chat.d.ts +4 -123
- package/dist/llms/openai-chat.js +2 -443
- package/dist/llms/openai.cjs +6 -530
- package/dist/llms/openai.d.ts +4 -123
- package/dist/llms/openai.js +2 -525
- package/dist/llms/portkey.cjs +14 -168
- package/dist/llms/portkey.d.ts +1 -57
- package/dist/llms/portkey.js +1 -162
- package/dist/llms/raycast.cjs +15 -94
- package/dist/llms/raycast.d.ts +1 -50
- package/dist/llms/raycast.js +1 -92
- package/dist/llms/replicate.cjs +15 -132
- package/dist/llms/replicate.d.ts +1 -51
- package/dist/llms/replicate.js +1 -130
- package/dist/llms/sagemaker_endpoint.cjs +15 -203
- package/dist/llms/sagemaker_endpoint.d.ts +1 -121
- package/dist/llms/sagemaker_endpoint.js +1 -200
- package/dist/llms/watsonx_ai.cjs +15 -152
- package/dist/llms/watsonx_ai.d.ts +1 -72
- package/dist/llms/watsonx_ai.js +1 -150
- package/dist/llms/writer.cjs +15 -165
- package/dist/llms/writer.d.ts +1 -60
- package/dist/llms/writer.js +1 -163
- package/dist/llms/yandex.cjs +15 -98
- package/dist/llms/yandex.d.ts +1 -40
- package/dist/llms/yandex.js +1 -96
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/load/index.cjs +3 -1
- package/dist/load/index.d.ts +1 -1
- package/dist/load/index.js +3 -1
- package/dist/retrievers/amazon_kendra.cjs +15 -273
- package/dist/retrievers/amazon_kendra.d.ts +1 -142
- package/dist/retrievers/amazon_kendra.js +1 -271
- package/dist/retrievers/chaindesk.cjs +15 -88
- package/dist/retrievers/chaindesk.d.ts +1 -31
- package/dist/retrievers/chaindesk.js +1 -86
- package/dist/retrievers/databerry.cjs +15 -82
- package/dist/retrievers/databerry.d.ts +1 -34
- package/dist/retrievers/databerry.js +1 -80
- package/dist/retrievers/metal.cjs +15 -51
- package/dist/retrievers/metal.d.ts +1 -36
- package/dist/retrievers/metal.js +1 -49
- package/dist/retrievers/supabase.cjs +15 -167
- package/dist/retrievers/supabase.d.ts +1 -82
- package/dist/retrievers/supabase.js +1 -165
- package/dist/retrievers/tavily_search_api.cjs +15 -142
- package/dist/retrievers/tavily_search_api.d.ts +1 -38
- package/dist/retrievers/tavily_search_api.js +1 -140
- package/dist/retrievers/zep.cjs +15 -150
- package/dist/retrievers/zep.d.ts +1 -76
- package/dist/retrievers/zep.js +1 -148
- package/dist/schema/document.cjs +2 -16
- package/dist/schema/document.d.ts +1 -11
- package/dist/schema/document.js +1 -16
- package/dist/schema/index.cjs +2 -7
- package/dist/schema/index.d.ts +2 -10
- package/dist/schema/index.js +1 -6
- package/dist/storage/convex.cjs +15 -143
- package/dist/storage/convex.d.ts +1 -106
- package/dist/storage/convex.js +1 -141
- package/dist/storage/ioredis.cjs +15 -158
- package/dist/storage/ioredis.d.ts +1 -71
- package/dist/storage/ioredis.js +1 -156
- package/dist/storage/upstash_redis.cjs +15 -172
- package/dist/storage/upstash_redis.d.ts +1 -78
- package/dist/storage/upstash_redis.js +1 -170
- package/dist/storage/vercel_kv.cjs +15 -161
- package/dist/storage/vercel_kv.d.ts +1 -62
- package/dist/storage/vercel_kv.js +1 -159
- package/dist/stores/doc/in_memory.cjs +15 -105
- package/dist/stores/doc/in_memory.d.ts +1 -46
- package/dist/stores/doc/in_memory.js +1 -102
- package/dist/stores/message/cassandra.cjs +15 -158
- package/dist/stores/message/cassandra.d.ts +1 -69
- package/dist/stores/message/cassandra.js +1 -156
- package/dist/stores/message/cloudflare_d1.cjs +15 -150
- package/dist/stores/message/cloudflare_d1.d.ts +1 -67
- package/dist/stores/message/cloudflare_d1.js +1 -148
- package/dist/stores/message/convex.cjs +15 -118
- package/dist/stores/message/convex.d.ts +1 -81
- package/dist/stores/message/convex.js +1 -116
- package/dist/stores/message/dynamodb.cjs +15 -159
- package/dist/stores/message/dynamodb.d.ts +1 -51
- package/dist/stores/message/dynamodb.js +1 -157
- package/dist/stores/message/firestore.cjs +15 -185
- package/dist/stores/message/firestore.d.ts +1 -68
- package/dist/stores/message/firestore.js +1 -183
- package/dist/stores/message/ioredis.cjs +15 -98
- package/dist/stores/message/ioredis.d.ts +1 -64
- package/dist/stores/message/ioredis.js +1 -96
- package/dist/stores/message/momento.cjs +15 -160
- package/dist/stores/message/momento.d.ts +1 -97
- package/dist/stores/message/momento.js +1 -158
- package/dist/stores/message/mongodb.cjs +15 -57
- package/dist/stores/message/mongodb.d.ts +1 -26
- package/dist/stores/message/mongodb.js +1 -55
- package/dist/stores/message/planetscale.cjs +15 -170
- package/dist/stores/message/planetscale.d.ts +1 -75
- package/dist/stores/message/planetscale.js +1 -168
- package/dist/stores/message/redis.cjs +15 -114
- package/dist/stores/message/redis.d.ts +1 -69
- package/dist/stores/message/redis.js +1 -112
- package/dist/stores/message/upstash_redis.cjs +15 -85
- package/dist/stores/message/upstash_redis.d.ts +1 -43
- package/dist/stores/message/upstash_redis.js +1 -83
- package/dist/stores/message/utils.cjs +3 -23
- package/dist/stores/message/utils.d.ts +1 -17
- package/dist/stores/message/utils.js +1 -21
- package/dist/stores/message/xata.cjs +15 -216
- package/dist/stores/message/xata.d.ts +1 -76
- package/dist/stores/message/xata.js +1 -214
- package/dist/tools/IFTTTWebhook.cjs +15 -87
- package/dist/tools/IFTTTWebhook.d.ts +1 -50
- package/dist/tools/IFTTTWebhook.js +1 -85
- package/dist/tools/aiplugin.cjs +15 -73
- package/dist/tools/aiplugin.d.ts +1 -34
- package/dist/tools/aiplugin.js +1 -71
- package/dist/tools/aws_sfn.cjs +15 -201
- package/dist/tools/aws_sfn.d.ts +1 -57
- package/dist/tools/aws_sfn.js +1 -197
- package/dist/tools/bingserpapi.cjs +15 -76
- package/dist/tools/bingserpapi.d.ts +1 -23
- package/dist/tools/bingserpapi.js +1 -75
- package/dist/tools/brave_search.cjs +15 -64
- package/dist/tools/brave_search.d.ts +1 -23
- package/dist/tools/brave_search.js +1 -62
- package/dist/tools/connery.cjs +15 -277
- package/dist/tools/connery.d.ts +1 -145
- package/dist/tools/connery.js +1 -274
- package/dist/tools/convert_to_openai.d.ts +1 -1
- package/dist/tools/dadjokeapi.cjs +15 -46
- package/dist/tools/dadjokeapi.d.ts +1 -15
- package/dist/tools/dadjokeapi.js +1 -45
- package/dist/tools/dataforseo_api_search.cjs +15 -280
- package/dist/tools/dataforseo_api_search.d.ts +1 -170
- package/dist/tools/dataforseo_api_search.js +1 -278
- package/dist/tools/gmail/index.cjs +15 -11
- package/dist/tools/gmail/index.d.ts +1 -11
- package/dist/tools/gmail/index.js +1 -5
- package/dist/tools/google_custom_search.cjs +15 -70
- package/dist/tools/google_custom_search.d.ts +1 -25
- package/dist/tools/google_custom_search.js +1 -68
- package/dist/tools/google_places.cjs +15 -79
- package/dist/tools/google_places.d.ts +1 -21
- package/dist/tools/google_places.js +1 -77
- package/dist/tools/searchapi.cjs +15 -164
- package/dist/tools/searchapi.d.ts +1 -91
- package/dist/tools/searchapi.js +1 -162
- package/dist/tools/searxng_search.cjs +15 -156
- package/dist/tools/searxng_search.d.ts +1 -114
- package/dist/tools/searxng_search.js +1 -154
- package/dist/tools/serpapi.cjs +15 -199
- package/dist/tools/serpapi.d.ts +1 -302
- package/dist/tools/serpapi.js +1 -197
- package/dist/tools/serper.cjs +15 -94
- package/dist/tools/serper.d.ts +1 -32
- package/dist/tools/serper.js +1 -92
- package/dist/tools/wikipedia_query_run.cjs +15 -133
- package/dist/tools/wikipedia_query_run.d.ts +1 -54
- package/dist/tools/wikipedia_query_run.js +1 -131
- package/dist/tools/wolframalpha.cjs +15 -47
- package/dist/tools/wolframalpha.d.ts +1 -21
- package/dist/tools/wolframalpha.js +1 -45
- package/dist/types/openai-types.d.ts +1 -1
- package/dist/util/convex.d.ts +1 -1
- package/dist/util/event-source-parse.cjs +15 -224
- package/dist/util/event-source-parse.d.ts +1 -39
- package/dist/util/event-source-parse.js +1 -219
- package/dist/util/math.cjs +15 -123
- package/dist/util/math.d.ts +1 -40
- package/dist/util/math.js +1 -116
- package/dist/util/openapi.d.ts +2 -2
- package/dist/vectorstores/analyticdb.cjs +3 -333
- package/dist/vectorstores/analyticdb.d.ts +1 -110
- package/dist/vectorstores/analyticdb.js +1 -317
- package/dist/vectorstores/base.cjs +15 -165
- package/dist/vectorstores/base.d.ts +1 -104
- package/dist/vectorstores/base.js +1 -161
- package/dist/vectorstores/cassandra.cjs +15 -478
- package/dist/vectorstores/cassandra.d.ts +1 -162
- package/dist/vectorstores/cassandra.js +1 -476
- package/dist/vectorstores/chroma.cjs +3 -312
- package/dist/vectorstores/chroma.d.ts +1 -133
- package/dist/vectorstores/chroma.js +1 -299
- package/dist/vectorstores/clickhouse.cjs +3 -272
- package/dist/vectorstores/clickhouse.d.ts +1 -126
- package/dist/vectorstores/clickhouse.js +1 -259
- package/dist/vectorstores/closevector/node.cjs +15 -107
- package/dist/vectorstores/closevector/node.d.ts +1 -83
- package/dist/vectorstores/closevector/node.js +1 -105
- package/dist/vectorstores/closevector/web.cjs +15 -107
- package/dist/vectorstores/closevector/web.d.ts +1 -80
- package/dist/vectorstores/closevector/web.js +1 -105
- package/dist/vectorstores/cloudflare_vectorize.cjs +3 -186
- package/dist/vectorstores/cloudflare_vectorize.d.ts +1 -90
- package/dist/vectorstores/cloudflare_vectorize.js +1 -173
- package/dist/vectorstores/convex.cjs +15 -175
- package/dist/vectorstores/convex.d.ts +1 -134
- package/dist/vectorstores/convex.js +1 -173
- package/dist/vectorstores/elasticsearch.cjs +3 -287
- package/dist/vectorstores/elasticsearch.d.ts +1 -143
- package/dist/vectorstores/elasticsearch.js +1 -274
- package/dist/vectorstores/faiss.cjs +3 -378
- package/dist/vectorstores/faiss.d.ts +1 -127
- package/dist/vectorstores/faiss.js +1 -365
- package/dist/vectorstores/googlevertexai.cjs +3 -538
- package/dist/vectorstores/googlevertexai.d.ts +1 -181
- package/dist/vectorstores/googlevertexai.js +1 -521
- package/dist/vectorstores/hnswlib.cjs +15 -273
- package/dist/vectorstores/hnswlib.d.ts +1 -122
- package/dist/vectorstores/hnswlib.js +1 -271
- package/dist/vectorstores/lancedb.cjs +15 -126
- package/dist/vectorstores/lancedb.d.ts +1 -61
- package/dist/vectorstores/lancedb.js +1 -124
- package/dist/vectorstores/memory.cjs +15 -143
- package/dist/vectorstores/memory.d.ts +1 -92
- package/dist/vectorstores/memory.js +1 -141
- package/dist/vectorstores/milvus.cjs +3 -592
- package/dist/vectorstores/milvus.d.ts +1 -139
- package/dist/vectorstores/milvus.js +1 -579
- package/dist/vectorstores/momento_vector_index.cjs +3 -305
- package/dist/vectorstores/momento_vector_index.d.ts +1 -143
- package/dist/vectorstores/momento_vector_index.js +1 -292
- package/dist/vectorstores/mongodb_atlas.cjs +15 -218
- package/dist/vectorstores/mongodb_atlas.d.ts +1 -118
- package/dist/vectorstores/mongodb_atlas.js +1 -216
- package/dist/vectorstores/myscale.cjs +3 -263
- package/dist/vectorstores/myscale.d.ts +1 -129
- package/dist/vectorstores/myscale.js +1 -250
- package/dist/vectorstores/neo4j_vector.cjs +3 -578
- package/dist/vectorstores/neo4j_vector.d.ts +1 -75
- package/dist/vectorstores/neo4j_vector.js +1 -562
- package/dist/vectorstores/opensearch.cjs +3 -274
- package/dist/vectorstores/opensearch.d.ts +1 -119
- package/dist/vectorstores/opensearch.js +1 -261
- package/dist/vectorstores/pgvector.cjs +14 -404
- package/dist/vectorstores/pgvector.d.ts +1 -160
- package/dist/vectorstores/pgvector.js +1 -400
- package/dist/vectorstores/pinecone.cjs +3 -280
- package/dist/vectorstores/pinecone.d.ts +1 -120
- package/dist/vectorstores/pinecone.js +1 -264
- package/dist/vectorstores/prisma.cjs +15 -305
- package/dist/vectorstores/prisma.d.ts +1 -180
- package/dist/vectorstores/prisma.js +1 -304
- package/dist/vectorstores/qdrant.cjs +15 -195
- package/dist/vectorstores/qdrant.d.ts +1 -95
- package/dist/vectorstores/qdrant.js +1 -193
- package/dist/vectorstores/redis.cjs +15 -333
- package/dist/vectorstores/redis.d.ts +1 -186
- package/dist/vectorstores/redis.js +1 -331
- package/dist/vectorstores/rockset.cjs +15 -351
- package/dist/vectorstores/rockset.d.ts +1 -202
- package/dist/vectorstores/rockset.js +1 -347
- package/dist/vectorstores/singlestore.cjs +15 -220
- package/dist/vectorstores/singlestore.d.ts +1 -88
- package/dist/vectorstores/singlestore.js +1 -218
- package/dist/vectorstores/supabase.cjs +15 -217
- package/dist/vectorstores/supabase.d.ts +1 -118
- package/dist/vectorstores/supabase.js +1 -215
- package/dist/vectorstores/tigris.cjs +3 -143
- package/dist/vectorstores/tigris.d.ts +1 -74
- package/dist/vectorstores/tigris.js +1 -130
- package/dist/vectorstores/typeorm.cjs +15 -259
- package/dist/vectorstores/typeorm.d.ts +1 -109
- package/dist/vectorstores/typeorm.js +1 -256
- package/dist/vectorstores/typesense.cjs +15 -223
- package/dist/vectorstores/typesense.d.ts +1 -131
- package/dist/vectorstores/typesense.js +1 -221
- package/dist/vectorstores/usearch.cjs +3 -203
- package/dist/vectorstores/usearch.d.ts +1 -85
- package/dist/vectorstores/usearch.js +1 -187
- package/dist/vectorstores/vectara.cjs +3 -409
- package/dist/vectorstores/vectara.d.ts +1 -148
- package/dist/vectorstores/vectara.js +1 -396
- package/dist/vectorstores/vercel_postgres.cjs +15 -320
- package/dist/vectorstores/vercel_postgres.d.ts +1 -145
- package/dist/vectorstores/vercel_postgres.js +1 -318
- package/dist/vectorstores/voy.cjs +15 -156
- package/dist/vectorstores/voy.d.ts +1 -73
- package/dist/vectorstores/voy.js +1 -154
- package/dist/vectorstores/weaviate.cjs +3 -347
- package/dist/vectorstores/weaviate.d.ts +1 -140
- package/dist/vectorstores/weaviate.js +1 -333
- package/dist/vectorstores/xata.cjs +15 -108
- package/dist/vectorstores/xata.d.ts +1 -70
- package/dist/vectorstores/xata.js +1 -106
- package/dist/vectorstores/zep.cjs +15 -313
- package/dist/vectorstores/zep.d.ts +1 -146
- package/dist/vectorstores/zep.js +1 -311
- package/experimental/masking.cjs +1 -0
- package/experimental/masking.d.ts +1 -0
- package/experimental/masking.js +1 -0
- package/package.json +13 -27
- package/dist/chat_models/googlevertexai/common.cjs +0 -274
- package/dist/chat_models/googlevertexai/common.d.ts +0 -123
- package/dist/chat_models/googlevertexai/common.js +0 -269
- package/dist/chat_models/iflytek_xinghuo/common.cjs +0 -340
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +0 -165
- package/dist/chat_models/iflytek_xinghuo/common.js +0 -336
- package/dist/llms/googlevertexai/common.cjs +0 -168
- package/dist/llms/googlevertexai/common.d.ts +0 -82
- package/dist/llms/googlevertexai/common.js +0 -164
- package/dist/tools/gmail/base.cjs +0 -69
- package/dist/tools/gmail/base.d.ts +0 -19
- package/dist/tools/gmail/base.js +0 -65
- package/dist/tools/gmail/create_draft.cjs +0 -62
- package/dist/tools/gmail/create_draft.d.ts +0 -35
- package/dist/tools/gmail/create_draft.js +0 -58
- package/dist/tools/gmail/descriptions.cjs +0 -118
- package/dist/tools/gmail/descriptions.d.ts +0 -5
- package/dist/tools/gmail/descriptions.js +0 -115
- package/dist/tools/gmail/get_message.cjs +0 -83
- package/dist/tools/gmail/get_message.d.ts +0 -18
- package/dist/tools/gmail/get_message.js +0 -79
- package/dist/tools/gmail/get_thread.cjs +0 -89
- package/dist/tools/gmail/get_thread.d.ts +0 -18
- package/dist/tools/gmail/get_thread.js +0 -85
- package/dist/tools/gmail/search.cjs +0 -118
- package/dist/tools/gmail/search.d.ts +0 -29
- package/dist/tools/gmail/search.js +0 -114
- package/dist/tools/gmail/send_message.cjs +0 -74
- package/dist/tools/gmail/send_message.d.ts +0 -35
- package/dist/tools/gmail/send_message.js +0 -70
- package/dist/util/openai-format-fndef.cjs +0 -81
- package/dist/util/openai-format-fndef.d.ts +0 -44
- package/dist/util/openai-format-fndef.js +0 -77
- package/dist/util/openai.cjs +0 -21
- package/dist/util/openai.d.ts +0 -1
- package/dist/util/openai.js +0 -17
- package/dist/vectorstores/closevector/common.cjs +0 -128
- package/dist/vectorstores/closevector/common.d.ts +0 -82
- package/dist/vectorstores/closevector/common.js +0 -124
|
@@ -1,699 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AIMessage, AIMessageChunk, ChatGenerationChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, } from "../schema/index.js";
|
|
3
|
-
import { formatToOpenAITool } from "../tools/convert_to_openai.js";
|
|
4
|
-
import { getEndpoint } from "../util/azure.js";
|
|
5
|
-
import { getEnvironmentVariable } from "../util/env.js";
|
|
1
|
+
import { ChatOpenAI, messageToOpenAIRole, } from "@langchain/openai";
|
|
6
2
|
import { promptLayerTrackRequest } from "../util/prompt-layer.js";
|
|
7
|
-
|
|
8
|
-
import { wrapOpenAIClientError } from "../util/openai.js";
|
|
9
|
-
import { formatFunctionDefinitions, } from "../util/openai-format-fndef.js";
|
|
10
|
-
function extractGenericMessageCustomRole(message) {
|
|
11
|
-
if (message.role !== "system" &&
|
|
12
|
-
message.role !== "assistant" &&
|
|
13
|
-
message.role !== "user" &&
|
|
14
|
-
message.role !== "function" &&
|
|
15
|
-
message.role !== "tool") {
|
|
16
|
-
console.warn(`Unknown message role: ${message.role}`);
|
|
17
|
-
}
|
|
18
|
-
return message.role;
|
|
19
|
-
}
|
|
20
|
-
function messageToOpenAIRole(message) {
|
|
21
|
-
const type = message._getType();
|
|
22
|
-
switch (type) {
|
|
23
|
-
case "system":
|
|
24
|
-
return "system";
|
|
25
|
-
case "ai":
|
|
26
|
-
return "assistant";
|
|
27
|
-
case "human":
|
|
28
|
-
return "user";
|
|
29
|
-
case "function":
|
|
30
|
-
return "function";
|
|
31
|
-
case "tool":
|
|
32
|
-
return "tool";
|
|
33
|
-
case "generic": {
|
|
34
|
-
if (!ChatMessage.isInstance(message))
|
|
35
|
-
throw new Error("Invalid generic chat message");
|
|
36
|
-
return extractGenericMessageCustomRole(message);
|
|
37
|
-
}
|
|
38
|
-
default:
|
|
39
|
-
throw new Error(`Unknown message type: ${type}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function openAIResponseToChatMessage(message) {
|
|
43
|
-
switch (message.role) {
|
|
44
|
-
case "assistant":
|
|
45
|
-
return new AIMessage(message.content || "", {
|
|
46
|
-
function_call: message.function_call,
|
|
47
|
-
tool_calls: message.tool_calls,
|
|
48
|
-
});
|
|
49
|
-
default:
|
|
50
|
-
return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function _convertDeltaToMessageChunk(
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
-
delta, defaultRole) {
|
|
56
|
-
const role = delta.role ?? defaultRole;
|
|
57
|
-
const content = delta.content ?? "";
|
|
58
|
-
let additional_kwargs;
|
|
59
|
-
if (delta.function_call) {
|
|
60
|
-
additional_kwargs = {
|
|
61
|
-
function_call: delta.function_call,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
else if (delta.tool_calls) {
|
|
65
|
-
additional_kwargs = {
|
|
66
|
-
tool_calls: delta.tool_calls,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
additional_kwargs = {};
|
|
71
|
-
}
|
|
72
|
-
if (role === "user") {
|
|
73
|
-
return new HumanMessageChunk({ content });
|
|
74
|
-
}
|
|
75
|
-
else if (role === "assistant") {
|
|
76
|
-
return new AIMessageChunk({ content, additional_kwargs });
|
|
77
|
-
}
|
|
78
|
-
else if (role === "system") {
|
|
79
|
-
return new SystemMessageChunk({ content });
|
|
80
|
-
}
|
|
81
|
-
else if (role === "function") {
|
|
82
|
-
return new FunctionMessageChunk({
|
|
83
|
-
content,
|
|
84
|
-
additional_kwargs,
|
|
85
|
-
name: delta.name,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
else if (role === "tool") {
|
|
89
|
-
return new ToolMessageChunk({
|
|
90
|
-
content,
|
|
91
|
-
additional_kwargs,
|
|
92
|
-
tool_call_id: delta.tool_call_id,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
return new ChatMessageChunk({ content, role });
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function convertMessagesToOpenAIParams(messages) {
|
|
100
|
-
// TODO: Function messages do not support array content, fix cast
|
|
101
|
-
return messages.map((message) => ({
|
|
102
|
-
role: messageToOpenAIRole(message),
|
|
103
|
-
content: message.content,
|
|
104
|
-
name: message.name,
|
|
105
|
-
function_call: message.additional_kwargs.function_call,
|
|
106
|
-
tool_calls: message.additional_kwargs.tool_calls,
|
|
107
|
-
tool_call_id: message.tool_call_id,
|
|
108
|
-
}));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Wrapper around OpenAI large language models that use the Chat endpoint.
|
|
112
|
-
*
|
|
113
|
-
* To use you should have the `openai` package installed, with the
|
|
114
|
-
* `OPENAI_API_KEY` environment variable set.
|
|
115
|
-
*
|
|
116
|
-
* To use with Azure you should have the `openai` package installed, with the
|
|
117
|
-
* `AZURE_OPENAI_API_KEY`,
|
|
118
|
-
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
119
|
-
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
120
|
-
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
121
|
-
* `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
|
|
122
|
-
*
|
|
123
|
-
* @remarks
|
|
124
|
-
* Any parameters that are valid to be passed to {@link
|
|
125
|
-
* https://platform.openai.com/docs/api-reference/chat/create |
|
|
126
|
-
* `openai.createChatCompletion`} can be passed through {@link modelKwargs}, even
|
|
127
|
-
* if not explicitly available on this class.
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* // Create a new instance of ChatOpenAI with specific temperature and model name settings
|
|
131
|
-
* const model = new ChatOpenAI({
|
|
132
|
-
* temperature: 0.9,
|
|
133
|
-
* modelName: "ft:gpt-3.5-turbo-0613:{ORG_NAME}::{MODEL_ID}",
|
|
134
|
-
* });
|
|
135
|
-
*
|
|
136
|
-
* // Invoke the model with a message and await the response
|
|
137
|
-
* const message = await model.invoke("Hi there!");
|
|
138
|
-
*
|
|
139
|
-
* // Log the response to the console
|
|
140
|
-
* console.log(message);
|
|
141
|
-
*
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
export class ChatOpenAI extends BaseChatModel {
|
|
145
|
-
static lc_name() {
|
|
146
|
-
return "ChatOpenAI";
|
|
147
|
-
}
|
|
148
|
-
get callKeys() {
|
|
149
|
-
return [
|
|
150
|
-
...super.callKeys,
|
|
151
|
-
"options",
|
|
152
|
-
"function_call",
|
|
153
|
-
"functions",
|
|
154
|
-
"tools",
|
|
155
|
-
"tool_choice",
|
|
156
|
-
"promptIndex",
|
|
157
|
-
"response_format",
|
|
158
|
-
"seed",
|
|
159
|
-
];
|
|
160
|
-
}
|
|
161
|
-
get lc_secrets() {
|
|
162
|
-
return {
|
|
163
|
-
openAIApiKey: "OPENAI_API_KEY",
|
|
164
|
-
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
165
|
-
organization: "OPENAI_ORGANIZATION",
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
get lc_aliases() {
|
|
169
|
-
return {
|
|
170
|
-
modelName: "model",
|
|
171
|
-
openAIApiKey: "openai_api_key",
|
|
172
|
-
azureOpenAIApiVersion: "azure_openai_api_version",
|
|
173
|
-
azureOpenAIApiKey: "azure_openai_api_key",
|
|
174
|
-
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
|
|
175
|
-
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
constructor(fields,
|
|
179
|
-
/** @deprecated */
|
|
180
|
-
configuration) {
|
|
181
|
-
super(fields ?? {});
|
|
182
|
-
Object.defineProperty(this, "lc_serializable", {
|
|
183
|
-
enumerable: true,
|
|
184
|
-
configurable: true,
|
|
185
|
-
writable: true,
|
|
186
|
-
value: true
|
|
187
|
-
});
|
|
188
|
-
Object.defineProperty(this, "temperature", {
|
|
189
|
-
enumerable: true,
|
|
190
|
-
configurable: true,
|
|
191
|
-
writable: true,
|
|
192
|
-
value: 1
|
|
193
|
-
});
|
|
194
|
-
Object.defineProperty(this, "topP", {
|
|
195
|
-
enumerable: true,
|
|
196
|
-
configurable: true,
|
|
197
|
-
writable: true,
|
|
198
|
-
value: 1
|
|
199
|
-
});
|
|
200
|
-
Object.defineProperty(this, "frequencyPenalty", {
|
|
201
|
-
enumerable: true,
|
|
202
|
-
configurable: true,
|
|
203
|
-
writable: true,
|
|
204
|
-
value: 0
|
|
205
|
-
});
|
|
206
|
-
Object.defineProperty(this, "presencePenalty", {
|
|
207
|
-
enumerable: true,
|
|
208
|
-
configurable: true,
|
|
209
|
-
writable: true,
|
|
210
|
-
value: 0
|
|
211
|
-
});
|
|
212
|
-
Object.defineProperty(this, "n", {
|
|
213
|
-
enumerable: true,
|
|
214
|
-
configurable: true,
|
|
215
|
-
writable: true,
|
|
216
|
-
value: 1
|
|
217
|
-
});
|
|
218
|
-
Object.defineProperty(this, "logitBias", {
|
|
219
|
-
enumerable: true,
|
|
220
|
-
configurable: true,
|
|
221
|
-
writable: true,
|
|
222
|
-
value: void 0
|
|
223
|
-
});
|
|
224
|
-
Object.defineProperty(this, "modelName", {
|
|
225
|
-
enumerable: true,
|
|
226
|
-
configurable: true,
|
|
227
|
-
writable: true,
|
|
228
|
-
value: "gpt-3.5-turbo"
|
|
229
|
-
});
|
|
230
|
-
Object.defineProperty(this, "modelKwargs", {
|
|
231
|
-
enumerable: true,
|
|
232
|
-
configurable: true,
|
|
233
|
-
writable: true,
|
|
234
|
-
value: void 0
|
|
235
|
-
});
|
|
236
|
-
Object.defineProperty(this, "stop", {
|
|
237
|
-
enumerable: true,
|
|
238
|
-
configurable: true,
|
|
239
|
-
writable: true,
|
|
240
|
-
value: void 0
|
|
241
|
-
});
|
|
242
|
-
Object.defineProperty(this, "user", {
|
|
243
|
-
enumerable: true,
|
|
244
|
-
configurable: true,
|
|
245
|
-
writable: true,
|
|
246
|
-
value: void 0
|
|
247
|
-
});
|
|
248
|
-
Object.defineProperty(this, "timeout", {
|
|
249
|
-
enumerable: true,
|
|
250
|
-
configurable: true,
|
|
251
|
-
writable: true,
|
|
252
|
-
value: void 0
|
|
253
|
-
});
|
|
254
|
-
Object.defineProperty(this, "streaming", {
|
|
255
|
-
enumerable: true,
|
|
256
|
-
configurable: true,
|
|
257
|
-
writable: true,
|
|
258
|
-
value: false
|
|
259
|
-
});
|
|
260
|
-
Object.defineProperty(this, "maxTokens", {
|
|
261
|
-
enumerable: true,
|
|
262
|
-
configurable: true,
|
|
263
|
-
writable: true,
|
|
264
|
-
value: void 0
|
|
265
|
-
});
|
|
266
|
-
Object.defineProperty(this, "openAIApiKey", {
|
|
267
|
-
enumerable: true,
|
|
268
|
-
configurable: true,
|
|
269
|
-
writable: true,
|
|
270
|
-
value: void 0
|
|
271
|
-
});
|
|
272
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
273
|
-
enumerable: true,
|
|
274
|
-
configurable: true,
|
|
275
|
-
writable: true,
|
|
276
|
-
value: void 0
|
|
277
|
-
});
|
|
278
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
279
|
-
enumerable: true,
|
|
280
|
-
configurable: true,
|
|
281
|
-
writable: true,
|
|
282
|
-
value: void 0
|
|
283
|
-
});
|
|
284
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
285
|
-
enumerable: true,
|
|
286
|
-
configurable: true,
|
|
287
|
-
writable: true,
|
|
288
|
-
value: void 0
|
|
289
|
-
});
|
|
290
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
291
|
-
enumerable: true,
|
|
292
|
-
configurable: true,
|
|
293
|
-
writable: true,
|
|
294
|
-
value: void 0
|
|
295
|
-
});
|
|
296
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
297
|
-
enumerable: true,
|
|
298
|
-
configurable: true,
|
|
299
|
-
writable: true,
|
|
300
|
-
value: void 0
|
|
301
|
-
});
|
|
302
|
-
Object.defineProperty(this, "organization", {
|
|
303
|
-
enumerable: true,
|
|
304
|
-
configurable: true,
|
|
305
|
-
writable: true,
|
|
306
|
-
value: void 0
|
|
307
|
-
});
|
|
308
|
-
Object.defineProperty(this, "client", {
|
|
309
|
-
enumerable: true,
|
|
310
|
-
configurable: true,
|
|
311
|
-
writable: true,
|
|
312
|
-
value: void 0
|
|
313
|
-
});
|
|
314
|
-
Object.defineProperty(this, "clientConfig", {
|
|
315
|
-
enumerable: true,
|
|
316
|
-
configurable: true,
|
|
317
|
-
writable: true,
|
|
318
|
-
value: void 0
|
|
319
|
-
});
|
|
320
|
-
this.openAIApiKey =
|
|
321
|
-
fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
|
|
322
|
-
this.azureOpenAIApiKey =
|
|
323
|
-
fields?.azureOpenAIApiKey ??
|
|
324
|
-
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
325
|
-
if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
|
|
326
|
-
throw new Error("OpenAI or Azure OpenAI API key not found");
|
|
327
|
-
}
|
|
328
|
-
this.azureOpenAIApiInstanceName =
|
|
329
|
-
fields?.azureOpenAIApiInstanceName ??
|
|
330
|
-
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
331
|
-
this.azureOpenAIApiDeploymentName =
|
|
332
|
-
fields?.azureOpenAIApiDeploymentName ??
|
|
333
|
-
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME");
|
|
334
|
-
this.azureOpenAIApiVersion =
|
|
335
|
-
fields?.azureOpenAIApiVersion ??
|
|
336
|
-
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
337
|
-
this.azureOpenAIBasePath =
|
|
338
|
-
fields?.azureOpenAIBasePath ??
|
|
339
|
-
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
340
|
-
this.organization =
|
|
341
|
-
fields?.configuration?.organization ??
|
|
342
|
-
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
343
|
-
this.modelName = fields?.modelName ?? this.modelName;
|
|
344
|
-
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
345
|
-
this.timeout = fields?.timeout;
|
|
346
|
-
this.temperature = fields?.temperature ?? this.temperature;
|
|
347
|
-
this.topP = fields?.topP ?? this.topP;
|
|
348
|
-
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
349
|
-
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
350
|
-
this.maxTokens = fields?.maxTokens;
|
|
351
|
-
this.n = fields?.n ?? this.n;
|
|
352
|
-
this.logitBias = fields?.logitBias;
|
|
353
|
-
this.stop = fields?.stop;
|
|
354
|
-
this.user = fields?.user;
|
|
355
|
-
this.streaming = fields?.streaming ?? false;
|
|
356
|
-
if (this.azureOpenAIApiKey) {
|
|
357
|
-
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
358
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
359
|
-
}
|
|
360
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
361
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
362
|
-
}
|
|
363
|
-
if (!this.azureOpenAIApiVersion) {
|
|
364
|
-
throw new Error("Azure OpenAI API version not found");
|
|
365
|
-
}
|
|
366
|
-
this.openAIApiKey = this.openAIApiKey ?? "";
|
|
367
|
-
}
|
|
368
|
-
this.clientConfig = {
|
|
369
|
-
apiKey: this.openAIApiKey,
|
|
370
|
-
organization: this.organization,
|
|
371
|
-
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
372
|
-
dangerouslyAllowBrowser: true,
|
|
373
|
-
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
374
|
-
fields?.configuration?.baseOptions?.headers,
|
|
375
|
-
defaultQuery: configuration?.baseOptions?.params ??
|
|
376
|
-
fields?.configuration?.baseOptions?.params,
|
|
377
|
-
...configuration,
|
|
378
|
-
...fields?.configuration,
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Get the parameters used to invoke the model
|
|
383
|
-
*/
|
|
384
|
-
invocationParams(options) {
|
|
385
|
-
function isStructuredToolArray(tools) {
|
|
386
|
-
return (tools !== undefined &&
|
|
387
|
-
tools.every((tool) => Array.isArray(tool.lc_namespace)));
|
|
388
|
-
}
|
|
389
|
-
const params = {
|
|
390
|
-
model: this.modelName,
|
|
391
|
-
temperature: this.temperature,
|
|
392
|
-
top_p: this.topP,
|
|
393
|
-
frequency_penalty: this.frequencyPenalty,
|
|
394
|
-
presence_penalty: this.presencePenalty,
|
|
395
|
-
max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
|
|
396
|
-
n: this.n,
|
|
397
|
-
logit_bias: this.logitBias,
|
|
398
|
-
stop: options?.stop ?? this.stop,
|
|
399
|
-
user: this.user,
|
|
400
|
-
stream: this.streaming,
|
|
401
|
-
functions: options?.functions,
|
|
402
|
-
function_call: options?.function_call,
|
|
403
|
-
tools: isStructuredToolArray(options?.tools)
|
|
404
|
-
? options?.tools.map(formatToOpenAITool)
|
|
405
|
-
: options?.tools,
|
|
406
|
-
tool_choice: options?.tool_choice,
|
|
407
|
-
response_format: options?.response_format,
|
|
408
|
-
seed: options?.seed,
|
|
409
|
-
...this.modelKwargs,
|
|
410
|
-
};
|
|
411
|
-
return params;
|
|
412
|
-
}
|
|
413
|
-
/** @ignore */
|
|
414
|
-
_identifyingParams() {
|
|
415
|
-
return {
|
|
416
|
-
model_name: this.modelName,
|
|
417
|
-
...this.invocationParams(),
|
|
418
|
-
...this.clientConfig,
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
async *_streamResponseChunks(messages, options, runManager) {
|
|
422
|
-
const messagesMapped = convertMessagesToOpenAIParams(messages);
|
|
423
|
-
const params = {
|
|
424
|
-
...this.invocationParams(options),
|
|
425
|
-
messages: messagesMapped,
|
|
426
|
-
stream: true,
|
|
427
|
-
};
|
|
428
|
-
let defaultRole;
|
|
429
|
-
const streamIterable = await this.completionWithRetry(params, options);
|
|
430
|
-
for await (const data of streamIterable) {
|
|
431
|
-
const choice = data?.choices[0];
|
|
432
|
-
if (!choice) {
|
|
433
|
-
continue;
|
|
434
|
-
}
|
|
435
|
-
const { delta } = choice;
|
|
436
|
-
if (!delta) {
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
const chunk = _convertDeltaToMessageChunk(delta, defaultRole);
|
|
440
|
-
defaultRole = delta.role ?? defaultRole;
|
|
441
|
-
const newTokenIndices = {
|
|
442
|
-
prompt: options.promptIndex ?? 0,
|
|
443
|
-
completion: choice.index ?? 0,
|
|
444
|
-
};
|
|
445
|
-
if (typeof chunk.content !== "string") {
|
|
446
|
-
console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
|
|
447
|
-
continue;
|
|
448
|
-
}
|
|
449
|
-
const generationChunk = new ChatGenerationChunk({
|
|
450
|
-
message: chunk,
|
|
451
|
-
text: chunk.content,
|
|
452
|
-
generationInfo: newTokenIndices,
|
|
453
|
-
});
|
|
454
|
-
yield generationChunk;
|
|
455
|
-
// eslint-disable-next-line no-void
|
|
456
|
-
void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
457
|
-
}
|
|
458
|
-
if (options.signal?.aborted) {
|
|
459
|
-
throw new Error("AbortError");
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Get the identifying parameters for the model
|
|
464
|
-
*
|
|
465
|
-
*/
|
|
466
|
-
identifyingParams() {
|
|
467
|
-
return this._identifyingParams();
|
|
468
|
-
}
|
|
469
|
-
/** @ignore */
|
|
470
|
-
async _generate(messages, options, runManager) {
|
|
471
|
-
const tokenUsage = {};
|
|
472
|
-
const params = this.invocationParams(options);
|
|
473
|
-
const messagesMapped = convertMessagesToOpenAIParams(messages);
|
|
474
|
-
if (params.stream) {
|
|
475
|
-
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
476
|
-
const finalChunks = {};
|
|
477
|
-
for await (const chunk of stream) {
|
|
478
|
-
const index = chunk.generationInfo?.completion ?? 0;
|
|
479
|
-
if (finalChunks[index] === undefined) {
|
|
480
|
-
finalChunks[index] = chunk;
|
|
481
|
-
}
|
|
482
|
-
else {
|
|
483
|
-
finalChunks[index] = finalChunks[index].concat(chunk);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
const generations = Object.entries(finalChunks)
|
|
487
|
-
.sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
|
|
488
|
-
.map(([_, value]) => value);
|
|
489
|
-
const { functions, function_call } = this.invocationParams(options);
|
|
490
|
-
// OpenAI does not support token usage report under stream mode,
|
|
491
|
-
// fallback to estimation.
|
|
492
|
-
const promptTokenUsage = await this.getEstimatedTokenCountFromPrompt(messages, functions, function_call);
|
|
493
|
-
const completionTokenUsage = await this.getNumTokensFromGenerations(generations);
|
|
494
|
-
tokenUsage.promptTokens = promptTokenUsage;
|
|
495
|
-
tokenUsage.completionTokens = completionTokenUsage;
|
|
496
|
-
tokenUsage.totalTokens = promptTokenUsage + completionTokenUsage;
|
|
497
|
-
return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
const data = await this.completionWithRetry({
|
|
501
|
-
...params,
|
|
502
|
-
stream: false,
|
|
503
|
-
messages: messagesMapped,
|
|
504
|
-
}, {
|
|
505
|
-
signal: options?.signal,
|
|
506
|
-
...options?.options,
|
|
507
|
-
});
|
|
508
|
-
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data?.usage ?? {};
|
|
509
|
-
if (completionTokens) {
|
|
510
|
-
tokenUsage.completionTokens =
|
|
511
|
-
(tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
512
|
-
}
|
|
513
|
-
if (promptTokens) {
|
|
514
|
-
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
515
|
-
}
|
|
516
|
-
if (totalTokens) {
|
|
517
|
-
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
518
|
-
}
|
|
519
|
-
const generations = [];
|
|
520
|
-
for (const part of data?.choices ?? []) {
|
|
521
|
-
const text = part.message?.content ?? "";
|
|
522
|
-
const generation = {
|
|
523
|
-
text,
|
|
524
|
-
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
|
|
525
|
-
};
|
|
526
|
-
if (part.finish_reason) {
|
|
527
|
-
generation.generationInfo = { finish_reason: part.finish_reason };
|
|
528
|
-
}
|
|
529
|
-
generations.push(generation);
|
|
530
|
-
}
|
|
531
|
-
return {
|
|
532
|
-
generations,
|
|
533
|
-
llmOutput: { tokenUsage },
|
|
534
|
-
};
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Estimate the number of tokens a prompt will use.
|
|
539
|
-
* Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
|
|
540
|
-
*/
|
|
541
|
-
async getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
542
|
-
// It appears that if functions are present, the first system message is padded with a trailing newline. This
|
|
543
|
-
// was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
|
|
544
|
-
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
545
|
-
// If there are functions, add the function definitions as they count towards token usage
|
|
546
|
-
if (functions && function_call !== "auto") {
|
|
547
|
-
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
548
|
-
tokens += await this.getNumTokens(promptDefinitions);
|
|
549
|
-
tokens += 9; // Add nine per completion
|
|
550
|
-
}
|
|
551
|
-
// If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
|
|
552
|
-
// functions typically add a system message, but reuse the first one if it's already there. This offsets
|
|
553
|
-
// the extra 9 tokens added by the function definitions.
|
|
554
|
-
if (functions && messages.find((m) => m._getType() === "system")) {
|
|
555
|
-
tokens -= 4;
|
|
556
|
-
}
|
|
557
|
-
// If function_call is 'none', add one token.
|
|
558
|
-
// If it's a FunctionCall object, add 4 + the number of tokens in the function name.
|
|
559
|
-
// If it's undefined or 'auto', don't add anything.
|
|
560
|
-
if (function_call === "none") {
|
|
561
|
-
tokens += 1;
|
|
562
|
-
}
|
|
563
|
-
else if (typeof function_call === "object") {
|
|
564
|
-
tokens += (await this.getNumTokens(function_call.name)) + 4;
|
|
565
|
-
}
|
|
566
|
-
return tokens;
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Estimate the number of tokens an array of generations have used.
|
|
570
|
-
*/
|
|
571
|
-
async getNumTokensFromGenerations(generations) {
|
|
572
|
-
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
573
|
-
if (generation.message.additional_kwargs?.function_call) {
|
|
574
|
-
return (await this.getNumTokensFromMessages([generation.message]))
|
|
575
|
-
.countPerMessage[0];
|
|
576
|
-
}
|
|
577
|
-
else {
|
|
578
|
-
return await this.getNumTokens(generation.message.content);
|
|
579
|
-
}
|
|
580
|
-
}));
|
|
581
|
-
return generationUsages.reduce((a, b) => a + b, 0);
|
|
582
|
-
}
|
|
583
|
-
async getNumTokensFromMessages(messages) {
|
|
584
|
-
let totalCount = 0;
|
|
585
|
-
let tokensPerMessage = 0;
|
|
586
|
-
let tokensPerName = 0;
|
|
587
|
-
// From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
|
588
|
-
if (this.modelName === "gpt-3.5-turbo-0301") {
|
|
589
|
-
tokensPerMessage = 4;
|
|
590
|
-
tokensPerName = -1;
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
593
|
-
tokensPerMessage = 3;
|
|
594
|
-
tokensPerName = 1;
|
|
595
|
-
}
|
|
596
|
-
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
597
|
-
const textCount = await this.getNumTokens(message.content);
|
|
598
|
-
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
599
|
-
const nameCount = message.name !== undefined
|
|
600
|
-
? tokensPerName + (await this.getNumTokens(message.name))
|
|
601
|
-
: 0;
|
|
602
|
-
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
603
|
-
// From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
|
|
604
|
-
const openAIMessage = message;
|
|
605
|
-
if (openAIMessage._getType() === "function") {
|
|
606
|
-
count -= 2;
|
|
607
|
-
}
|
|
608
|
-
if (openAIMessage.additional_kwargs?.function_call) {
|
|
609
|
-
count += 3;
|
|
610
|
-
}
|
|
611
|
-
if (openAIMessage?.additional_kwargs.function_call?.name) {
|
|
612
|
-
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
613
|
-
}
|
|
614
|
-
if (openAIMessage.additional_kwargs.function_call?.arguments) {
|
|
615
|
-
count += await this.getNumTokens(
|
|
616
|
-
// Remove newlines and spaces
|
|
617
|
-
JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
618
|
-
}
|
|
619
|
-
totalCount += count;
|
|
620
|
-
return count;
|
|
621
|
-
}));
|
|
622
|
-
totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
|
|
623
|
-
return { totalCount, countPerMessage };
|
|
624
|
-
}
|
|
625
|
-
async completionWithRetry(request, options) {
|
|
626
|
-
const requestOptions = this._getClientOptions(options);
|
|
627
|
-
return this.caller.call(async () => {
|
|
628
|
-
try {
|
|
629
|
-
const res = await this.client.chat.completions.create(request, requestOptions);
|
|
630
|
-
return res;
|
|
631
|
-
}
|
|
632
|
-
catch (e) {
|
|
633
|
-
const error = wrapOpenAIClientError(e);
|
|
634
|
-
throw error;
|
|
635
|
-
}
|
|
636
|
-
});
|
|
637
|
-
}
|
|
638
|
-
_getClientOptions(options) {
|
|
639
|
-
if (!this.client) {
|
|
640
|
-
const openAIEndpointConfig = {
|
|
641
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
642
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
643
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
644
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
645
|
-
baseURL: this.clientConfig.baseURL,
|
|
646
|
-
};
|
|
647
|
-
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
648
|
-
const params = {
|
|
649
|
-
...this.clientConfig,
|
|
650
|
-
baseURL: endpoint,
|
|
651
|
-
timeout: this.timeout,
|
|
652
|
-
maxRetries: 0,
|
|
653
|
-
};
|
|
654
|
-
if (!params.baseURL) {
|
|
655
|
-
delete params.baseURL;
|
|
656
|
-
}
|
|
657
|
-
this.client = new OpenAIClient(params);
|
|
658
|
-
}
|
|
659
|
-
const requestOptions = {
|
|
660
|
-
...this.clientConfig,
|
|
661
|
-
...options,
|
|
662
|
-
};
|
|
663
|
-
if (this.azureOpenAIApiKey) {
|
|
664
|
-
requestOptions.headers = {
|
|
665
|
-
"api-key": this.azureOpenAIApiKey,
|
|
666
|
-
...requestOptions.headers,
|
|
667
|
-
};
|
|
668
|
-
requestOptions.query = {
|
|
669
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
670
|
-
...requestOptions.query,
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
|
-
return requestOptions;
|
|
674
|
-
}
|
|
675
|
-
_llmType() {
|
|
676
|
-
return "openai";
|
|
677
|
-
}
|
|
678
|
-
/** @ignore */
|
|
679
|
-
_combineLLMOutput(...llmOutputs) {
|
|
680
|
-
return llmOutputs.reduce((acc, llmOutput) => {
|
|
681
|
-
if (llmOutput && llmOutput.tokenUsage) {
|
|
682
|
-
acc.tokenUsage.completionTokens +=
|
|
683
|
-
llmOutput.tokenUsage.completionTokens ?? 0;
|
|
684
|
-
acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
|
|
685
|
-
acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
|
|
686
|
-
}
|
|
687
|
-
return acc;
|
|
688
|
-
}, {
|
|
689
|
-
tokenUsage: {
|
|
690
|
-
completionTokens: 0,
|
|
691
|
-
promptTokens: 0,
|
|
692
|
-
totalTokens: 0,
|
|
693
|
-
},
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
}
|
|
3
|
+
export { ChatOpenAI };
|
|
697
4
|
export class PromptLayerChatOpenAI extends ChatOpenAI {
|
|
698
5
|
constructor(fields) {
|
|
699
6
|
super(fields);
|