kensho-kfinance 1.2.2__tar.gz → 2.0.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-1.2.2 → kensho_kfinance-2.0.0}/.gitignore +4 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/PKG-INFO +5 -3
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/README.md +1 -1
- kensho_kfinance-2.0.0/docs/build_tool_calling_documentation.py +123 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/conf.py +11 -5
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/index.rst +2 -1
- kensho_kfinance-2.0.0/docs/tool_calling.rst +17 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/justfile +1 -1
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kensho_kfinance.egg-info/PKG-INFO +5 -3
- kensho_kfinance-2.0.0/kensho_kfinance.egg-info/SOURCES.txt +64 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kensho_kfinance.egg-info/requires.txt +3 -1
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/CHANGELOG.md +3 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/constants.py +40 -8
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/fetch.py +15 -9
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/kfinance.py +128 -38
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/meta_classes.py +3 -9
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/tests/test_fetch.py +7 -6
- kensho_kfinance-2.0.0/kfinance/tests/test_tools.py +430 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/README.md +38 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/__init__.py +47 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_business_relationship_from_identifier.py +28 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_capitalization_from_identifier.py +35 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_company_id_from_identifier.py +14 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_cusip_from_ticker.py +18 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_earnings_call_datetimes_from_identifier.py +17 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_financial_line_item_from_identifier.py +45 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_financial_statement_from_identifier.py +41 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_history_metadata_from_identifier.py +15 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_info_from_identifier.py +14 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_isin_from_ticker.py +18 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_latest.py +21 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_n_quarters_ago.py +21 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_prices_from_identifier.py +44 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_security_id_from_identifier.py +14 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/get_trading_item_id_from_identifier.py +14 -0
- kensho_kfinance-2.0.0/kfinance/tool_calling/shared_models.py +53 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/version.py +2 -2
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/pyproject.toml +4 -1
- kensho_kfinance-1.2.2/docs/llm_tools.rst +0 -7
- kensho_kfinance-1.2.2/kensho_kfinance.egg-info/SOURCES.txt +0 -46
- kensho_kfinance-1.2.2/kfinance/llm_tools.py +0 -747
- kensho_kfinance-1.2.2/kfinance/tool_schemas.py +0 -148
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/.coveragerc +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/.github/workflows/ci-lint.yml +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/.github/workflows/ci-test.yml +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/.github/workflows/python-publish.yml +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/.readthedocs.yaml +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/AUTHORS.md +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/CODE_OF_CONDUCT.md +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/CONTRIBUTING.md +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/LICENSE +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/kfinance.rst +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/requirements.txt +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/templates/apidoc/package.rst_t +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/docs/templates/apidoc/toc.rst_t +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kensho_kfinance.egg-info/dependency_links.txt +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kensho_kfinance.egg-info/top_level.txt +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/__init__.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/batch_request_handling.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/prompt.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/py.typed +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/server_thread.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/tests/__init__.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/tests/test_batch_requests.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/kfinance/tests/test_objects.py +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/scripts/copyright_line_check.sh +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/scripts/lint.sh +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/scripts/test.sh +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/setup.cfg +0 -0
- {kensho_kfinance-1.2.2 → kensho_kfinance-2.0.0}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kensho-kfinance
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.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,6 +13,7 @@ Description-Content-Type: text/markdown
|
|
|
13
13
|
License-File: LICENSE
|
|
14
14
|
License-File: AUTHORS.md
|
|
15
15
|
Requires-Dist: langchain-core>=0.3.15
|
|
16
|
+
Requires-Dist: langchain-google-genai<3,>=2.1.0
|
|
16
17
|
Requires-Dist: numpy>=1.22.4
|
|
17
18
|
Requires-Dist: pandas>=2.0.0
|
|
18
19
|
Requires-Dist: pillow>=10
|
|
@@ -29,15 +30,16 @@ Requires-Dist: coverage<8,>=7.6.10; extra == "dev"
|
|
|
29
30
|
Requires-Dist: mypy<2,>=1.15.0; extra == "dev"
|
|
30
31
|
Requires-Dist: pytest<7,>=6.1.2; extra == "dev"
|
|
31
32
|
Requires-Dist: pytest-cov<7,>=6.0.0; extra == "dev"
|
|
32
|
-
Requires-Dist: requests_mock<
|
|
33
|
+
Requires-Dist: requests_mock<2,>=1.12; extra == "dev"
|
|
33
34
|
Requires-Dist: ruff<1,>=0.9.4; extra == "dev"
|
|
35
|
+
Requires-Dist: time_machine<3,>=2.1; extra == "dev"
|
|
34
36
|
Dynamic: license-file
|
|
35
37
|
|
|
36
38
|
# kFinance
|
|
37
39
|
|
|
38
40
|
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.
|
|
39
41
|
|
|
40
|
-
For a complete overview of the functions, usage, and features of the kFinance Python library, please refer to documentation [here](https://kfinance.
|
|
42
|
+
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/).
|
|
41
43
|
|
|
42
44
|
Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-maintainers@kensho.com).
|
|
43
45
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
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
4
|
|
|
5
|
-
For a complete overview of the functions, usage, and features of the kFinance Python library, please refer to documentation [here](https://kfinance.
|
|
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
6
|
|
|
7
7
|
Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-maintainers@kensho.com).
|
|
8
8
|
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from langchain_core.utils.function_calling import convert_to_openai_tool
|
|
6
|
+
from pydantic._internal._repr import display_as_type
|
|
7
|
+
|
|
8
|
+
from kfinance.kfinance import Client
|
|
9
|
+
from kfinance.tool_calling import ALL_TOOLS, KfinanceTool
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def add_tool_calling_docs_for_all_tools() -> None:
|
|
13
|
+
"""Add tool calling documentation for all tools.
|
|
14
|
+
|
|
15
|
+
Adding tool calling documentation involves two steps:
|
|
16
|
+
- Add a function definition to the tool file
|
|
17
|
+
- Add the module to tool_calling.rst to instruct sphinx what tool functions to document.
|
|
18
|
+
|
|
19
|
+
Do not commit changes that result from executing this function.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
for tool_cls in ALL_TOOLS:
|
|
23
|
+
tool = tool_cls(kfinance_client=Client(refresh_token="fake"))
|
|
24
|
+
add_function_to_tools_file(tool)
|
|
25
|
+
add_module_to_tool_calling_rst(tool)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def add_function_to_tools_file(tool: KfinanceTool) -> None:
|
|
29
|
+
"""Add a function definition to each tool file, which mimics the tool definition passed to LLMs
|
|
30
|
+
|
|
31
|
+
The KfinanceTools are primarily intended to be transpiled for use with langchain, openai,
|
|
32
|
+
anthropic etc. However, the automatically generated documentation is not very helpful.
|
|
33
|
+
Instead, we use the tool definition to create a function definition that's close to what we
|
|
34
|
+
pass to the LLM. We then use sphinx to generate docs only for these generated functions.
|
|
35
|
+
|
|
36
|
+
We append the function definition to the same file as the tool. This ensures that the
|
|
37
|
+
[source] field in the documentation links to the file where the tool is defined.
|
|
38
|
+
|
|
39
|
+
Here's an example from the `GetNQuartersAgo` tool:
|
|
40
|
+
|
|
41
|
+
def get_n_quarters_ago(n: int) -> kfinance.constants.YearAndQuarter:
|
|
42
|
+
'''Get the year and quarter corresponding to [n] quarters before the current quarter.
|
|
43
|
+
|
|
44
|
+
:param n: Number of quarters before the current quarter
|
|
45
|
+
:type n: int
|
|
46
|
+
:rtype: YearAndQuarter'''
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
# The signature built with the inspect module does not include necessary imports.
|
|
51
|
+
imports = [
|
|
52
|
+
"import kfinance",
|
|
53
|
+
"import datetime",
|
|
54
|
+
"from typing import Optional"
|
|
55
|
+
]
|
|
56
|
+
signature_str = build_signature_str(tool)
|
|
57
|
+
|
|
58
|
+
# Use inspect to retrieve the return type and add it to the imports if it's not a builtin.
|
|
59
|
+
return_annotation = inspect.signature(tool._run).return_annotation
|
|
60
|
+
if return_annotation.__module__ != "builtins":
|
|
61
|
+
imports.append(f"from {return_annotation.__module__} import {return_annotation.__name__}")
|
|
62
|
+
|
|
63
|
+
# Generate sphinx style annotations for each param.
|
|
64
|
+
openai_params = convert_to_openai_tool(tool)["function"]["parameters"]["properties"]
|
|
65
|
+
args = ""
|
|
66
|
+
for field_name, field_metadata in tool.args_schema.model_fields.items():
|
|
67
|
+
# We use the openai definition to extract the field description. This means that, just like
|
|
68
|
+
# pydantic/langchain, we use the docstring of an enum as the description for an enum field.
|
|
69
|
+
try:
|
|
70
|
+
openai_params[field_name]['description']
|
|
71
|
+
except:
|
|
72
|
+
assert False, tool.name
|
|
73
|
+
args += f"\n :param {field_name}: {openai_params[field_name]['description']}"
|
|
74
|
+
args += f"\n :type {field_name}: {display_as_type(field_metadata.annotation)}"
|
|
75
|
+
|
|
76
|
+
func_str = "\n" + "\n".join(imports) + "\n"
|
|
77
|
+
func_str += f"{signature_str}"
|
|
78
|
+
func_str += f'\n """{tool.description}\n'
|
|
79
|
+
func_str += args
|
|
80
|
+
# Add sphinx style return annotation
|
|
81
|
+
func_str += f'\n :rtype: {return_annotation.__name__}"""'
|
|
82
|
+
|
|
83
|
+
# Write definition to tool file
|
|
84
|
+
with open(inspect.getfile(tool.__class__), mode="a") as f:
|
|
85
|
+
f.write(func_str)
|
|
86
|
+
|
|
87
|
+
def build_signature_str(tool) -> str:
|
|
88
|
+
"""Return the signature string of the tool
|
|
89
|
+
|
|
90
|
+
Return value example:
|
|
91
|
+
def get_latest(use_local_timezone: bool = True) -> kfinance.constants.LatestPeriods:
|
|
92
|
+
|
|
93
|
+
This function is mostly necessary for proper enum handling.
|
|
94
|
+
inspect.Parameter uses __repr__ to stringify enum default values, not __str__.
|
|
95
|
+
As a result, it formats default values as "<Periodicity.day: 'day'>" rather than
|
|
96
|
+
"Periodicity.day".
|
|
97
|
+
"""
|
|
98
|
+
signature = inspect.signature(tool._run)
|
|
99
|
+
|
|
100
|
+
for param in signature.parameters.values():
|
|
101
|
+
if isinstance(param.default, Enum):
|
|
102
|
+
# For enums, redirect __repr__ to __str__
|
|
103
|
+
param.default.__class__.__repr__ = param.default.__class__.__str__
|
|
104
|
+
return f"def {tool.name}{signature}:"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def add_module_to_tool_calling_rst(tool: KfinanceTool) -> None:
|
|
109
|
+
"""Add a module for each tool to tool_calling.rst.
|
|
110
|
+
|
|
111
|
+
We only want to include the generated function in the docs.
|
|
112
|
+
|
|
113
|
+
Example:
|
|
114
|
+
.. automodule:: kfinance.tool_calling.get_latest
|
|
115
|
+
:members: get_latest
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
module_str = "\n"
|
|
119
|
+
module_str += f"\n.. automodule:: {tool.__module__}"
|
|
120
|
+
module_str += f"\n :members: {tool.name}"
|
|
121
|
+
|
|
122
|
+
with open(Path(Path(__file__).resolve().parent, "tool_calling.rst"), mode="a") as f:
|
|
123
|
+
f.write(module_str)
|
|
@@ -7,15 +7,21 @@
|
|
|
7
7
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
|
8
8
|
from importlib.metadata import version as get_version
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
from docs.build_tool_calling_documentation import add_tool_calling_docs_for_all_tools
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
project = "kensho-kfinance"
|
|
14
|
+
copyright = "2025, Kensho Technologies"
|
|
15
|
+
author = "Kensho Technologies"
|
|
16
|
+
|
|
17
|
+
# Add documentation for tool calling.
|
|
18
|
+
add_tool_calling_docs_for_all_tools()
|
|
13
19
|
|
|
14
20
|
# borrowed from here:
|
|
15
21
|
# https://setuptools-scm.readthedocs.io/en/latest/usage/#usage-from-sphinx
|
|
16
22
|
release: str = get_version(project)
|
|
17
23
|
# for example take major/minor
|
|
18
|
-
version: str = ".".join(release.split(
|
|
24
|
+
version: str = ".".join(release.split(".")[:2])
|
|
19
25
|
|
|
20
26
|
# -- General configuration ---------------------------------------------------
|
|
21
27
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
|
@@ -57,7 +63,7 @@ source_suffix = [".rst", ".md"]
|
|
|
57
63
|
# -- Options for HTML output -------------------------------------------------
|
|
58
64
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
|
59
65
|
|
|
60
|
-
html_theme =
|
|
66
|
+
html_theme = "sphinx_rtd_theme"
|
|
61
67
|
|
|
62
68
|
# https://sphinx-rtd-theme.readthedocs.io/en/stable/configuring.html
|
|
63
69
|
html_theme_options = {"body_min_width": 0, "body_max_width": "none"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Tool Calling
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
Below, you can find all the tools that are available for tool calling.
|
|
5
|
+
|
|
6
|
+
For examples on how to call these tools via OpenAI, Anthropic, or Gemini, see the
|
|
7
|
+
`LLM-ready API examples. <https://github.com/kensho-technologies/llm-ready-api-examples>`_
|
|
8
|
+
|
|
9
|
+
Note: The tool definitions below cannot be called directly and are intended for documentation only.
|
|
10
|
+
They are auto-generated from langchain
|
|
11
|
+
`BaseTools. <https://api.python.langchain.com/en/latest/tools/langchain_core.tools.BaseTool.html>`_
|
|
12
|
+
To see the BaseTool behind a function definition, click on the source of the definition.
|
|
13
|
+
|
|
14
|
+
.. The contents of tool_calling.rst get auto-generated by
|
|
15
|
+
.. build_tool_calling_documentation.py. Don't commit any content below
|
|
16
|
+
.. this comment.
|
|
17
|
+
|
|
@@ -34,4 +34,4 @@ unit-test *args:
|
|
|
34
34
|
# First, copy the dependencies in docs/requirements.txt into pyproject.toml and install in a venv.
|
|
35
35
|
# Don't merge changes to pyproject and docs/output into the remote repo!
|
|
36
36
|
sphinx *args:
|
|
37
|
-
sphinx-build docs docs/output
|
|
37
|
+
PYTHONPATH="${PYTHONPATH:-}:." sphinx-build docs docs/output
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kensho-kfinance
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.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,6 +13,7 @@ Description-Content-Type: text/markdown
|
|
|
13
13
|
License-File: LICENSE
|
|
14
14
|
License-File: AUTHORS.md
|
|
15
15
|
Requires-Dist: langchain-core>=0.3.15
|
|
16
|
+
Requires-Dist: langchain-google-genai<3,>=2.1.0
|
|
16
17
|
Requires-Dist: numpy>=1.22.4
|
|
17
18
|
Requires-Dist: pandas>=2.0.0
|
|
18
19
|
Requires-Dist: pillow>=10
|
|
@@ -29,15 +30,16 @@ Requires-Dist: coverage<8,>=7.6.10; extra == "dev"
|
|
|
29
30
|
Requires-Dist: mypy<2,>=1.15.0; extra == "dev"
|
|
30
31
|
Requires-Dist: pytest<7,>=6.1.2; extra == "dev"
|
|
31
32
|
Requires-Dist: pytest-cov<7,>=6.0.0; extra == "dev"
|
|
32
|
-
Requires-Dist: requests_mock<
|
|
33
|
+
Requires-Dist: requests_mock<2,>=1.12; extra == "dev"
|
|
33
34
|
Requires-Dist: ruff<1,>=0.9.4; extra == "dev"
|
|
35
|
+
Requires-Dist: time_machine<3,>=2.1; extra == "dev"
|
|
34
36
|
Dynamic: license-file
|
|
35
37
|
|
|
36
38
|
# kFinance
|
|
37
39
|
|
|
38
40
|
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.
|
|
39
41
|
|
|
40
|
-
For a complete overview of the functions, usage, and features of the kFinance Python library, please refer to documentation [here](https://kfinance.
|
|
42
|
+
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/).
|
|
41
43
|
|
|
42
44
|
Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-maintainers@kensho.com).
|
|
43
45
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
.coveragerc
|
|
2
|
+
.gitignore
|
|
3
|
+
.readthedocs.yaml
|
|
4
|
+
AUTHORS.md
|
|
5
|
+
CODE_OF_CONDUCT.md
|
|
6
|
+
CONTRIBUTING.md
|
|
7
|
+
LICENSE
|
|
8
|
+
README.md
|
|
9
|
+
justfile
|
|
10
|
+
pyproject.toml
|
|
11
|
+
setup.py
|
|
12
|
+
.github/workflows/ci-lint.yml
|
|
13
|
+
.github/workflows/ci-test.yml
|
|
14
|
+
.github/workflows/python-publish.yml
|
|
15
|
+
docs/build_tool_calling_documentation.py
|
|
16
|
+
docs/conf.py
|
|
17
|
+
docs/index.rst
|
|
18
|
+
docs/kfinance.rst
|
|
19
|
+
docs/requirements.txt
|
|
20
|
+
docs/tool_calling.rst
|
|
21
|
+
docs/templates/apidoc/package.rst_t
|
|
22
|
+
docs/templates/apidoc/toc.rst_t
|
|
23
|
+
kensho_kfinance.egg-info/PKG-INFO
|
|
24
|
+
kensho_kfinance.egg-info/SOURCES.txt
|
|
25
|
+
kensho_kfinance.egg-info/dependency_links.txt
|
|
26
|
+
kensho_kfinance.egg-info/requires.txt
|
|
27
|
+
kensho_kfinance.egg-info/top_level.txt
|
|
28
|
+
kfinance/CHANGELOG.md
|
|
29
|
+
kfinance/__init__.py
|
|
30
|
+
kfinance/batch_request_handling.py
|
|
31
|
+
kfinance/constants.py
|
|
32
|
+
kfinance/fetch.py
|
|
33
|
+
kfinance/kfinance.py
|
|
34
|
+
kfinance/meta_classes.py
|
|
35
|
+
kfinance/prompt.py
|
|
36
|
+
kfinance/py.typed
|
|
37
|
+
kfinance/server_thread.py
|
|
38
|
+
kfinance/version.py
|
|
39
|
+
kfinance/tests/__init__.py
|
|
40
|
+
kfinance/tests/test_batch_requests.py
|
|
41
|
+
kfinance/tests/test_fetch.py
|
|
42
|
+
kfinance/tests/test_objects.py
|
|
43
|
+
kfinance/tests/test_tools.py
|
|
44
|
+
kfinance/tool_calling/README.md
|
|
45
|
+
kfinance/tool_calling/__init__.py
|
|
46
|
+
kfinance/tool_calling/get_business_relationship_from_identifier.py
|
|
47
|
+
kfinance/tool_calling/get_capitalization_from_identifier.py
|
|
48
|
+
kfinance/tool_calling/get_company_id_from_identifier.py
|
|
49
|
+
kfinance/tool_calling/get_cusip_from_ticker.py
|
|
50
|
+
kfinance/tool_calling/get_earnings_call_datetimes_from_identifier.py
|
|
51
|
+
kfinance/tool_calling/get_financial_line_item_from_identifier.py
|
|
52
|
+
kfinance/tool_calling/get_financial_statement_from_identifier.py
|
|
53
|
+
kfinance/tool_calling/get_history_metadata_from_identifier.py
|
|
54
|
+
kfinance/tool_calling/get_info_from_identifier.py
|
|
55
|
+
kfinance/tool_calling/get_isin_from_ticker.py
|
|
56
|
+
kfinance/tool_calling/get_latest.py
|
|
57
|
+
kfinance/tool_calling/get_n_quarters_ago.py
|
|
58
|
+
kfinance/tool_calling/get_prices_from_identifier.py
|
|
59
|
+
kfinance/tool_calling/get_security_id_from_identifier.py
|
|
60
|
+
kfinance/tool_calling/get_trading_item_id_from_identifier.py
|
|
61
|
+
kfinance/tool_calling/shared_models.py
|
|
62
|
+
scripts/copyright_line_check.sh
|
|
63
|
+
scripts/lint.sh
|
|
64
|
+
scripts/test.sh
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
langchain-core>=0.3.15
|
|
2
|
+
langchain-google-genai<3,>=2.1.0
|
|
2
3
|
numpy>=1.22.4
|
|
3
4
|
pandas>=2.0.0
|
|
4
5
|
pillow>=10
|
|
@@ -16,5 +17,6 @@ coverage<8,>=7.6.10
|
|
|
16
17
|
mypy<2,>=1.15.0
|
|
17
18
|
pytest<7,>=6.1.2
|
|
18
19
|
pytest-cov<7,>=6.0.0
|
|
19
|
-
requests_mock<
|
|
20
|
+
requests_mock<2,>=1.12
|
|
20
21
|
ruff<1,>=0.9.4
|
|
22
|
+
time_machine<3,>=2.1
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from datetime import date
|
|
2
|
+
from enum import Enum
|
|
2
3
|
from itertools import chain
|
|
3
4
|
from typing import TypedDict
|
|
4
5
|
|
|
@@ -26,7 +27,45 @@ class IdentificationTriple(TypedDict):
|
|
|
26
27
|
company_id: int
|
|
27
28
|
|
|
28
29
|
|
|
30
|
+
class Capitalization(Enum):
|
|
31
|
+
"""The capitalization type"""
|
|
32
|
+
|
|
33
|
+
market_cap = "market_cap"
|
|
34
|
+
tev = "tev"
|
|
35
|
+
shares_outstanding = "shares_outstanding"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class PeriodType(Enum):
|
|
39
|
+
"""The period type"""
|
|
40
|
+
|
|
41
|
+
annual = "annual"
|
|
42
|
+
quarterly = "quarterly"
|
|
43
|
+
ltm = "ltm"
|
|
44
|
+
ytd = "ytd"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Periodicity(Enum):
|
|
48
|
+
"""The frequency or interval at which the historical data points are sampled or aggregated. Periodicity is not the same as the date range. The date range specifies the time span over which the data is retrieved, while periodicity determines how the data within that date range is aggregated."""
|
|
49
|
+
|
|
50
|
+
day = "day"
|
|
51
|
+
week = "week"
|
|
52
|
+
month = "month"
|
|
53
|
+
year = "year"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class StatementType(Enum):
|
|
57
|
+
"""The type of financial statement"""
|
|
58
|
+
|
|
59
|
+
balance_sheet = "balance_sheet"
|
|
60
|
+
bs = "balance_sheet"
|
|
61
|
+
income_statement = "income_statement"
|
|
62
|
+
cashflow = "cashflow"
|
|
63
|
+
cf = "cashflow"
|
|
64
|
+
|
|
65
|
+
|
|
29
66
|
class BusinessRelationshipType(StrEnum):
|
|
67
|
+
"""The type of business relationship"""
|
|
68
|
+
|
|
30
69
|
supplier = "supplier"
|
|
31
70
|
customer = "customer"
|
|
32
71
|
distributor = "distributor"
|
|
@@ -1672,18 +1711,11 @@ LINE_ITEMS: list[LineItemType] = [
|
|
|
1672
1711
|
},
|
|
1673
1712
|
]
|
|
1674
1713
|
|
|
1675
|
-
LINE_ITEMS_TO_DATA_ITEM_ID = {
|
|
1676
|
-
line_item["name"]: line_item["dataitemid"] for line_item in LINE_ITEMS
|
|
1677
|
-
}
|
|
1678
1714
|
|
|
1679
|
-
LINE_ITEM_NAMES_AND_ALIASES = list(
|
|
1715
|
+
LINE_ITEM_NAMES_AND_ALIASES: list[str] = list(
|
|
1680
1716
|
chain(*[[line_item["name"]] + list(line_item["aliases"]) for line_item in LINE_ITEMS])
|
|
1681
1717
|
)
|
|
1682
1718
|
|
|
1683
|
-
LINE_ITEM_SYNONYMS = {
|
|
1684
|
-
alias: line_item["dataitemid"] for line_item in LINE_ITEMS for alias in line_item["aliases"]
|
|
1685
|
-
}
|
|
1686
|
-
|
|
1687
1719
|
|
|
1688
1720
|
FINANCIAL_STATEMENTS: dict[str, int] = {
|
|
1689
1721
|
"income_statement": 1,
|
|
@@ -7,7 +7,13 @@ from uuid import uuid4
|
|
|
7
7
|
import jwt
|
|
8
8
|
import requests
|
|
9
9
|
|
|
10
|
-
from .constants import
|
|
10
|
+
from .constants import (
|
|
11
|
+
BusinessRelationshipType,
|
|
12
|
+
IdentificationTriple,
|
|
13
|
+
IndustryClassification,
|
|
14
|
+
Periodicity,
|
|
15
|
+
PeriodType,
|
|
16
|
+
)
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
# version.py gets autogenerated by setuptools-scm and is not available
|
|
@@ -228,14 +234,14 @@ class KFinanceApiClient:
|
|
|
228
234
|
is_adjusted: bool = True,
|
|
229
235
|
start_date: Optional[str] = None,
|
|
230
236
|
end_date: Optional[str] = None,
|
|
231
|
-
periodicity: Optional[
|
|
237
|
+
periodicity: Optional[Periodicity] = None,
|
|
232
238
|
) -> dict:
|
|
233
239
|
"""Get the pricing history."""
|
|
234
240
|
url = (
|
|
235
241
|
f"{self.url_base}pricing/{trading_item_id}/"
|
|
236
242
|
f"{start_date if start_date is not None else 'none'}/"
|
|
237
243
|
f"{end_date if end_date is not None else 'none'}/"
|
|
238
|
-
f"{periodicity if periodicity
|
|
244
|
+
f"{periodicity.value if periodicity else 'none'}/"
|
|
239
245
|
f"{'adjusted' if is_adjusted else 'unadjusted'}"
|
|
240
246
|
)
|
|
241
247
|
return self.fetch(url)
|
|
@@ -265,14 +271,14 @@ class KFinanceApiClient:
|
|
|
265
271
|
is_adjusted: bool = True,
|
|
266
272
|
start_date: Optional[str] = None,
|
|
267
273
|
end_date: Optional[str] = None,
|
|
268
|
-
periodicity: Optional[
|
|
274
|
+
periodicity: Optional[Periodicity] = None,
|
|
269
275
|
) -> bytes:
|
|
270
276
|
"""Get the price chart."""
|
|
271
277
|
url = (
|
|
272
278
|
f"{self.url_base}price_chart/{trading_item_id}/"
|
|
273
279
|
f"{start_date if start_date is not None else 'none'}/"
|
|
274
280
|
f"{end_date if end_date is not None else 'none'}/"
|
|
275
|
-
f"{periodicity if periodicity
|
|
281
|
+
f"{periodicity.value if periodicity else 'none'}/"
|
|
276
282
|
f"{'adjusted' if is_adjusted else 'unadjusted'}"
|
|
277
283
|
)
|
|
278
284
|
|
|
@@ -291,7 +297,7 @@ class KFinanceApiClient:
|
|
|
291
297
|
self,
|
|
292
298
|
company_id: int,
|
|
293
299
|
statement_type: str,
|
|
294
|
-
period_type: Optional[
|
|
300
|
+
period_type: Optional[PeriodType] = None,
|
|
295
301
|
start_year: Optional[int] = None,
|
|
296
302
|
end_year: Optional[int] = None,
|
|
297
303
|
start_quarter: Optional[int] = None,
|
|
@@ -300,7 +306,7 @@ class KFinanceApiClient:
|
|
|
300
306
|
"""Get a specified financial statement for a specified duration."""
|
|
301
307
|
url = (
|
|
302
308
|
f"{self.url_base}statements/{company_id}/{statement_type}/"
|
|
303
|
-
f"{period_type if period_type
|
|
309
|
+
f"{period_type.value if period_type else 'none'}/"
|
|
304
310
|
f"{start_year if start_year is not None else 'none'}/"
|
|
305
311
|
f"{end_year if end_year is not None else 'none'}/"
|
|
306
312
|
f"{start_quarter if start_quarter is not None else 'none'}/"
|
|
@@ -312,7 +318,7 @@ class KFinanceApiClient:
|
|
|
312
318
|
self,
|
|
313
319
|
company_id: int,
|
|
314
320
|
line_item: str,
|
|
315
|
-
period_type: Optional[
|
|
321
|
+
period_type: Optional[PeriodType] = None,
|
|
316
322
|
start_year: Optional[int] = None,
|
|
317
323
|
end_year: Optional[int] = None,
|
|
318
324
|
start_quarter: Optional[int] = None,
|
|
@@ -321,7 +327,7 @@ class KFinanceApiClient:
|
|
|
321
327
|
"""Get a specified financial line item for a specified duration."""
|
|
322
328
|
url = (
|
|
323
329
|
f"{self.url_base}line_item/{company_id}/{line_item}/"
|
|
324
|
-
f"{period_type if period_type
|
|
330
|
+
f"{period_type.value if period_type else 'none'}/"
|
|
325
331
|
f"{start_year if start_year is not None else 'none'}/"
|
|
326
332
|
f"{end_year if end_year is not None else 'none'}/"
|
|
327
333
|
f"{start_quarter if start_quarter is not None else 'none'}/"
|