@ax-llm/ax 9.0.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/LICENSE +201 -0
- package/README.md +547 -0
- package/build/module/docs/assets/icons.d.ts +1 -0
- package/build/module/docs/assets/icons.js +19 -0
- package/build/module/docs/assets/icons.js.map +1 -0
- package/build/module/docs/assets/main.d.ts +1 -0
- package/build/module/docs/assets/main.js +629 -0
- package/build/module/docs/assets/main.js.map +1 -0
- package/build/module/docs/assets/navigation.d.ts +1 -0
- package/build/module/docs/assets/navigation.js +3 -0
- package/build/module/docs/assets/navigation.js.map +1 -0
- package/build/module/docs/assets/search.d.ts +1 -0
- package/build/module/docs/assets/search.js +3 -0
- package/build/module/docs/assets/search.js.map +1 -0
- package/build/module/src/ai/anthropic/api.d.ts +24 -0
- package/build/module/src/ai/anthropic/api.js +188 -0
- package/build/module/src/ai/anthropic/api.js.map +1 -0
- package/build/module/src/ai/anthropic/index.d.ts +2 -0
- package/build/module/src/ai/anthropic/index.js +3 -0
- package/build/module/src/ai/anthropic/index.js.map +1 -0
- package/build/module/src/ai/anthropic/info.d.ts +2 -0
- package/build/module/src/ai/anthropic/info.js +34 -0
- package/build/module/src/ai/anthropic/info.js.map +1 -0
- package/build/module/src/ai/anthropic/types.d.ts +140 -0
- package/build/module/src/ai/anthropic/types.js +13 -0
- package/build/module/src/ai/anthropic/types.js.map +1 -0
- package/build/module/src/ai/azure-openai/api.d.ts +34 -0
- package/build/module/src/ai/azure-openai/api.js +42 -0
- package/build/module/src/ai/azure-openai/api.js.map +1 -0
- package/build/module/src/ai/azure-openai/index.d.ts +1 -0
- package/build/module/src/ai/azure-openai/index.js +2 -0
- package/build/module/src/ai/azure-openai/index.js.map +1 -0
- package/build/module/src/ai/balance.d.ts +26 -0
- package/build/module/src/ai/balance.js +87 -0
- package/build/module/src/ai/balance.js.map +1 -0
- package/build/module/src/ai/base.d.ts +56 -0
- package/build/module/src/ai/base.js +328 -0
- package/build/module/src/ai/base.js.map +1 -0
- package/build/module/src/ai/cohere/api.d.ts +35 -0
- package/build/module/src/ai/cohere/api.js +214 -0
- package/build/module/src/ai/cohere/api.js.map +1 -0
- package/build/module/src/ai/cohere/index.d.ts +2 -0
- package/build/module/src/ai/cohere/index.js +3 -0
- package/build/module/src/ai/cohere/index.js.map +1 -0
- package/build/module/src/ai/cohere/info.d.ts +2 -0
- package/build/module/src/ai/cohere/info.js +52 -0
- package/build/module/src/ai/cohere/info.js.map +1 -0
- package/build/module/src/ai/cohere/types.d.ts +85 -0
- package/build/module/src/ai/cohere/types.js +23 -0
- package/build/module/src/ai/cohere/types.js.map +1 -0
- package/build/module/src/ai/deepseek/api.d.ts +27 -0
- package/build/module/src/ai/deepseek/api.js +38 -0
- package/build/module/src/ai/deepseek/api.js.map +1 -0
- package/build/module/src/ai/deepseek/index.d.ts +2 -0
- package/build/module/src/ai/deepseek/index.js +3 -0
- package/build/module/src/ai/deepseek/index.js.map +1 -0
- package/build/module/src/ai/deepseek/info.d.ts +2 -0
- package/build/module/src/ai/deepseek/info.js +16 -0
- package/build/module/src/ai/deepseek/info.js.map +1 -0
- package/build/module/src/ai/deepseek/types.d.ts +8 -0
- package/build/module/src/ai/deepseek/types.js +10 -0
- package/build/module/src/ai/deepseek/types.js.map +1 -0
- package/build/module/src/ai/google-gemini/api.d.ts +34 -0
- package/build/module/src/ai/google-gemini/api.js +268 -0
- package/build/module/src/ai/google-gemini/api.js.map +1 -0
- package/build/module/src/ai/google-gemini/index.d.ts +2 -0
- package/build/module/src/ai/google-gemini/index.js +3 -0
- package/build/module/src/ai/google-gemini/index.js.map +1 -0
- package/build/module/src/ai/google-gemini/info.d.ts +6 -0
- package/build/module/src/ai/google-gemini/info.js +29 -0
- package/build/module/src/ai/google-gemini/info.js.map +1 -0
- package/build/module/src/ai/google-gemini/types.d.ts +151 -0
- package/build/module/src/ai/google-gemini/types.js +38 -0
- package/build/module/src/ai/google-gemini/types.js.map +1 -0
- package/build/module/src/ai/groq/api.d.ts +22 -0
- package/build/module/src/ai/groq/api.js +29 -0
- package/build/module/src/ai/groq/api.js.map +1 -0
- package/build/module/src/ai/groq/index.d.ts +1 -0
- package/build/module/src/ai/groq/index.js +2 -0
- package/build/module/src/ai/groq/index.js.map +1 -0
- package/build/module/src/ai/huggingface/api.d.ts +32 -0
- package/build/module/src/ai/huggingface/api.js +99 -0
- package/build/module/src/ai/huggingface/api.js.map +1 -0
- package/build/module/src/ai/huggingface/index.d.ts +2 -0
- package/build/module/src/ai/huggingface/index.js +3 -0
- package/build/module/src/ai/huggingface/index.js.map +1 -0
- package/build/module/src/ai/huggingface/info.d.ts +6 -0
- package/build/module/src/ai/huggingface/info.js +6 -0
- package/build/module/src/ai/huggingface/info.js.map +1 -0
- package/build/module/src/ai/huggingface/types.d.ts +42 -0
- package/build/module/src/ai/huggingface/types.js +9 -0
- package/build/module/src/ai/huggingface/types.js.map +1 -0
- package/build/module/src/ai/index.d.ts +28 -0
- package/build/module/src/ai/index.js +54 -0
- package/build/module/src/ai/index.js.map +1 -0
- package/build/module/src/ai/mistral/api.d.ts +27 -0
- package/build/module/src/ai/mistral/api.js +38 -0
- package/build/module/src/ai/mistral/api.js.map +1 -0
- package/build/module/src/ai/mistral/index.d.ts +2 -0
- package/build/module/src/ai/mistral/index.js +3 -0
- package/build/module/src/ai/mistral/index.js.map +1 -0
- package/build/module/src/ai/mistral/info.d.ts +2 -0
- package/build/module/src/ai/mistral/info.js +34 -0
- package/build/module/src/ai/mistral/info.js.map +1 -0
- package/build/module/src/ai/mistral/types.d.ts +18 -0
- package/build/module/src/ai/mistral/types.js +21 -0
- package/build/module/src/ai/mistral/types.js.map +1 -0
- package/build/module/src/ai/ollama/api.d.ts +24 -0
- package/build/module/src/ai/ollama/api.js +28 -0
- package/build/module/src/ai/ollama/api.js.map +1 -0
- package/build/module/src/ai/ollama/index.d.ts +1 -0
- package/build/module/src/ai/ollama/index.js +2 -0
- package/build/module/src/ai/ollama/index.js.map +1 -0
- package/build/module/src/ai/openai/api.d.ts +49 -0
- package/build/module/src/ai/openai/api.js +333 -0
- package/build/module/src/ai/openai/api.js.map +1 -0
- package/build/module/src/ai/openai/index.d.ts +2 -0
- package/build/module/src/ai/openai/index.js +3 -0
- package/build/module/src/ai/openai/index.js.map +1 -0
- package/build/module/src/ai/openai/info.d.ts +6 -0
- package/build/module/src/ai/openai/info.js +50 -0
- package/build/module/src/ai/openai/info.js.map +1 -0
- package/build/module/src/ai/openai/types.d.ts +192 -0
- package/build/module/src/ai/openai/types.js +35 -0
- package/build/module/src/ai/openai/types.js.map +1 -0
- package/build/module/src/ai/together/api.d.ts +22 -0
- package/build/module/src/ai/together/api.js +29 -0
- package/build/module/src/ai/together/api.js.map +1 -0
- package/build/module/src/ai/together/index.d.ts +1 -0
- package/build/module/src/ai/together/index.js +2 -0
- package/build/module/src/ai/together/index.js.map +1 -0
- package/build/module/src/ai/together/info.d.ts +2 -0
- package/build/module/src/ai/together/info.js +2 -0
- package/build/module/src/ai/together/info.js.map +1 -0
- package/build/module/src/ai/types.d.ts +70 -0
- package/build/module/src/ai/types.js +2 -0
- package/build/module/src/ai/types.js.map +1 -0
- package/build/module/src/ai/util.d.ts +13 -0
- package/build/module/src/ai/util.js +112 -0
- package/build/module/src/ai/util.js.map +1 -0
- package/build/module/src/db/base.d.ts +24 -0
- package/build/module/src/db/base.js +90 -0
- package/build/module/src/db/base.js.map +1 -0
- package/build/module/src/db/cloudflare.d.ts +20 -0
- package/build/module/src/db/cloudflare.js +98 -0
- package/build/module/src/db/cloudflare.js.map +1 -0
- package/build/module/src/db/index.d.ts +12 -0
- package/build/module/src/db/index.js +23 -0
- package/build/module/src/db/index.js.map +1 -0
- package/build/module/src/db/memory.d.ts +23 -0
- package/build/module/src/db/memory.js +125 -0
- package/build/module/src/db/memory.js.map +1 -0
- package/build/module/src/db/pinecone.d.ts +20 -0
- package/build/module/src/db/pinecone.js +71 -0
- package/build/module/src/db/pinecone.js.map +1 -0
- package/build/module/src/db/types.d.ts +35 -0
- package/build/module/src/db/types.js +3 -0
- package/build/module/src/db/types.js.map +1 -0
- package/build/module/src/db/weaviate.d.ts +20 -0
- package/build/module/src/db/weaviate.js +127 -0
- package/build/module/src/db/weaviate.js.map +1 -0
- package/build/module/src/docs/index.d.ts +4 -0
- package/build/module/src/docs/index.js +5 -0
- package/build/module/src/docs/index.js.map +1 -0
- package/build/module/src/docs/manager.d.ts +45 -0
- package/build/module/src/docs/manager.js +159 -0
- package/build/module/src/docs/manager.js.map +1 -0
- package/build/module/src/docs/reranker.d.ts +7 -0
- package/build/module/src/docs/reranker.js +26 -0
- package/build/module/src/docs/reranker.js.map +1 -0
- package/build/module/src/docs/rewriter.d.ts +4 -0
- package/build/module/src/docs/rewriter.js +9 -0
- package/build/module/src/docs/rewriter.js.map +1 -0
- package/build/module/src/docs/tika.d.ts +21 -0
- package/build/module/src/docs/tika.js +46 -0
- package/build/module/src/docs/tika.js.map +1 -0
- package/build/module/src/dsp/asserts.d.ts +32 -0
- package/build/module/src/dsp/asserts.js +88 -0
- package/build/module/src/dsp/asserts.js.map +1 -0
- package/build/module/src/dsp/eval.d.ts +62 -0
- package/build/module/src/dsp/eval.js +120 -0
- package/build/module/src/dsp/eval.js.map +1 -0
- package/build/module/src/dsp/evaluate.d.ts +12 -0
- package/build/module/src/dsp/evaluate.js +32 -0
- package/build/module/src/dsp/evaluate.js.map +1 -0
- package/build/module/src/dsp/extract.d.ts +25 -0
- package/build/module/src/dsp/extract.js +152 -0
- package/build/module/src/dsp/extract.js.map +1 -0
- package/build/module/src/dsp/generate.d.ts +52 -0
- package/build/module/src/dsp/generate.js +335 -0
- package/build/module/src/dsp/generate.js.map +1 -0
- package/build/module/src/dsp/generate.test.d.ts +1 -0
- package/build/module/src/dsp/generate.test.js +41 -0
- package/build/module/src/dsp/generate.test.js.map +1 -0
- package/build/module/src/dsp/index.d.ts +14 -0
- package/build/module/src/dsp/index.js +10 -0
- package/build/module/src/dsp/index.js.map +1 -0
- package/build/module/src/dsp/loader.d.ts +25 -0
- package/build/module/src/dsp/loader.js +87 -0
- package/build/module/src/dsp/loader.js.map +1 -0
- package/build/module/src/dsp/optimize.d.ts +29 -0
- package/build/module/src/dsp/optimize.js +100 -0
- package/build/module/src/dsp/optimize.js.map +1 -0
- package/build/module/src/dsp/parser.d.ts +86 -0
- package/build/module/src/dsp/parser.js +1437 -0
- package/build/module/src/dsp/parser.js.map +1 -0
- package/build/module/src/dsp/program.d.ts +64 -0
- package/build/module/src/dsp/program.js +129 -0
- package/build/module/src/dsp/program.js.map +1 -0
- package/build/module/src/dsp/prompt.d.ts +25 -0
- package/build/module/src/dsp/prompt.js +178 -0
- package/build/module/src/dsp/prompt.js.map +1 -0
- package/build/module/src/dsp/registry.d.ts +6 -0
- package/build/module/src/dsp/registry.js +15 -0
- package/build/module/src/dsp/registry.js.map +1 -0
- package/build/module/src/dsp/router.d.ts +24 -0
- package/build/module/src/dsp/router.js +72 -0
- package/build/module/src/dsp/router.js.map +1 -0
- package/build/module/src/dsp/sig.d.ts +37 -0
- package/build/module/src/dsp/sig.js +160 -0
- package/build/module/src/dsp/sig.js.map +1 -0
- package/build/module/src/dsp/sig.test.d.ts +1 -0
- package/build/module/src/dsp/sig.test.js +25 -0
- package/build/module/src/dsp/sig.test.js.map +1 -0
- package/build/module/src/dsp/stopwords.d.ts +1 -0
- package/build/module/src/dsp/stopwords.js +1163 -0
- package/build/module/src/dsp/stopwords.js.map +1 -0
- package/build/module/src/dsp/strutil.d.ts +9 -0
- package/build/module/src/dsp/strutil.js +52 -0
- package/build/module/src/dsp/strutil.js.map +1 -0
- package/build/module/src/dsp/util.d.ts +1 -0
- package/build/module/src/dsp/util.js +14 -0
- package/build/module/src/dsp/util.js.map +1 -0
- package/build/module/src/examples/agent.d.ts +1 -0
- package/build/module/src/examples/agent.js +24 -0
- package/build/module/src/examples/agent.js.map +1 -0
- package/build/module/src/examples/chain-of-thought.d.ts +1 -0
- package/build/module/src/examples/chain-of-thought.js +18 -0
- package/build/module/src/examples/chain-of-thought.js.map +1 -0
- package/build/module/src/examples/customer-support.d.ts +1 -0
- package/build/module/src/examples/customer-support.js +22 -0
- package/build/module/src/examples/customer-support.js.map +1 -0
- package/build/module/src/examples/fibonacci.d.ts +1 -0
- package/build/module/src/examples/fibonacci.js +9 -0
- package/build/module/src/examples/fibonacci.js.map +1 -0
- package/build/module/src/examples/food-search.d.ts +1 -0
- package/build/module/src/examples/food-search.js +137 -0
- package/build/module/src/examples/food-search.js.map +1 -0
- package/build/module/src/examples/marketing.d.ts +1 -0
- package/build/module/src/examples/marketing.js +26 -0
- package/build/module/src/examples/marketing.js.map +1 -0
- package/build/module/src/examples/meetings.d.ts +1 -0
- package/build/module/src/examples/meetings.js +149 -0
- package/build/module/src/examples/meetings.js.map +1 -0
- package/build/module/src/examples/qna-tune.d.ts +1 -0
- package/build/module/src/examples/qna-tune.js +29 -0
- package/build/module/src/examples/qna-tune.js.map +1 -0
- package/build/module/src/examples/qna-use-tuned.d.ts +1 -0
- package/build/module/src/examples/qna-use-tuned.js +25 -0
- package/build/module/src/examples/qna-use-tuned.js.map +1 -0
- package/build/module/src/examples/rag-docs.d.ts +1 -0
- package/build/module/src/examples/rag-docs.js +14 -0
- package/build/module/src/examples/rag-docs.js.map +1 -0
- package/build/module/src/examples/rag.d.ts +1 -0
- package/build/module/src/examples/rag.js +14 -0
- package/build/module/src/examples/rag.js.map +1 -0
- package/build/module/src/examples/react.d.ts +1 -0
- package/build/module/src/examples/react.js +36 -0
- package/build/module/src/examples/react.js.map +1 -0
- package/build/module/src/examples/routing.d.ts +1 -0
- package/build/module/src/examples/routing.js +48 -0
- package/build/module/src/examples/routing.js.map +1 -0
- package/build/module/src/examples/streaming1.d.ts +1 -0
- package/build/module/src/examples/streaming1.js +15 -0
- package/build/module/src/examples/streaming1.js.map +1 -0
- package/build/module/src/examples/streaming2.d.ts +1 -0
- package/build/module/src/examples/streaming2.js +24 -0
- package/build/module/src/examples/streaming2.js.map +1 -0
- package/build/module/src/examples/summarize.d.ts +1 -0
- package/build/module/src/examples/summarize.js +27 -0
- package/build/module/src/examples/summarize.js.map +1 -0
- package/build/module/src/examples/vectordb.d.ts +1 -0
- package/build/module/src/examples/vectordb.js +14 -0
- package/build/module/src/examples/vectordb.js.map +1 -0
- package/build/module/src/funcs/code.d.ts +10 -0
- package/build/module/src/funcs/code.js +63 -0
- package/build/module/src/funcs/code.js.map +1 -0
- package/build/module/src/funcs/embed.d.ts +8 -0
- package/build/module/src/funcs/embed.js +29 -0
- package/build/module/src/funcs/embed.js.map +1 -0
- package/build/module/src/funcs/index.d.ts +2 -0
- package/build/module/src/funcs/index.js +3 -0
- package/build/module/src/funcs/index.js.map +1 -0
- package/build/module/src/index.d.ts +9 -0
- package/build/module/src/index.js +15 -0
- package/build/module/src/index.js.map +1 -0
- package/build/module/src/prompts/agent.d.ts +24 -0
- package/build/module/src/prompts/agent.js +65 -0
- package/build/module/src/prompts/agent.js.map +1 -0
- package/build/module/src/prompts/cot.d.ts +9 -0
- package/build/module/src/prompts/cot.js +17 -0
- package/build/module/src/prompts/cot.js.map +1 -0
- package/build/module/src/prompts/index.d.ts +4 -0
- package/build/module/src/prompts/index.js +5 -0
- package/build/module/src/prompts/index.js.map +1 -0
- package/build/module/src/prompts/prompts.test.d.ts +1 -0
- package/build/module/src/prompts/prompts.test.js +48 -0
- package/build/module/src/prompts/prompts.test.js.map +1 -0
- package/build/module/src/prompts/rag.d.ts +23 -0
- package/build/module/src/prompts/rag.js +30 -0
- package/build/module/src/prompts/rag.js.map +1 -0
- package/build/module/src/prompts/react.d.ts +8 -0
- package/build/module/src/prompts/react.js +22 -0
- package/build/module/src/prompts/react.js.map +1 -0
- package/build/module/src/text/functions.d.ts +25 -0
- package/build/module/src/text/functions.js +55 -0
- package/build/module/src/text/functions.js.map +1 -0
- package/build/module/src/text/index.d.ts +3 -0
- package/build/module/src/text/index.js +4 -0
- package/build/module/src/text/index.js.map +1 -0
- package/build/module/src/text/jsonschema.d.ts +2 -0
- package/build/module/src/text/jsonschema.js +96 -0
- package/build/module/src/text/jsonschema.js.map +1 -0
- package/build/module/src/text/memory.d.ts +21 -0
- package/build/module/src/text/memory.js +73 -0
- package/build/module/src/text/memory.js.map +1 -0
- package/build/module/src/text/types.d.ts +57 -0
- package/build/module/src/text/types.js +2 -0
- package/build/module/src/text/types.js.map +1 -0
- package/build/module/src/trace/index.d.ts +171 -0
- package/build/module/src/trace/index.js +67 -0
- package/build/module/src/trace/index.js.map +1 -0
- package/build/module/src/types/index.d.ts +81 -0
- package/build/module/src/types/index.js +2 -0
- package/build/module/src/types/index.js.map +1 -0
- package/build/module/src/util/apicall.d.ts +21 -0
- package/build/module/src/util/apicall.js +137 -0
- package/build/module/src/util/apicall.js.map +1 -0
- package/build/module/src/util/log.d.ts +14 -0
- package/build/module/src/util/log.js +30 -0
- package/build/module/src/util/log.js.map +1 -0
- package/build/module/src/util/other.d.ts +1 -0
- package/build/module/src/util/other.js +4 -0
- package/build/module/src/util/other.js.map +1 -0
- package/build/module/src/util/stream.d.ts +10 -0
- package/build/module/src/util/stream.js +28 -0
- package/build/module/src/util/stream.js.map +1 -0
- package/build/module/src/util/transform.d.ts +8 -0
- package/build/module/src/util/transform.js +57 -0
- package/build/module/src/util/transform.js.map +1 -0
- package/package.json +111 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
import test from 'ava';
|
|
3
|
+
import { AI } from '../ai/index.js';
|
|
4
|
+
import { ChainOfThought } from './cot.js';
|
|
5
|
+
const text = `The technological singularity—or simply the singularity[1]—is a hypothetical future point in time at which technological growth becomes uncontrollable and irreversible.`;
|
|
6
|
+
const examples = [
|
|
7
|
+
{
|
|
8
|
+
text: 'Mathematical platonism is a philosophical view that posits the existence of abstract mathematical objects that are independent of human thought and language. According to this view, mathematical entities such as numbers, shapes, and functions exist in a non-physical realm and can be discovered but not invented.',
|
|
9
|
+
shortSummary: 'A philosophy that suggests mathematical objects exist independently of human thought in a non-physical realm.'
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
text: 'Quantum entanglement is a physical phenomenon occurring when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the quantum state of each particle cannot be described independently of the state of the others, even when the particles are separated by large distances. This leads to correlations between observable physical properties of the particles.',
|
|
13
|
+
shortSummary: 'A phenomenon where particles remain interconnected and the state of one affects the state of another, regardless of distance.'
|
|
14
|
+
}
|
|
15
|
+
];
|
|
16
|
+
const mockFetch = async () => {
|
|
17
|
+
const mockRes = {
|
|
18
|
+
choices: [
|
|
19
|
+
{
|
|
20
|
+
message: {
|
|
21
|
+
role: 'assistant',
|
|
22
|
+
content: 'Reason: Blah blah blah\nShort Summary: More blah blah blah'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
};
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
resolve({
|
|
29
|
+
ok: true,
|
|
30
|
+
status: 200,
|
|
31
|
+
body: Readable.from(JSON.stringify(mockRes)),
|
|
32
|
+
json: async () => new Promise((resolve) => resolve(mockRes))
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
test('generate prompt', async (t) => {
|
|
37
|
+
const options = { fetch: mockFetch };
|
|
38
|
+
const ai = AI('openai', { apiKey: 'no-key', options });
|
|
39
|
+
// const ai = AI('ollama', { model: 'nous-hermes2' });
|
|
40
|
+
const gen = new ChainOfThought(ai, `text -> shortSummary "summarize in 5 to 10 words"`);
|
|
41
|
+
gen.setExamples(examples);
|
|
42
|
+
const res = await gen.forward({ text });
|
|
43
|
+
t.deepEqual(res, {
|
|
44
|
+
reason: 'Blah blah blah',
|
|
45
|
+
shortSummary: 'More blah blah blah'
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=prompts.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompts.test.js","sourceRoot":"","sources":["../../../../src/pr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;IAEtD,MAAM,GAAG,GAAG,IAAI,cAAc,CAC5B,EAAE,EACF,mDAAmD,CACpD,CAAC;IACF,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAE1B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IAExC,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE;QACf,MAAM,EAAE,gBAAgB;QACxB,YAAY,EAAE,qBAAqB;KACpC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type GenerateOptions } from '../dsp/index.js';
|
|
2
|
+
import { type ProgramForwardOptions } from '../dsp/program.js';
|
|
3
|
+
import type { AIService } from '../text/types.js';
|
|
4
|
+
import { ChainOfThought } from './cot.js';
|
|
5
|
+
export declare class RAG extends ChainOfThought<{
|
|
6
|
+
context: string[];
|
|
7
|
+
question: string;
|
|
8
|
+
}, {
|
|
9
|
+
answer: string;
|
|
10
|
+
}> {
|
|
11
|
+
private genQuery;
|
|
12
|
+
private queryFn;
|
|
13
|
+
private maxHops;
|
|
14
|
+
constructor(ai: AIService, queryFn: (query: string) => Promise<string>, options: Readonly<GenerateOptions & {
|
|
15
|
+
maxHops?: number;
|
|
16
|
+
}>);
|
|
17
|
+
forward({ question }: Readonly<{
|
|
18
|
+
question: string;
|
|
19
|
+
}>, options?: Readonly<ProgramForwardOptions>): Promise<{
|
|
20
|
+
answer: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { dedup, Generate, Signature } from '../dsp/index.js';
|
|
2
|
+
import {} from '../dsp/program.js';
|
|
3
|
+
import { ChainOfThought } from './cot.js';
|
|
4
|
+
export class RAG extends ChainOfThought {
|
|
5
|
+
genQuery;
|
|
6
|
+
queryFn;
|
|
7
|
+
maxHops;
|
|
8
|
+
constructor(ai, queryFn, options) {
|
|
9
|
+
const sig = '"Answer questions with short factoid answers." context:string[] "may contain relevant facts", question -> answer';
|
|
10
|
+
super(ai, sig, options);
|
|
11
|
+
this.maxHops = options?.maxHops ?? 3;
|
|
12
|
+
const qsig = new Signature('"Write a simple search query that will help answer a complex question." context?:string[] "may contain relevant facts", question -> query "question to further our understanding"');
|
|
13
|
+
this.genQuery = new Generate(ai, qsig);
|
|
14
|
+
this.queryFn = queryFn;
|
|
15
|
+
this.register(this.genQuery);
|
|
16
|
+
}
|
|
17
|
+
async forward({ question }, options) {
|
|
18
|
+
let context = [];
|
|
19
|
+
for (let i = 0; i < this.maxHops; i++) {
|
|
20
|
+
const { query } = await this.genQuery.forward({
|
|
21
|
+
context,
|
|
22
|
+
question
|
|
23
|
+
}, options);
|
|
24
|
+
const val = await this.queryFn(query);
|
|
25
|
+
context = dedup([...context, val]);
|
|
26
|
+
}
|
|
27
|
+
return super.forward({ context, question }, options);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=rag.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rag.js","sourceRoot":"","sources":["../../../../src/prompts/rag.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,QAAQ,EAER,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAA8B,MAAM,mBAAmB,CAAC;AAG/D,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,OAAO,GAAI,SAAQ,cAGxB;IACS,QAAQ,CAGd;IACM,OAAO,CAAqC;IAC5C,OAAO,CAAS;IAExB,YACE,EAAa,EACb,OAA2C,EAC3C,OAAyD;QAEzD,MAAM,GAAG,GACP,kHAAkH,CAAC;QACrH,KAAK,CAAC,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;QAExB,IAAI,CAAC,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,CAAC,CAAC;QAErC,MAAM,IAAI,GAAG,IAAI,SAAS,CACxB,mLAAmL,CACpL,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,IAAI,QAAQ,CAG1B,EAAE,EAAE,IAAI,CAAC,CAAC;QACZ,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAEe,KAAK,CAAC,OAAO,CAC3B,EAAE,QAAQ,EAAkC,EAC5C,OAAyC;QAEzC,IAAI,OAAO,GAAa,EAAE,CAAC;QAE3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAC3C;gBACE,OAAO;gBACP,QAAQ;aACT,EACD,OAAO,CACR,CAAC;YACF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACtC,OAAO,GAAG,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GenerateOptions } from '../dsp/generate.js';
|
|
2
|
+
import type { GenIn, GenOut } from '../dsp/program.js';
|
|
3
|
+
import { Signature } from '../dsp/sig.js';
|
|
4
|
+
import type { AIService } from '../text/types.js';
|
|
5
|
+
import { ChainOfThought } from './cot.js';
|
|
6
|
+
export declare class ReAct<IN extends GenIn = GenIn, OUT extends GenOut = GenOut> extends ChainOfThought<IN, OUT> {
|
|
7
|
+
constructor(ai: AIService, signature: Readonly<Signature | string>, options: Readonly<GenerateOptions>);
|
|
8
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Signature } from '../dsp/sig.js';
|
|
2
|
+
import { ChainOfThought } from './cot.js';
|
|
3
|
+
export class ReAct extends ChainOfThought {
|
|
4
|
+
constructor(ai, signature, options) {
|
|
5
|
+
if (!options?.functions || options.functions.length === 0) {
|
|
6
|
+
throw new Error('No functions provided');
|
|
7
|
+
}
|
|
8
|
+
const functions = [...options.functions];
|
|
9
|
+
const funcList = functions.map((f) => `'${f.name}'`).join(', ');
|
|
10
|
+
const sig = new Signature(signature);
|
|
11
|
+
sig.setDescription(`Use the provided functions ${funcList} to complete the task and return the result if any.`);
|
|
12
|
+
// sig.addInputField({
|
|
13
|
+
// name: 'observation',
|
|
14
|
+
// description: 'Result value from executed function',
|
|
15
|
+
// isOptional: true,
|
|
16
|
+
// type: { name: 'string', isArray: true }
|
|
17
|
+
// });
|
|
18
|
+
sig.setOutputFields(sig.getOutputFields().map((v) => ({ ...v, isOptional: true })));
|
|
19
|
+
super(ai, sig, options);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../../../../src/prompts/react.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG1C,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,OAAO,KAGX,SAAQ,cAAuB;IAC/B,YACE,EAAa,EACb,SAAuC,EACvC,OAAkC;QAElC,IAAI,CAAC,OAAO,EAAE,SAAS,IAAI,OAAO,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC3C,CAAC;QAED,MAAM,SAAS,GAAG,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;QACzC,MAAM,QAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEhE,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC,CAAC;QACrC,GAAG,CAAC,cAAc,CAChB,8BAA8B,QAAQ,qDAAqD,CAC5F,CAAC;QAEF,sBAAsB;QACtB,yBAAyB;QACzB,wDAAwD;QACxD,sBAAsB;QACtB,4CAA4C;QAC5C,MAAM;QAEN,GAAG,CAAC,eAAe,CACjB,GAAG,CAAC,eAAe,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAC/D,CAAC;QAEF,KAAK,CAAC,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAC1B,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { TextResponseFunctionCall } from '../ai/types.js';
|
|
2
|
+
import type { AIServiceActionOptions, FunctionExec } from './types.js';
|
|
3
|
+
export type AITextFunctionHandler = (args?: any, extra?: Readonly<AIServiceActionOptions>) => unknown;
|
|
4
|
+
export type FunctionJSONSchema = {
|
|
5
|
+
type: string;
|
|
6
|
+
properties?: Record<string, FunctionJSONSchema & {
|
|
7
|
+
enum?: string[];
|
|
8
|
+
default?: unknown;
|
|
9
|
+
description: string;
|
|
10
|
+
}>;
|
|
11
|
+
required?: string[];
|
|
12
|
+
items?: FunctionJSONSchema;
|
|
13
|
+
};
|
|
14
|
+
export type AITextFunction = {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
parameters?: FunctionJSONSchema;
|
|
18
|
+
func?: AITextFunctionHandler;
|
|
19
|
+
};
|
|
20
|
+
export declare class FunctionProcessor {
|
|
21
|
+
private funcList;
|
|
22
|
+
constructor(funcList: readonly AITextFunction[]);
|
|
23
|
+
private executeFunction;
|
|
24
|
+
execute: (func: Readonly<TextResponseFunctionCall>, options?: Readonly<AIServiceActionOptions>) => Promise<FunctionExec>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import JSON5 from 'json5';
|
|
2
|
+
import { validateJSONSchema } from './jsonschema.js';
|
|
3
|
+
export class FunctionProcessor {
|
|
4
|
+
funcList;
|
|
5
|
+
constructor(funcList) {
|
|
6
|
+
funcList
|
|
7
|
+
.filter((v) => v.parameters)
|
|
8
|
+
.forEach((v) => validateJSONSchema(v.parameters));
|
|
9
|
+
this.funcList = funcList;
|
|
10
|
+
}
|
|
11
|
+
executeFunction = async (fnSpec, func, options) => {
|
|
12
|
+
if (!fnSpec.func) {
|
|
13
|
+
throw new Error(`Function handler for ${fnSpec.name} not implemented`);
|
|
14
|
+
}
|
|
15
|
+
let argOBj;
|
|
16
|
+
if (func.args && func.args.length > 0) {
|
|
17
|
+
argOBj = JSON5.parse(func.args);
|
|
18
|
+
}
|
|
19
|
+
if (!fnSpec.parameters) {
|
|
20
|
+
const res = fnSpec.func.length === 1
|
|
21
|
+
? await fnSpec.func(options)
|
|
22
|
+
: await fnSpec.func();
|
|
23
|
+
return {
|
|
24
|
+
name: fnSpec.name,
|
|
25
|
+
result: JSON.stringify(res, null, 2)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const res = fnSpec.func.length === 2
|
|
29
|
+
? await fnSpec.func(argOBj, options)
|
|
30
|
+
: await fnSpec.func(argOBj);
|
|
31
|
+
return {
|
|
32
|
+
id: func.id,
|
|
33
|
+
name: func.name,
|
|
34
|
+
args: func.args,
|
|
35
|
+
result: JSON.stringify(res, null, 2)
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
execute = async (func, options) => {
|
|
39
|
+
const fnSpec = this.funcList.find((v) => v.name.localeCompare(func.name) === 0);
|
|
40
|
+
if (!fnSpec) {
|
|
41
|
+
throw new Error(`Function not found: ` + func.name);
|
|
42
|
+
}
|
|
43
|
+
if (!fnSpec.func) {
|
|
44
|
+
throw new Error('No handler for function: ' + func.name);
|
|
45
|
+
}
|
|
46
|
+
// execute value function calls
|
|
47
|
+
const funcExec = await this.executeFunction(fnSpec, func, options);
|
|
48
|
+
// // signal error if no data returned
|
|
49
|
+
// if (!funcExec.result || funcExec.result.length === 0) {
|
|
50
|
+
// funcExec.result = `No data returned by function`;
|
|
51
|
+
// }
|
|
52
|
+
return funcExec;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=functions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../../../src/text/functions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AA8BrD,MAAM,OAAO,iBAAiB;IACpB,QAAQ,CAA4B;IAE5C,YAAY,QAAmC;QAC7C,QAAQ;aACL,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;aAC3B,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,kBAAkB,CAAC,CAAC,CAAC,UAAW,CAAC,CAAC,CAAC;QACrD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEO,eAAe,GAAG,KAAK,EAC7B,MAAgC,EAChC,IAAwC,EACxC,OAA0C,EACnB,EAAE;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,wBAAwB,MAAM,CAAC,IAAI,kBAAkB,CAAC,CAAC;QACzE,CAAC;QAED,IAAI,MAAM,CAAC;QACX,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACtC,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAA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,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3D,CAAC;QAED,+BAA+B;QAC/B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAEnE,sCAAsC;QACtC,0DAA0D;QAC1D,sDAAsD;QACtD,IAAI;QACJ,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/text/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export const validateJSONSchema = (schema) => {
|
|
2
|
+
const errors = [];
|
|
3
|
+
const validateSchemaObject = (schema, path = '') => {
|
|
4
|
+
const validTypes = [
|
|
5
|
+
'array',
|
|
6
|
+
'integer',
|
|
7
|
+
'number',
|
|
8
|
+
'string',
|
|
9
|
+
'boolean',
|
|
10
|
+
'null',
|
|
11
|
+
'object'
|
|
12
|
+
];
|
|
13
|
+
if (!validTypes.includes(schema.type)) {
|
|
14
|
+
errors.push(`Invalid type '${schema.type}' at ${path || 'root'}`);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (schema.type === 'object' && schema.properties) {
|
|
18
|
+
if (typeof schema.properties !== 'object' ||
|
|
19
|
+
Array.isArray(schema.properties)) {
|
|
20
|
+
errors.push(`Invalid properties object at ${path || 'root'}`);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
for (const key in schema.properties) {
|
|
24
|
+
const value = schema.properties[key];
|
|
25
|
+
if (typeof value !== 'object') {
|
|
26
|
+
errors.push(`Invalid schema object at ${path}${key}`);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
validateSchemaObject(value, `${path}${key}.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (schema.required && !Array.isArray(schema.required)) {
|
|
33
|
+
errors.push(`'required' should be an array at ${path || 'root'}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (schema.type === 'array' && schema.items) {
|
|
37
|
+
if (typeof schema.items !== 'object') {
|
|
38
|
+
errors.push(`Invalid items schema at ${path || 'root'}`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
validateSchemaObject(schema.items, `${path}items.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
validateSchemaObject(schema);
|
|
46
|
+
if (errors.length > 0) {
|
|
47
|
+
throw new Error(errors.join('; '));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Example Usage:
|
|
51
|
+
/*
|
|
52
|
+
const validSchema: FunctionJSONSchema = {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
id: { type: 'integer' },
|
|
56
|
+
name: { type: 'string' },
|
|
57
|
+
email: { type: 'string' },
|
|
58
|
+
isActive: { type: 'boolean' },
|
|
59
|
+
tags: {
|
|
60
|
+
type: 'array',
|
|
61
|
+
items: { type: 'string' }
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
required: ['id', 'name', 'email']
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const invalidSchema: any = {
|
|
68
|
+
type: 'object',
|
|
69
|
+
properties: {
|
|
70
|
+
id: { type: 'integer' },
|
|
71
|
+
name: { type: 'string' },
|
|
72
|
+
email: { type: 'unknownType' }, // Invalid type
|
|
73
|
+
isActive: { type: 'boolean' },
|
|
74
|
+
tags: {
|
|
75
|
+
type: 'array',
|
|
76
|
+
items: { type: 'string' }
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
required: 'id,name,email' // Invalid 'required' field
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
validateSchemaStructure(validSchema);
|
|
84
|
+
console.log('Schema is valid!');
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('Schema validation failed:', error.message);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
validateSchemaStructure(invalidSchema);
|
|
91
|
+
console.log('Schema is valid!');
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error('Schema validation failed:', error.message);
|
|
94
|
+
}
|
|
95
|
+
*/
|
|
96
|
+
//# sourceMappingURL=jsonschema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonschema.js","sourceRoot":"","sources":["../../../../src/text/jsonschema.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,MAAoC,EAC9B,EAAE;IACR,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,MAAM,oBAAoB,GAAG,CAC3B,MAAoC,EACpC,OAAe,EAAE,EACX,EAAE;QACR,MAAM,UAAU,GAAG;YACjB,OAAO;YACP,SAAS;YACT,QAAQ;YACR,QAAQ;YACR,SAAS;YACT,MAAM;YACN,QAAQ;SACT,CAAC;QAEF,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YACtC,MAAM,CAAC,IAAI,CAAC,iBAAiB,MAAM,CAAC,IAAI,QAAQ,IAAI,IAAI,MAAM,EAAE,CAAC,CAAC;YAClE,OAAO;QACT,CAAC;QAED,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YAClD,IACE,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ;gBACrC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,EAChC,CAAC;gBACD,MAAM,CAAC,IAAI,CAAC,gCAAgC,IAAI,IAAI,MAAM,EAAE,CAAC,CAAC;YAChE,CAAC;iBAAM,CAAC;gBACN,KAAK,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@
|
|
|
1
|
+
import type { TextResponseResult } from '../ai/types.js';
|
|
2
|
+
import type { AITextChatRequest } from '../types/index.js';
|
|
3
|
+
import type { AIMemory } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A memory class to store ai interactions
|
|
6
|
+
* @export
|
|
7
|
+
*/
|
|
8
|
+
export declare class Memory implements AIMemory {
|
|
9
|
+
private data;
|
|
10
|
+
private sdata;
|
|
11
|
+
private limit;
|
|
12
|
+
constructor(limit?: number);
|
|
13
|
+
add(value: Readonly<AITextChatRequest['chatPrompt'][0] | AITextChatRequest['chatPrompt']>, sessionId?: string): void;
|
|
14
|
+
addResult({ content, name, functionCalls }: Readonly<TextResponseResult>, sessionId?: string): void;
|
|
15
|
+
updateResult({ content, name, functionCalls }: Readonly<TextResponseResult>, sessionId?: string): void;
|
|
16
|
+
history(sessionId?: string): AITextChatRequest['chatPrompt'];
|
|
17
|
+
peek(sessionId?: string): AITextChatRequest['chatPrompt'];
|
|
18
|
+
getLast(sessionId?: string): AITextChatRequest['chatPrompt'][0] | undefined;
|
|
19
|
+
reset(sessionId?: string): void;
|
|
20
|
+
private get;
|
|
21
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A memory class to store ai interactions
|
|
3
|
+
* @export
|
|
4
|
+
*/
|
|
5
|
+
export class Memory {
|
|
6
|
+
data = [];
|
|
7
|
+
sdata = new Map();
|
|
8
|
+
limit;
|
|
9
|
+
constructor(limit = 50) {
|
|
10
|
+
if (limit <= 0) {
|
|
11
|
+
throw Error("argument 'last' must be greater than 0");
|
|
12
|
+
}
|
|
13
|
+
this.limit = limit;
|
|
14
|
+
}
|
|
15
|
+
add(value, sessionId) {
|
|
16
|
+
const d = this.get(sessionId);
|
|
17
|
+
let n = 0;
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
n = d.push(...structuredClone(value));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
n = d.push({
|
|
23
|
+
...structuredClone(value)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (d.length > this.limit) {
|
|
27
|
+
d.splice(0, this.limit + n - this.limit);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
addResult({ content, name, functionCalls }, sessionId) {
|
|
31
|
+
this.add({ content, name, role: 'assistant', functionCalls }, sessionId);
|
|
32
|
+
}
|
|
33
|
+
updateResult({ content, name, functionCalls }, sessionId) {
|
|
34
|
+
const item = this.get(sessionId);
|
|
35
|
+
if ('content' in item && content) {
|
|
36
|
+
item.content = content;
|
|
37
|
+
}
|
|
38
|
+
if ('name' in item && name) {
|
|
39
|
+
item.name = name;
|
|
40
|
+
}
|
|
41
|
+
if ('functionCalls' in item && functionCalls) {
|
|
42
|
+
item.functionCalls = functionCalls;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
history(sessionId) {
|
|
46
|
+
return this.get(sessionId);
|
|
47
|
+
}
|
|
48
|
+
peek(sessionId) {
|
|
49
|
+
return this.get(sessionId);
|
|
50
|
+
}
|
|
51
|
+
getLast(sessionId) {
|
|
52
|
+
const d = this.get(sessionId);
|
|
53
|
+
return d.at(-1);
|
|
54
|
+
}
|
|
55
|
+
reset(sessionId) {
|
|
56
|
+
if (!sessionId) {
|
|
57
|
+
this.data = [];
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.sdata.set(sessionId, []);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
get(sessionId) {
|
|
64
|
+
if (!sessionId) {
|
|
65
|
+
return this.data;
|
|
66
|
+
}
|
|
67
|
+
if (!this.sdata.has(sessionId)) {
|
|
68
|
+
this.sdata.set(sessionId, []);
|
|
69
|
+
}
|
|
70
|
+
return this.sdata.get(sessionId) || [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../../src/text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;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IACzC,CAAC;CACF"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import type { ReadableStream } from 'stream/web';
|
|
4
|
+
import type { EmbedResponse, RateLimiterFunction, TextModelConfig, TextModelInfo, TextResponse, TextResponseResult } from '../ai/types.js';
|
|
5
|
+
import type { Tracer } from '../trace/index.js';
|
|
6
|
+
import type { AITextChatRequest, AITextEmbedRequest } from '../types/index.js';
|
|
7
|
+
export type FunctionExec = {
|
|
8
|
+
id?: string;
|
|
9
|
+
name: string;
|
|
10
|
+
args?: any;
|
|
11
|
+
result?: string;
|
|
12
|
+
};
|
|
13
|
+
export type AITextResponse<T> = {
|
|
14
|
+
prompt: string;
|
|
15
|
+
sessionId?: string;
|
|
16
|
+
value(): T;
|
|
17
|
+
};
|
|
18
|
+
export interface AIMemory {
|
|
19
|
+
add(result: Readonly<AITextChatRequest['chatPrompt'] | AITextChatRequest['chatPrompt'][0]>, sessionId?: string): void;
|
|
20
|
+
addResult(result: Readonly<TextResponseResult>, sessionId?: string): void;
|
|
21
|
+
updateResult(result: Readonly<TextResponseResult>, sessionId?: string): void;
|
|
22
|
+
history(sessionId?: string): AITextChatRequest['chatPrompt'];
|
|
23
|
+
peek(sessionId?: string): AITextChatRequest['chatPrompt'];
|
|
24
|
+
reset(sessionId?: string): void;
|
|
25
|
+
getLast(sessionId?: string): AITextChatRequest['chatPrompt'][0] | undefined;
|
|
26
|
+
}
|
|
27
|
+
export type AIPromptConfig = {
|
|
28
|
+
stream?: boolean;
|
|
29
|
+
};
|
|
30
|
+
export type AITranscribeConfig = {
|
|
31
|
+
language?: string;
|
|
32
|
+
};
|
|
33
|
+
export type AIServiceOptions = {
|
|
34
|
+
debug?: boolean;
|
|
35
|
+
rateLimiter?: RateLimiterFunction;
|
|
36
|
+
fetch?: typeof fetch;
|
|
37
|
+
tracer?: Tracer;
|
|
38
|
+
};
|
|
39
|
+
export type AIServiceActionOptions = {
|
|
40
|
+
sessionId?: string;
|
|
41
|
+
traceId?: string;
|
|
42
|
+
};
|
|
43
|
+
export interface AIService {
|
|
44
|
+
getName(): string;
|
|
45
|
+
getModelInfo(): Readonly<TextModelInfo & {
|
|
46
|
+
provider: string;
|
|
47
|
+
}>;
|
|
48
|
+
getEmbedModelInfo(): Readonly<TextModelInfo> | undefined;
|
|
49
|
+
getModelConfig(): Readonly<TextModelConfig>;
|
|
50
|
+
getFeatures(): {
|
|
51
|
+
functions: boolean;
|
|
52
|
+
streaming: boolean;
|
|
53
|
+
};
|
|
54
|
+
chat(req: Readonly<AITextChatRequest>, options?: Readonly<AIPromptConfig & AIServiceActionOptions>): Promise<TextResponse | ReadableStream<TextResponse>>;
|
|
55
|
+
embed(req: Readonly<AITextEmbedRequest>, options?: Readonly<AIServiceActionOptions & AIServiceActionOptions>): Promise<EmbedResponse>;
|
|
56
|
+
setOptions(options: Readonly<AIServiceOptions>): void;
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/text/types.ts"],"names":[],"mappings":""}
|