kensho-kfinance 2.2.5__tar.gz → 2.4.0__tar.gz
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.
Potentially problematic release.
This version of kensho-kfinance might be problematic. Click here for more details.
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.gitignore +2 -1
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/PKG-INFO +40 -7
- kensho_kfinance-2.4.0/README.md +62 -0
- kensho_kfinance-2.2.5/usage_examples.ipynb → kensho_kfinance-2.4.0/example_notebooks/basic_usage.ipynb +1 -1
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/langchain/anthropic_langchain_tool_calling.ipynb +200 -0
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/langchain/google_gemini_langchain_tool_calling.ipynb +193 -0
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/langchain/openai_langchain_tool_calling.ipynb +193 -0
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/non-langchain/anthropic_tool_calling.ipynb +286 -0
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/non-langchain/google_gemini_tool_calling.ipynb +228 -0
- kensho_kfinance-2.4.0/example_notebooks/tool_calling/non-langchain/openai_langchain_tool_calling.ipynb +278 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/justfile +5 -1
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kensho_kfinance.egg-info/PKG-INFO +40 -7
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kensho_kfinance.egg-info/SOURCES.txt +14 -1
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kensho_kfinance.egg-info/requires.txt +5 -2
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/CHANGELOG.md +9 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/fetch.py +10 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/kfinance.py +227 -11
- kensho_kfinance-2.4.0/kfinance/mcp.py +101 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/pydantic_models.py +8 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_example_notebook.py +4 -2
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_fetch.py +12 -0
- kensho_kfinance-2.4.0/kfinance/tests/test_mcp.py +16 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_objects.py +165 -2
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_tools.py +217 -1
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/__init__.py +8 -0
- kensho_kfinance-2.4.0/kfinance/tool_calling/get_earnings.py +30 -0
- kensho_kfinance-2.4.0/kfinance/tool_calling/get_latest_earnings.py +27 -0
- kensho_kfinance-2.4.0/kfinance/tool_calling/get_next_earnings.py +27 -0
- kensho_kfinance-2.4.0/kfinance/tool_calling/get_transcript.py +23 -0
- kensho_kfinance-2.4.0/kfinance/tool_calling/prompts.py +16 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/version.py +2 -2
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/pyproject.toml +5 -2
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/scripts/lint.sh +1 -1
- kensho_kfinance-2.2.5/README.md +0 -32
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.coveragerc +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.github/workflows/ci-lint.yml +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.github/workflows/ci-test.yml +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.github/workflows/python-publish.yml +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/.readthedocs.yaml +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/AUTHORS.md +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/CODE_OF_CONDUCT.md +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/CONTRIBUTING.md +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/LICENSE +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/build_tool_calling_documentation.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/conf.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/index.rst +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/kfinance.rst +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/requirements.txt +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/templates/apidoc/package.rst_t +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/templates/apidoc/toc.rst_t +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/docs/tool_calling.rst +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/images/colab_logo_32px.png +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kensho_kfinance.egg-info/dependency_links.txt +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kensho_kfinance.egg-info/top_level.txt +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/__init__.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/batch_request_handling.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/constants.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/meta_classes.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/prompt.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/py.typed +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/server_thread.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/__init__.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/conftest.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_batch_requests.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_client.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tests/test_group_objects.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/README.md +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_business_relationship_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_capitalization_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_cusip_from_ticker.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_earnings_call_datetimes_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_financial_line_item_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_financial_statement_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_history_metadata_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_info_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_isin_from_ticker.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_latest.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_n_quarters_ago.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_prices_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/get_segments_from_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/resolve_identifier.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/kfinance/tool_calling/shared_models.py +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/scripts/copyright_line_check.sh +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/scripts/test.sh +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/setup.cfg +0 -0
- {kensho_kfinance-2.2.5 → kensho_kfinance-2.4.0}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kensho-kfinance
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.4.0
|
|
4
4
|
Summary: Python CLI for kFinance
|
|
5
5
|
Author-email: Luke Brown <luke.brown@kensho.com>, Michelle Keoy <michelle.keoy@kensho.com>, Keith Page <keith.page@kensho.com>, Matthew Rosen <matthew.rosen@kensho.com>, Nick Roshdieh <nick.roshdieh@kensho.com>
|
|
6
6
|
Project-URL: source, https://github.com/kensho-technologies/kfinance
|
|
@@ -13,8 +13,10 @@ Description-Content-Type: text/markdown
|
|
|
13
13
|
License-File: LICENSE
|
|
14
14
|
License-File: AUTHORS.md
|
|
15
15
|
Requires-Dist: cachetools<6,>=5.5
|
|
16
|
+
Requires-Dist: click<=9,>=8.2.1
|
|
17
|
+
Requires-Dist: fastmcp<3,>=2
|
|
16
18
|
Requires-Dist: langchain-core>=0.3.15
|
|
17
|
-
Requires-Dist: langchain-google-genai<3,>=2.1.
|
|
19
|
+
Requires-Dist: langchain-google-genai<3,>=2.1.5
|
|
18
20
|
Requires-Dist: numpy>=1.22.4
|
|
19
21
|
Requires-Dist: pandas>=2.0.0
|
|
20
22
|
Requires-Dist: pillow>=10
|
|
@@ -29,7 +31,8 @@ Requires-Dist: urllib3>=1.21.1
|
|
|
29
31
|
Provides-Extra: dev
|
|
30
32
|
Requires-Dist: coverage<8,>=7.6.10; extra == "dev"
|
|
31
33
|
Requires-Dist: ipykernel<7,>=6.29; extra == "dev"
|
|
32
|
-
Requires-Dist:
|
|
34
|
+
Requires-Dist: langchain-anthropic<1,>=0.3.10; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy<2,>=1.16.0; extra == "dev"
|
|
33
36
|
Requires-Dist: nbconvert<8,>=7.16; extra == "dev"
|
|
34
37
|
Requires-Dist: nbformat<6,>5.10; extra == "dev"
|
|
35
38
|
Requires-Dist: nbqa<2,>1.9; extra == "dev"
|
|
@@ -51,7 +54,7 @@ Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-
|
|
|
51
54
|
|
|
52
55
|
# Setup
|
|
53
56
|
|
|
54
|
-
You can install kFinance on [PyPI](https://pypi.org/project/kensho-kfinance/) via
|
|
57
|
+
You can install kFinance on [PyPI](https://pypi.org/project/kensho-kfinance/) via
|
|
55
58
|
|
|
56
59
|
`pip install kensho-kfinance`
|
|
57
60
|
|
|
@@ -61,11 +64,41 @@ To receive access, please email [S&P Global Market Intelligence](market.intellig
|
|
|
61
64
|
|
|
62
65
|
Once access is obtained, get started using the [Authentication Guide](https://docs.kensho.com/llmreadyapi/kf-authentication) and [Usage Guide](https://docs.kensho.com/llmreadyapi/usage).
|
|
63
66
|
|
|
64
|
-
|
|
67
|
+
To get started, we provide some notebooks:
|
|
68
|
+
|
|
69
|
+
- The [LLM-ready API Basic Usage](example_notebooks%2Fbasic_usage.ipynb) notebook demonstrates how
|
|
70
|
+
fetch data with the kFinance client.
|
|
71
|
+
- The [tool_calling notebooks](example_notebooks%2Ftool_calling) show how the kFinance library can
|
|
72
|
+
be used for tool calling. We provide notebooks for OpenAI (GPT), Anthropic (Claude), and Google
|
|
73
|
+
(Gemini). Each of these integrations comes in a langchain version, which uses langchain as a
|
|
74
|
+
wrapper to simplify the integration, and as a lower level non-langchain version.
|
|
75
|
+
|
|
76
|
+
We also provide an [interactive notebook](example_notebooks/basic_usage.ipynb) that demonstrates some usage examples.
|
|
77
|
+
|
|
78
|
+
# MCP (Model Context Protocol)
|
|
79
|
+
|
|
80
|
+
To run the kFinance MCP server use:
|
|
81
|
+
|
|
82
|
+
`python -m kfinance.mcp`
|
|
83
|
+
|
|
84
|
+
This function initializes and starts an MCP server that exposes the kFinance tools. The server supports multiple authentication methods and transport protocols to accommodate different deployment scenarios.
|
|
85
|
+
|
|
86
|
+
The server's full signature is as follows:
|
|
87
|
+
|
|
88
|
+
`kfinance.mcp [--stdio,-s]/[--sse, ] --refresh-token <refresh-token> --client-id <client-id> --private-key <private-key>`
|
|
89
|
+
|
|
90
|
+
Authentication Methods (in order of precedence):
|
|
91
|
+
1. Refresh Token: Uses an existing refresh token for authentication. The `--refresh-token <refresh-token>` argument must be provided.
|
|
92
|
+
2. Key Pair: Uses client ID and private key for authentication. Both the `--client-id <client-id>` and `--private-key <private-key>` arguments must be provided.
|
|
93
|
+
3. Browser: Falls back to browser-based authentication flow. This occurs if no auth arguments are provided.
|
|
94
|
+
|
|
95
|
+
Transport Layers:
|
|
96
|
+
- stdio can be set by passing either `--stdio` or `-s`
|
|
97
|
+
- sse can be set by passing `--sse` or no other transport related flag
|
|
65
98
|
|
|
66
99
|
# Versioning
|
|
67
|
-
The kFinance uses semantic versioning (major, minor, patch).
|
|
68
|
-
To bump the version, add a new entry in [CHANGELOG.md](kfinance%2FCHANGELOG.md).
|
|
100
|
+
The kFinance uses semantic versioning (major, minor, patch).
|
|
101
|
+
To bump the version, add a new entry in [CHANGELOG.md](kfinance%2FCHANGELOG.md).
|
|
69
102
|
This will generate a new version of the library as part of the release process.
|
|
70
103
|
|
|
71
104
|
# License
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# kFinance
|
|
2
|
+
|
|
3
|
+
The kFinance Python library provides a simple interface for the LLM-ready API, streamlining API requests and response handling. It can be used on its own, with LLMs, or integrated into applications.
|
|
4
|
+
|
|
5
|
+
For a complete overview of the functions, usage, and features of the kFinance Python library, please refer to documentation [here](https://kensho-kfinance.readthedocs.io/en/stable/).
|
|
6
|
+
|
|
7
|
+
Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-maintainers@kensho.com).
|
|
8
|
+
|
|
9
|
+
# Setup
|
|
10
|
+
|
|
11
|
+
You can install kFinance on [PyPI](https://pypi.org/project/kensho-kfinance/) via
|
|
12
|
+
|
|
13
|
+
`pip install kensho-kfinance`
|
|
14
|
+
|
|
15
|
+
# Getting started
|
|
16
|
+
|
|
17
|
+
To receive access, please email [S&P Global Market Intelligence](market.intelligence@spglobal.com) for information on free trials and pricing.
|
|
18
|
+
|
|
19
|
+
Once access is obtained, get started using the [Authentication Guide](https://docs.kensho.com/llmreadyapi/kf-authentication) and [Usage Guide](https://docs.kensho.com/llmreadyapi/usage).
|
|
20
|
+
|
|
21
|
+
To get started, we provide some notebooks:
|
|
22
|
+
|
|
23
|
+
- The [LLM-ready API Basic Usage](example_notebooks%2Fbasic_usage.ipynb) notebook demonstrates how
|
|
24
|
+
fetch data with the kFinance client.
|
|
25
|
+
- The [tool_calling notebooks](example_notebooks%2Ftool_calling) show how the kFinance library can
|
|
26
|
+
be used for tool calling. We provide notebooks for OpenAI (GPT), Anthropic (Claude), and Google
|
|
27
|
+
(Gemini). Each of these integrations comes in a langchain version, which uses langchain as a
|
|
28
|
+
wrapper to simplify the integration, and as a lower level non-langchain version.
|
|
29
|
+
|
|
30
|
+
We also provide an [interactive notebook](example_notebooks/basic_usage.ipynb) that demonstrates some usage examples.
|
|
31
|
+
|
|
32
|
+
# MCP (Model Context Protocol)
|
|
33
|
+
|
|
34
|
+
To run the kFinance MCP server use:
|
|
35
|
+
|
|
36
|
+
`python -m kfinance.mcp`
|
|
37
|
+
|
|
38
|
+
This function initializes and starts an MCP server that exposes the kFinance tools. The server supports multiple authentication methods and transport protocols to accommodate different deployment scenarios.
|
|
39
|
+
|
|
40
|
+
The server's full signature is as follows:
|
|
41
|
+
|
|
42
|
+
`kfinance.mcp [--stdio,-s]/[--sse, ] --refresh-token <refresh-token> --client-id <client-id> --private-key <private-key>`
|
|
43
|
+
|
|
44
|
+
Authentication Methods (in order of precedence):
|
|
45
|
+
1. Refresh Token: Uses an existing refresh token for authentication. The `--refresh-token <refresh-token>` argument must be provided.
|
|
46
|
+
2. Key Pair: Uses client ID and private key for authentication. Both the `--client-id <client-id>` and `--private-key <private-key>` arguments must be provided.
|
|
47
|
+
3. Browser: Falls back to browser-based authentication flow. This occurs if no auth arguments are provided.
|
|
48
|
+
|
|
49
|
+
Transport Layers:
|
|
50
|
+
- stdio can be set by passing either `--stdio` or `-s`
|
|
51
|
+
- sse can be set by passing `--sse` or no other transport related flag
|
|
52
|
+
|
|
53
|
+
# Versioning
|
|
54
|
+
The kFinance uses semantic versioning (major, minor, patch).
|
|
55
|
+
To bump the version, add a new entry in [CHANGELOG.md](kfinance%2FCHANGELOG.md).
|
|
56
|
+
This will generate a new version of the library as part of the release process.
|
|
57
|
+
|
|
58
|
+
# License
|
|
59
|
+
|
|
60
|
+
Use is solely in accordance with the signed agreement between your entity and S&P.
|
|
61
|
+
|
|
62
|
+
Copyright 2025-present Kensho Technologies, LLC. The present date is determined by the timestamp of the most recent commit in the repository.
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"\n",
|
|
21
21
|
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
|
|
22
22
|
" <td>\n",
|
|
23
|
-
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/kensho-technologies/
|
|
23
|
+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/kensho-technologies/kfinance/blob/main/example_notebooks/basic_usage.ipynb\"><img src=\"../images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
|
|
24
24
|
" </td>\n",
|
|
25
25
|
"</table>"
|
|
26
26
|
]
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"attachments": {},
|
|
5
|
+
"cell_type": "markdown",
|
|
6
|
+
"metadata": {},
|
|
7
|
+
"source": [
|
|
8
|
+
"##### Copyright 2025 Kensho Technologies, LLC"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"cell_type": "markdown",
|
|
13
|
+
"source": [
|
|
14
|
+
"# LangChain Tool Calling with Anthropic Claude\n",
|
|
15
|
+
"**_Claude to retrieve data from the LLM-ready API using the kFinance python library!_**\n",
|
|
16
|
+
"\n",
|
|
17
|
+
"What you'll need to run this notebook:\n",
|
|
18
|
+
"\n",
|
|
19
|
+
"1. kFinance credentials\n",
|
|
20
|
+
"2. An Anthropic API key\n",
|
|
21
|
+
"\n",
|
|
22
|
+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
|
|
23
|
+
" <td>\n",
|
|
24
|
+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/kensho-technologies/kfinance/blob/main/example_notebooks/tool_calling/langchain/anthropic_langchain_tool_calling.ipynb\"><img src=\"../../../images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
|
|
25
|
+
" </td>\n",
|
|
26
|
+
"</table>"
|
|
27
|
+
],
|
|
28
|
+
"metadata": {
|
|
29
|
+
"collapsed": false
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"attachments": {},
|
|
34
|
+
"cell_type": "markdown",
|
|
35
|
+
"metadata": {
|
|
36
|
+
"id": "ruP6MHfzR2Y4"
|
|
37
|
+
},
|
|
38
|
+
"source": [
|
|
39
|
+
"## Install dependencies"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"cell_type": "code",
|
|
44
|
+
"execution_count": null,
|
|
45
|
+
"metadata": {
|
|
46
|
+
"colab": {
|
|
47
|
+
"base_uri": "https://localhost:8080/"
|
|
48
|
+
},
|
|
49
|
+
"collapsed": true,
|
|
50
|
+
"id": "YAwU0_7TRhV7",
|
|
51
|
+
"jupyter": {
|
|
52
|
+
"outputs_hidden": true
|
|
53
|
+
},
|
|
54
|
+
"outputId": "e6ebf89c-7680-4852-e52b-c99610107cdf"
|
|
55
|
+
},
|
|
56
|
+
"outputs": [],
|
|
57
|
+
"source": [
|
|
58
|
+
"# install the latest version of kFinance package\n",
|
|
59
|
+
"%pip install kensho-kfinance\n",
|
|
60
|
+
"# install langchain and anthropic\n",
|
|
61
|
+
"%pip install anthropic\n",
|
|
62
|
+
"%pip install langchain\n",
|
|
63
|
+
"%pip install langchain-anthropic"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"attachments": {},
|
|
68
|
+
"cell_type": "markdown",
|
|
69
|
+
"metadata": {
|
|
70
|
+
"id": "e_G2p3tTSHeo"
|
|
71
|
+
},
|
|
72
|
+
"source": [
|
|
73
|
+
"# Instantiate kFinance Client"
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"cell_type": "code",
|
|
78
|
+
"execution_count": null,
|
|
79
|
+
"metadata": {
|
|
80
|
+
"id": "7QPnfPuX87rg"
|
|
81
|
+
},
|
|
82
|
+
"outputs": [],
|
|
83
|
+
"source": [
|
|
84
|
+
"# import the kfinance client\n",
|
|
85
|
+
"import sys\n",
|
|
86
|
+
"from kfinance.kfinance import Client\n",
|
|
87
|
+
"\n",
|
|
88
|
+
"# check if the current environment is a Google Colab\n",
|
|
89
|
+
"try:\n",
|
|
90
|
+
" import google.colab\n",
|
|
91
|
+
"\n",
|
|
92
|
+
" IN_GOOGLE_COLAB = True\n",
|
|
93
|
+
"except:\n",
|
|
94
|
+
" IN_GOOGLE_COLAB = False\n",
|
|
95
|
+
"\n",
|
|
96
|
+
"# initialize the kfinance client with one of the following:\n",
|
|
97
|
+
"# 1. your kensho refresh token\n",
|
|
98
|
+
"# 2. your kensho client id and kensho private key\n",
|
|
99
|
+
"# 3. automated login (not accessible on Google Collab)\n",
|
|
100
|
+
"if IN_GOOGLE_COLAB:\n",
|
|
101
|
+
" kensho_refresh_token = \"\"\n",
|
|
102
|
+
" assert kensho_refresh_token != \"\", \"kensho refresh token is empty! Make sure to enter your kensho refresh token above\"\n",
|
|
103
|
+
" kfinance_client = Client(refresh_token=kensho_refresh_token)\n",
|
|
104
|
+
"\n",
|
|
105
|
+
" # kensho_client_id = \"\"\n",
|
|
106
|
+
" # kensho_private_key = \"\"\n",
|
|
107
|
+
" # assert kensho_client_id != \"\", \"kensho client id is empty! Make sure to enter your kensho client id above\"\n",
|
|
108
|
+
" # assert kensho_private_key != \"\", \"kensho private key is empty! Make sure to enter your kensho private key above\"\n",
|
|
109
|
+
" # kfinance_client = Client(client_id=kensho_client_id, private_key=kensho_private_key)\n",
|
|
110
|
+
"else:\n",
|
|
111
|
+
" kfinance_client = Client()"
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"attachments": {},
|
|
116
|
+
"cell_type": "markdown",
|
|
117
|
+
"metadata": {
|
|
118
|
+
"id": "LvQy_66Jioys"
|
|
119
|
+
},
|
|
120
|
+
"source": [
|
|
121
|
+
"# LangChain Tool Calling with Anthropic"
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"cell_type": "code",
|
|
126
|
+
"execution_count": null,
|
|
127
|
+
"metadata": {
|
|
128
|
+
"id": "WkoRuipOEQz6"
|
|
129
|
+
},
|
|
130
|
+
"outputs": [],
|
|
131
|
+
"source": [
|
|
132
|
+
"from pydantic import SecretStr\n",
|
|
133
|
+
"from kfinance.tool_calling.prompts import BASE_PROMPT\n",
|
|
134
|
+
"from langchain.agents import AgentExecutor, create_tool_calling_agent\n",
|
|
135
|
+
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
|
136
|
+
"from langchain_anthropic import ChatAnthropic\n",
|
|
137
|
+
"\n",
|
|
138
|
+
"\n",
|
|
139
|
+
"class LangChainAnthropicChat:\n",
|
|
140
|
+
" def __init__(self, kfinance_client: Client) -> None:\n",
|
|
141
|
+
" \"\"\"Initialize a LangChainAnthropicChat\"\"\"\n",
|
|
142
|
+
"\n",
|
|
143
|
+
" # Initialize Anthropic with your Anthropic key\n",
|
|
144
|
+
" anthropic_api_key = SecretStr(\"\") # replace with your own key\n",
|
|
145
|
+
" assert anthropic_api_key, \"Anthropic API key is empty! Make sure to enter your Anthropic API key above\"\n",
|
|
146
|
+
" llm = ChatAnthropic(model=\"claude-3-7-sonnet-20250219\",\n",
|
|
147
|
+
" api_key=anthropic_api_key) # type: ignore[call-arg]\n",
|
|
148
|
+
"\n",
|
|
149
|
+
" # Get the prompt to use - can be replaced with any prompt that includes variables \"agent_scratchpad\" and \"input\"!\n",
|
|
150
|
+
" # Prompt\n",
|
|
151
|
+
" prompt = ChatPromptTemplate.from_messages(\n",
|
|
152
|
+
" [\n",
|
|
153
|
+
" (\"system\", BASE_PROMPT),\n",
|
|
154
|
+
" MessagesPlaceholder(\"chat_history\", optional=True),\n",
|
|
155
|
+
" (\"human\", \"{input}\"),\n",
|
|
156
|
+
" MessagesPlaceholder(\"agent_scratchpad\"),\n",
|
|
157
|
+
" ]\n",
|
|
158
|
+
" )\n",
|
|
159
|
+
" # Construct the tool calling agent\n",
|
|
160
|
+
" agent = create_tool_calling_agent(llm=llm, tools=kfinance_client.langchain_tools,\n",
|
|
161
|
+
" prompt=prompt)\n",
|
|
162
|
+
" # Create an agent executor by passing in the agent and tools\n",
|
|
163
|
+
" self.agent_executor = AgentExecutor(agent=agent, tools=kfinance_client.langchain_tools,\n",
|
|
164
|
+
" verbose=True)\n",
|
|
165
|
+
"\n",
|
|
166
|
+
" def start_chatting(self) -> None:\n",
|
|
167
|
+
" \"\"\"Open chat shell\"\"\"\n",
|
|
168
|
+
" while True:\n",
|
|
169
|
+
" user_input = input(\"Enter your message and press the [return] key\\n\")\n",
|
|
170
|
+
" self.agent_executor.invoke({\"input\": user_input})\n",
|
|
171
|
+
" sys.stdout.write(\"\\n\")"
|
|
172
|
+
]
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"cell_type": "code",
|
|
176
|
+
"execution_count": null,
|
|
177
|
+
"metadata": {},
|
|
178
|
+
"outputs": [],
|
|
179
|
+
"source": [
|
|
180
|
+
"# instantiate LangChainAnthropicChat with the kfinance client\n",
|
|
181
|
+
"anthropic_chat = LangChainAnthropicChat(kfinance_client)\n",
|
|
182
|
+
"# start chatting with LangChainAnthropicChat\n",
|
|
183
|
+
"anthropic_chat.start_chatting()"
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
],
|
|
187
|
+
"metadata": {
|
|
188
|
+
"language_info": {
|
|
189
|
+
"name": "python"
|
|
190
|
+
},
|
|
191
|
+
"orig_nbformat": 4,
|
|
192
|
+
"kernelspec": {
|
|
193
|
+
"name": "python3",
|
|
194
|
+
"language": "python",
|
|
195
|
+
"display_name": "Python 3 (ipykernel)"
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
"nbformat": 4,
|
|
199
|
+
"nbformat_minor": 2
|
|
200
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"attachments": {},
|
|
5
|
+
"cell_type": "markdown",
|
|
6
|
+
"metadata": {},
|
|
7
|
+
"source": [
|
|
8
|
+
"##### Copyright 2025 Kensho Technologies, LLC"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"attachments": {},
|
|
13
|
+
"cell_type": "markdown",
|
|
14
|
+
"metadata": {
|
|
15
|
+
"id": "-zOQ3ZfKSrT2"
|
|
16
|
+
},
|
|
17
|
+
"source": [
|
|
18
|
+
"# LangChain Tool Calling with Google Gemini\n",
|
|
19
|
+
"**_Gemini to retrieve data from the LLM-ready API using the kFinance python library!_**\n",
|
|
20
|
+
"\n",
|
|
21
|
+
"What you'll need to run this notebook:\n",
|
|
22
|
+
"\n",
|
|
23
|
+
"1. kFinance credentials\n",
|
|
24
|
+
"2. An Gemini API key\n",
|
|
25
|
+
"\n",
|
|
26
|
+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
|
|
27
|
+
" <td>\n",
|
|
28
|
+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/kensho-technologies/kfinance/blob/main/example_notebooks/tool_calling/langchain/google_gemini_langchain_tool_calling.ipynb\"><img src=\"../../../images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
|
|
29
|
+
" </td>\n",
|
|
30
|
+
"</table>"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"attachments": {},
|
|
35
|
+
"cell_type": "markdown",
|
|
36
|
+
"metadata": {
|
|
37
|
+
"id": "ruP6MHfzR2Y4"
|
|
38
|
+
},
|
|
39
|
+
"source": [
|
|
40
|
+
"## Install dependencies"
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"cell_type": "code",
|
|
45
|
+
"execution_count": null,
|
|
46
|
+
"metadata": {
|
|
47
|
+
"colab": {
|
|
48
|
+
"base_uri": "https://localhost:8080/"
|
|
49
|
+
},
|
|
50
|
+
"collapsed": true,
|
|
51
|
+
"id": "YAwU0_7TRhV7",
|
|
52
|
+
"jupyter": {
|
|
53
|
+
"outputs_hidden": true
|
|
54
|
+
},
|
|
55
|
+
"outputId": "e6ebf89c-7680-4852-e52b-c99610107cdf"
|
|
56
|
+
},
|
|
57
|
+
"outputs": [],
|
|
58
|
+
"source": [
|
|
59
|
+
"# install the latest version of kFinance package\n",
|
|
60
|
+
"%pip install kensho-kfinance\n",
|
|
61
|
+
"# install langchain and gemini\n",
|
|
62
|
+
"%pip install langchain\n",
|
|
63
|
+
"%pip install google-generativeai\n",
|
|
64
|
+
"%pip install langchain-google-genai"
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"attachments": {},
|
|
69
|
+
"cell_type": "markdown",
|
|
70
|
+
"metadata": {
|
|
71
|
+
"id": "e_G2p3tTSHeo"
|
|
72
|
+
},
|
|
73
|
+
"source": [
|
|
74
|
+
"# Instantiate kFinance Client"
|
|
75
|
+
]
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"cell_type": "code",
|
|
79
|
+
"execution_count": null,
|
|
80
|
+
"metadata": {
|
|
81
|
+
"id": "7QPnfPuX87rg"
|
|
82
|
+
},
|
|
83
|
+
"outputs": [],
|
|
84
|
+
"source": [
|
|
85
|
+
"# import the kfinance client\n",
|
|
86
|
+
"import sys\n",
|
|
87
|
+
"from kfinance.kfinance import Client\n",
|
|
88
|
+
"# check if the current environment is a Google Colab\n",
|
|
89
|
+
"try:\n",
|
|
90
|
+
" import google.colab\n",
|
|
91
|
+
" IN_GOOGLE_COLAB = True\n",
|
|
92
|
+
"except:\n",
|
|
93
|
+
" IN_GOOGLE_COLAB = False\n",
|
|
94
|
+
"\n",
|
|
95
|
+
"# initialize the kfinance client with one of the following:\n",
|
|
96
|
+
"# 1. your kensho refresh token\n",
|
|
97
|
+
"# 2. your kensho client id and kensho private key\n",
|
|
98
|
+
"# 3. automated login (not accessible on Google Collab)\n",
|
|
99
|
+
"if IN_GOOGLE_COLAB:\n",
|
|
100
|
+
" kensho_refresh_token = \"\"\n",
|
|
101
|
+
" assert kensho_refresh_token != \"\", \"kensho refresh token is empty! Make sure to enter your kensho refresh token above\"\n",
|
|
102
|
+
" kfinance_client = Client(refresh_token=kensho_refresh_token)\n",
|
|
103
|
+
"\n",
|
|
104
|
+
" # kensho_client_id = \"\"\n",
|
|
105
|
+
" # kensho_private_key = \"\"\n",
|
|
106
|
+
" # assert kensho_client_id != \"\", \"kensho client id is empty! Make sure to enter your kensho client id above\"\n",
|
|
107
|
+
" # assert kensho_private_key != \"\", \"kensho private key is empty! Make sure to enter your kensho private key above\"\n",
|
|
108
|
+
" # kfinance_client = Client(client_id=kensho_client_id, private_key=kensho_private_key)\n",
|
|
109
|
+
"else:\n",
|
|
110
|
+
" kfinance_client = Client()\n"
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"attachments": {},
|
|
115
|
+
"cell_type": "markdown",
|
|
116
|
+
"metadata": {
|
|
117
|
+
"id": "LvQy_66Jioys"
|
|
118
|
+
},
|
|
119
|
+
"source": [
|
|
120
|
+
"# LangChain Function Calling with Gemini"
|
|
121
|
+
]
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"cell_type": "code",
|
|
125
|
+
"execution_count": null,
|
|
126
|
+
"metadata": {
|
|
127
|
+
"id": "WkoRuipOEQz6"
|
|
128
|
+
},
|
|
129
|
+
"outputs": [],
|
|
130
|
+
"source": [
|
|
131
|
+
"from kfinance.tool_calling.prompts import BASE_PROMPT\n",
|
|
132
|
+
"# import langchain\n",
|
|
133
|
+
"from langchain.agents import AgentExecutor, create_tool_calling_agent\n",
|
|
134
|
+
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
|
135
|
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
|
136
|
+
"\n",
|
|
137
|
+
"class LangChainGeminiChat:\n",
|
|
138
|
+
" def __init__(self, kfinance_client: Client) -> None:\n",
|
|
139
|
+
" # Initialize Gemini with your Gemini API key\n",
|
|
140
|
+
" gemini_api_key = \"\" # replace with your own key\n",
|
|
141
|
+
" assert gemini_api_key != \"\", \"Gemini API key is empty! Make sure to enter your Gemini API key above\"\n",
|
|
142
|
+
" llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", google_api_key=gemini_api_key)\n",
|
|
143
|
+
" \n",
|
|
144
|
+
" # Prompt\n",
|
|
145
|
+
" prompt = ChatPromptTemplate.from_messages(\n",
|
|
146
|
+
" [\n",
|
|
147
|
+
" (\"system\", BASE_PROMPT),\n",
|
|
148
|
+
" MessagesPlaceholder(\"chat_history\", optional=True),\n",
|
|
149
|
+
" (\"human\", \"{input}\"),\n",
|
|
150
|
+
" MessagesPlaceholder(\"agent_scratchpad\"),\n",
|
|
151
|
+
" ]\n",
|
|
152
|
+
" )\n",
|
|
153
|
+
" # Construct the tool calling agent\n",
|
|
154
|
+
" agent = create_tool_calling_agent(llm=llm, tools=kfinance_client.langchain_tools, prompt=prompt)\n",
|
|
155
|
+
" # Create an agent executor by passing in the agent and tools\n",
|
|
156
|
+
" self.agent_executor = AgentExecutor(agent=agent, tools=kfinance_client.langchain_tools, verbose=True)\n",
|
|
157
|
+
"\n",
|
|
158
|
+
"\n",
|
|
159
|
+
" def start_chatting(self) -> None:\n",
|
|
160
|
+
" \"\"\"Open chat shell\"\"\"\n",
|
|
161
|
+
" while True:\n",
|
|
162
|
+
" user_input = input(\"Enter your message and press the [return] key\\n\")\n",
|
|
163
|
+
" self.agent_executor.invoke({\"input\": user_input})\n",
|
|
164
|
+
" sys.stdout.write(\"\\n\")\n"
|
|
165
|
+
]
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"cell_type": "code",
|
|
169
|
+
"execution_count": null,
|
|
170
|
+
"metadata": {},
|
|
171
|
+
"outputs": [],
|
|
172
|
+
"source": [
|
|
173
|
+
"# instantiate LangChainGeminiChat with the kfinance client\n",
|
|
174
|
+
"gemini_chat = LangChainGeminiChat(kfinance_client)\n",
|
|
175
|
+
"# start chatting with LangChainGeminiChat\n",
|
|
176
|
+
"gemini_chat.start_chatting()"
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
"metadata": {
|
|
181
|
+
"language_info": {
|
|
182
|
+
"name": "python"
|
|
183
|
+
},
|
|
184
|
+
"orig_nbformat": 4,
|
|
185
|
+
"kernelspec": {
|
|
186
|
+
"name": "python3",
|
|
187
|
+
"language": "python",
|
|
188
|
+
"display_name": "Python 3 (ipykernel)"
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
"nbformat": 4,
|
|
192
|
+
"nbformat_minor": 2
|
|
193
|
+
}
|