kensho-kfinance 1.2.2__py3-none-any.whl → 2.0.1__py3-none-any.whl

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.

Files changed (35) hide show
  1. {kensho_kfinance-1.2.2.dist-info → kensho_kfinance-2.0.1.dist-info}/METADATA +5 -3
  2. kensho_kfinance-2.0.1.dist-info/RECORD +40 -0
  3. {kensho_kfinance-1.2.2.dist-info → kensho_kfinance-2.0.1.dist-info}/WHEEL +1 -1
  4. kfinance/CHANGELOG.md +6 -0
  5. kfinance/constants.py +40 -8
  6. kfinance/fetch.py +15 -9
  7. kfinance/kfinance.py +128 -38
  8. kfinance/meta_classes.py +3 -9
  9. kfinance/tests/test_fetch.py +7 -6
  10. kfinance/tests/test_tools.py +430 -0
  11. kfinance/tool_calling/README.md +38 -0
  12. kfinance/tool_calling/__init__.py +47 -0
  13. kfinance/tool_calling/get_business_relationship_from_identifier.py +28 -0
  14. kfinance/tool_calling/get_capitalization_from_identifier.py +35 -0
  15. kfinance/tool_calling/get_company_id_from_identifier.py +14 -0
  16. kfinance/tool_calling/get_cusip_from_ticker.py +18 -0
  17. kfinance/tool_calling/get_earnings_call_datetimes_from_identifier.py +17 -0
  18. kfinance/tool_calling/get_financial_line_item_from_identifier.py +45 -0
  19. kfinance/tool_calling/get_financial_statement_from_identifier.py +41 -0
  20. kfinance/tool_calling/get_history_metadata_from_identifier.py +15 -0
  21. kfinance/tool_calling/get_info_from_identifier.py +14 -0
  22. kfinance/tool_calling/get_isin_from_ticker.py +18 -0
  23. kfinance/tool_calling/get_latest.py +21 -0
  24. kfinance/tool_calling/get_n_quarters_ago.py +21 -0
  25. kfinance/tool_calling/get_prices_from_identifier.py +44 -0
  26. kfinance/tool_calling/get_security_id_from_identifier.py +14 -0
  27. kfinance/tool_calling/get_trading_item_id_from_identifier.py +14 -0
  28. kfinance/tool_calling/shared_models.py +53 -0
  29. kfinance/version.py +2 -2
  30. kensho_kfinance-1.2.2.dist-info/RECORD +0 -23
  31. kfinance/llm_tools.py +0 -747
  32. kfinance/tool_schemas.py +0 -148
  33. {kensho_kfinance-1.2.2.dist-info → kensho_kfinance-2.0.1.dist-info}/licenses/AUTHORS.md +0 -0
  34. {kensho_kfinance-1.2.2.dist-info → kensho_kfinance-2.0.1.dist-info}/licenses/LICENSE +0 -0
  35. {kensho_kfinance-1.2.2.dist-info → kensho_kfinance-2.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,41 @@
1
+ from typing import Literal, Type
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+ from kfinance.constants import PeriodType, StatementType
6
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
7
+
8
+
9
+ class GetFinancialStatementFromIdentifierArgs(ToolArgsWithIdentifier):
10
+ # no description because the description for enum fields comes from the enum docstring.
11
+ statement: StatementType
12
+ period_type: PeriodType | None = Field(default=None, description="The period type")
13
+ start_year: int | None = Field(default=None, description="The starting year for the data range")
14
+ end_year: int | None = Field(default=None, description="The ending year for the data range")
15
+ start_quarter: Literal[1, 2, 3, 4] | None = Field(default=None, description="Starting quarter")
16
+ end_quarter: Literal[1, 2, 3, 4] | None = Field(default=None, description="Ending quarter")
17
+
18
+
19
+ class GetFinancialStatementFromIdentifier(KfinanceTool):
20
+ name: str = "get_financial_statement_from_identifier"
21
+ description: str = "Get the financial statement associated with an identifier."
22
+ args_schema: Type[BaseModel] = GetFinancialStatementFromIdentifierArgs
23
+
24
+ def _run(
25
+ self,
26
+ identifier: str,
27
+ statement: StatementType,
28
+ period_type: PeriodType | None = None,
29
+ start_year: int | None = None,
30
+ end_year: int | None = None,
31
+ start_quarter: Literal[1, 2, 3, 4] | None = None,
32
+ end_quarter: Literal[1, 2, 3, 4] | None = None,
33
+ ) -> str:
34
+ ticker = self.kfinance_client.ticker(identifier)
35
+ return getattr(ticker, statement.value)(
36
+ period_type=period_type,
37
+ start_year=start_year,
38
+ end_year=end_year,
39
+ start_quarter=start_quarter,
40
+ end_quarter=end_quarter,
41
+ ).to_markdown()
@@ -0,0 +1,15 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from kfinance.constants import HistoryMetadata
6
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
7
+
8
+
9
+ class GetHistoryMetadataFromIdentifier(KfinanceTool):
10
+ name: str = "get_history_metadata_from_identifier"
11
+ description: str = "Get the history metadata associated with an identifier. History metadata includes currency, symbol, exchange name, instrument type, and first trade date."
12
+ args_schema: Type[BaseModel] = ToolArgsWithIdentifier
13
+
14
+ def _run(self, identifier: str) -> HistoryMetadata:
15
+ return self.kfinance_client.ticker(identifier).history_metadata
@@ -0,0 +1,14 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
6
+
7
+
8
+ class GetInfoFromIdentifier(KfinanceTool):
9
+ name: str = "get_info_from_identifier"
10
+ description: str = "Get the information associated with an identifier. Info includes company name, status, type, simple industry, number of employees, founding date, webpage, HQ address, HQ city, HQ zip code, HQ state, HQ country, and HQ country iso code."
11
+ args_schema: Type[BaseModel] = ToolArgsWithIdentifier
12
+
13
+ def _run(self, identifier: str) -> str:
14
+ return str(self.kfinance_client.ticker(identifier).info)
@@ -0,0 +1,18 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+ from kfinance.tool_calling.shared_models import KfinanceTool
6
+
7
+
8
+ class GetIsinFromTickerArgs(BaseModel):
9
+ ticker_str: str = Field(description="The ticker")
10
+
11
+
12
+ class GetIsinFromTicker(KfinanceTool):
13
+ name: str = "get_isin_from_ticker"
14
+ description: str = "Get the ISIN associated with a ticker."
15
+ args_schema: Type[BaseModel] = GetIsinFromTickerArgs
16
+
17
+ def _run(self, ticker_str: str) -> str:
18
+ return self.kfinance_client.ticker(ticker_str).isin
@@ -0,0 +1,21 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+ from kfinance.constants import LatestPeriods
6
+ from kfinance.tool_calling.shared_models import KfinanceTool
7
+
8
+
9
+ class GetLatestArgs(BaseModel):
10
+ use_local_timezone: bool = Field(
11
+ description="Whether to use the local timezone of the user", default=True
12
+ )
13
+
14
+
15
+ class GetLatest(KfinanceTool):
16
+ name: str = "get_latest"
17
+ description: str = "Get the latest annual reporting year, latest quarterly reporting quarter and year, and current date."
18
+ args_schema: Type[BaseModel] = GetLatestArgs
19
+
20
+ def _run(self, use_local_timezone: bool = True) -> LatestPeriods:
21
+ return self.kfinance_client.get_latest(use_local_timezone=use_local_timezone)
@@ -0,0 +1,21 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+ from kfinance.constants import YearAndQuarter
6
+ from kfinance.tool_calling.shared_models import KfinanceTool
7
+
8
+
9
+ class GetNQuartersAgoArgs(BaseModel):
10
+ n: int = Field(description="Number of quarters before the current quarter")
11
+
12
+
13
+ class GetNQuartersAgo(KfinanceTool):
14
+ name: str = "get_n_quarters_ago"
15
+ description: str = (
16
+ "Get the year and quarter corresponding to [n] quarters before the current quarter."
17
+ )
18
+ args_schema: Type[BaseModel] = GetNQuartersAgoArgs
19
+
20
+ def _run(self, n: int) -> YearAndQuarter:
21
+ return self.kfinance_client.get_n_quarters_ago(n)
@@ -0,0 +1,44 @@
1
+ from datetime import date
2
+ from typing import Type
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+ from kfinance.constants import Periodicity
7
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
8
+
9
+
10
+ class GetPricesFromIdentifierArgs(ToolArgsWithIdentifier):
11
+ start_date: date | None = Field(
12
+ description="The start date for historical price retrieval", default=None
13
+ )
14
+ end_date: date | None = Field(
15
+ description="The end date for historical price retrieval", default=None
16
+ )
17
+ # no description because the description for enum fields comes from the enum docstring.
18
+ periodicity: Periodicity = Field(default=Periodicity.day)
19
+ adjusted: bool = Field(
20
+ description="Whether to retrieve adjusted prices that account for corporate actions such as dividends and splits.",
21
+ default=True,
22
+ )
23
+
24
+
25
+ class GetPricesFromIdentifier(KfinanceTool):
26
+ name: str = "get_prices_from_identifier"
27
+ description: str = "Get the historical open, high, low, and close prices, and volume of an identifier between inclusive start_date and inclusive end date. When requesting the most recent values, leave start_date and end_date empty."
28
+ args_schema: Type[BaseModel] = GetPricesFromIdentifierArgs
29
+
30
+ def _run(
31
+ self,
32
+ identifier: str,
33
+ start_date: date | None = None,
34
+ end_date: date | None = None,
35
+ periodicity: Periodicity = Periodicity.day,
36
+ adjusted: bool = True,
37
+ ) -> str:
38
+ ticker = self.kfinance_client.ticker(identifier)
39
+ return ticker.history(
40
+ start_date=start_date.isoformat() if start_date else None,
41
+ end_date=end_date.isoformat() if end_date else None,
42
+ periodicity=periodicity,
43
+ adjusted=adjusted,
44
+ ).to_markdown()
@@ -0,0 +1,14 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
6
+
7
+
8
+ class GetSecurityIdFromIdentifier(KfinanceTool):
9
+ name: str = "get_security_id_from_identifier"
10
+ description: str = "Get the security id associated with an identifier."
11
+ args_schema: Type[BaseModel] = ToolArgsWithIdentifier
12
+
13
+ def _run(self, identifier: str) -> int:
14
+ return self.kfinance_client.ticker(identifier).security_id
@@ -0,0 +1,14 @@
1
+ from typing import Type
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from kfinance.tool_calling.shared_models import KfinanceTool, ToolArgsWithIdentifier
6
+
7
+
8
+ class GetTradingItemIdFromIdentifier(KfinanceTool):
9
+ name: str = "get_trading_item_id_from_identifier"
10
+ description: str = "Get the trading item id associated with an identifier."
11
+ args_schema: Type[BaseModel] = ToolArgsWithIdentifier
12
+
13
+ def _run(self, identifier: str) -> int:
14
+ return self.kfinance_client.ticker(identifier).trading_item_id
@@ -0,0 +1,53 @@
1
+ from typing import Any, Type
2
+
3
+ from langchain_core.tools import BaseTool
4
+ from pydantic import BaseModel, ConfigDict, Field
5
+
6
+ from kfinance.kfinance import Client
7
+
8
+
9
+ class KfinanceTool(BaseTool):
10
+ """KfinanceTool is a langchain base tool with a kfinance Client.
11
+
12
+ The kfinance_client attribute allows us to make kfinance calls without needing
13
+ the client to get passed in as a param during invocations.
14
+ """
15
+
16
+ kfinance_client: Client
17
+ args_schema: Type[BaseModel]
18
+
19
+ model_config = ConfigDict(populate_by_name=True, extra="forbid")
20
+
21
+ def run_without_langchain(self, *args: Any, **kwargs: Any) -> Any:
22
+ """Execute a Kfinance tool without langchain.
23
+
24
+ Langchain converts json input params into the pydantic args_schema, which means that
25
+ strings get turned into enums, dates, or datetimes where necessary.
26
+ When executing a tool without langchain, we have to handle this
27
+ conversion ourselves.
28
+ """
29
+ args_model = self.args_schema.model_validate(kwargs)
30
+ args_dict = args_model.model_dump()
31
+ # Only pass params included in the LLM generated kwargs.
32
+ # This means that we don't use defaults defined by the pydantic models and instead use
33
+ # the defaults defined in the `_run` function.
34
+ # This behavior matches the langchain handling. See
35
+ # https://github.com/langchain-ai/langchain/blob/ca39680d2ab0d786bc035930778a5787e7bb5e01/libs/core/langchain_core/tools/base.py#L595-L597
36
+ args_dict = {k: v for k, v in args_dict.items() if k in kwargs}
37
+ return self._run(**args_dict)
38
+
39
+ def _run(self, *args: Any, **kwargs: Any) -> Any:
40
+ """The code to execute the tool"""
41
+ ...
42
+
43
+
44
+ class ToolArgsWithIdentifier(BaseModel):
45
+ """Tool argument with an identifier.
46
+
47
+ All tools using an identifier should subclass this model to ensure that the description
48
+ of identifier is always the same.
49
+ """
50
+
51
+ identifier: str = Field(
52
+ description="The identifier, which can be a ticker symbol, ISIN, or CUSIP"
53
+ )
kfinance/version.py CHANGED
@@ -17,5 +17,5 @@ __version__: str
17
17
  __version_tuple__: VERSION_TUPLE
18
18
  version_tuple: VERSION_TUPLE
19
19
 
20
- __version__ = version = '1.2.2'
21
- __version_tuple__ = version_tuple = (1, 2, 2)
20
+ __version__ = version = '2.0.1'
21
+ __version_tuple__ = version_tuple = (2, 0, 1)
@@ -1,23 +0,0 @@
1
- kensho_kfinance-1.2.2.dist-info/licenses/AUTHORS.md,sha256=0h9ClbI0pu1oKj1M28ROUsaxrbZg-6ukQGl6X4y9noI,68
2
- kensho_kfinance-1.2.2.dist-info/licenses/LICENSE,sha256=bsY4blvSgq6o0FMQ3RXa2NCgco--nHCCchLXzxr6kms,83
3
- kfinance/CHANGELOG.md,sha256=2s4rnko1N6MDMK38Bn0OUvhHSygSGb2XGQgM1kKEUtc,580
4
- kfinance/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- kfinance/batch_request_handling.py,sha256=s0uXRY4CC-GnShI0i8PLPgLddRdqLyQ8jnqDKdmaZzs,5531
6
- kfinance/constants.py,sha256=uopijPmpAU97l9OA_mllG2UvGHP9-zeq0UqkA7Yrk-w,47553
7
- kfinance/fetch.py,sha256=XrkOC_5RsiI_ycRiaBbhrs1ND-gqBOknWmfIOpDSLRI,20114
8
- kfinance/kfinance.py,sha256=PGJzfq_AdX7IZsY4BpNuni90R6SGa6nrNLicJi_QEec,45643
9
- kfinance/llm_tools.py,sha256=t3i-5y34AzeFujjwOJVWY7OFI5aOAWG8rt-9KgR_0-E,30683
10
- kfinance/meta_classes.py,sha256=ryncusGDe48gG31a-Ldx1ApJ-ZPRFJvjC18jry7TOVE,15980
11
- kfinance/prompt.py,sha256=PtVB8c_FcSlVdyGgByAnIFGzuUuBaEjciCqnBJl1hSQ,25133
12
- kfinance/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- kfinance/server_thread.py,sha256=jUnt1YGoYDkqqz1MbCwd44zJs1T_Z2BCgvj75bdtLgA,2574
14
- kfinance/tool_schemas.py,sha256=JrFSbb6i2lOQaI24QqnhGhZ3e-pWLhjNqZloY9WGlnM,5643
15
- kfinance/version.py,sha256=o0zEAUXpMoJZCULVYplCdjcZuIQ-3sIIZKyfNu_loQE,511
16
- kfinance/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
- kfinance/tests/test_batch_requests.py,sha256=NsldoZqjjQpv0QKM2th80F1nru2tIF4LJeXU-RamQvA,9836
18
- kfinance/tests/test_fetch.py,sha256=6au-TrsLbLoyvgd1vZSMlxOtXrau8ptM4tcS7vPXIzA,12088
19
- kfinance/tests/test_objects.py,sha256=egXkhfoK2MepZdXtrBxzWxWni7f-zVCefUbnyDnWDOE,22554
20
- kensho_kfinance-1.2.2.dist-info/METADATA,sha256=1OzzFZCWvGJEYcKJGURgJjgZ_awy66sdEKqxSpfpuGE,2935
21
- kensho_kfinance-1.2.2.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
22
- kensho_kfinance-1.2.2.dist-info/top_level.txt,sha256=kT_kNwVhfQoOAecY8W7uYah5xaHMoHoAdBIvXh6DaKM,9
23
- kensho_kfinance-1.2.2.dist-info/RECORD,,