mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 12:48:12 +00:00
langchain-fireworks[patch]: Add ruff bandit rules to linter (#31796)
- Add ruff bandit rules - Address a s113 error
This commit is contained in:
parent
625f7c3710
commit
479b6fd7c5
@ -288,7 +288,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_lc_namespace(cls) -> list[str]:
|
def get_lc_namespace(cls) -> list[str]:
|
||||||
"""Get the namespace of the langchain object."""
|
"""Get the namespace of the LangChain object."""
|
||||||
return ["langchain", "chat_models", "fireworks"]
|
return ["langchain", "chat_models", "fireworks"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -327,7 +327,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
)
|
)
|
||||||
"""Fireworks API key.
|
"""Fireworks API key.
|
||||||
|
|
||||||
Automatically read from env variable `FIREWORKS_API_KEY` if not provided.
|
Automatically read from env variable ``FIREWORKS_API_KEY`` if not provided.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
fireworks_api_base: Optional[str] = Field(
|
fireworks_api_base: Optional[str] = Field(
|
||||||
@ -338,8 +338,8 @@ class ChatFireworks(BaseChatModel):
|
|||||||
request_timeout: Union[float, tuple[float, float], Any, None] = Field(
|
request_timeout: Union[float, tuple[float, float], Any, None] = Field(
|
||||||
default=None, alias="timeout"
|
default=None, alias="timeout"
|
||||||
)
|
)
|
||||||
"""Timeout for requests to Fireworks completion API. Can be float, httpx.Timeout or
|
"""Timeout for requests to Fireworks completion API. Can be ``float``,
|
||||||
None."""
|
``httpx.Timeout`` or ``None``."""
|
||||||
streaming: bool = False
|
streaming: bool = False
|
||||||
"""Whether to stream the results or not."""
|
"""Whether to stream the results or not."""
|
||||||
n: int = 1
|
n: int = 1
|
||||||
@ -636,8 +636,8 @@ class ChatFireworks(BaseChatModel):
|
|||||||
|
|
||||||
Assumes model is compatible with Fireworks function-calling API.
|
Assumes model is compatible with Fireworks function-calling API.
|
||||||
|
|
||||||
NOTE: Using bind_tools is recommended instead, as the `functions` and
|
NOTE: Using bind_tools is recommended instead, as the ``functions`` and
|
||||||
`function_call` request parameters are officially marked as deprecated by
|
``function_call`` request parameters are officially marked as deprecated by
|
||||||
Fireworks.
|
Fireworks.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -699,10 +699,10 @@ class ChatFireworks(BaseChatModel):
|
|||||||
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
|
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
|
||||||
tool_choice: Which tool to require the model to call.
|
tool_choice: Which tool to require the model to call.
|
||||||
Must be the name of the single provided function,
|
Must be the name of the single provided function,
|
||||||
"auto" to automatically determine which function to call
|
``'auto'`` to automatically determine which function to call
|
||||||
with the option to not call any function, "any" to enforce that some
|
with the option to not call any function, ``'any'`` to enforce that some
|
||||||
function is called, or a dict of the form:
|
function is called, or a dict of the form:
|
||||||
{"type": "function", "function": {"name": <<tool_name>>}}.
|
``{"type": "function", "function": {"name": <<tool_name>>}}``.
|
||||||
**kwargs: Any additional parameters to pass to
|
**kwargs: Any additional parameters to pass to
|
||||||
:meth:`~langchain_fireworks.chat_models.ChatFireworks.bind`
|
:meth:`~langchain_fireworks.chat_models.ChatFireworks.bind`
|
||||||
"""
|
"""
|
||||||
@ -760,11 +760,11 @@ class ChatFireworks(BaseChatModel):
|
|||||||
|
|
||||||
method: The method for steering model generation, one of:
|
method: The method for steering model generation, one of:
|
||||||
|
|
||||||
- "function_calling":
|
- ``'function_calling'``:
|
||||||
Uses Fireworks's `tool-calling features <https://docs.fireworks.ai/guides/function-calling>`_.
|
Uses Fireworks's `tool-calling features <https://docs.fireworks.ai/guides/function-calling>`_.
|
||||||
- "json_schema":
|
- ``'json_schema'``:
|
||||||
Uses Fireworks's `structured output feature <https://docs.fireworks.ai/structured-responses/structured-response-formatting>`_.
|
Uses Fireworks's `structured output feature <https://docs.fireworks.ai/structured-responses/structured-response-formatting>`_.
|
||||||
- "json_mode":
|
- ``'json_mode'``:
|
||||||
Uses Fireworks's `JSON mode feature <https://docs.fireworks.ai/structured-responses/structured-response-formatting>`_.
|
Uses Fireworks's `JSON mode feature <https://docs.fireworks.ai/structured-responses/structured-response-formatting>`_.
|
||||||
|
|
||||||
.. versionchanged:: 0.2.8
|
.. versionchanged:: 0.2.8
|
||||||
@ -793,6 +793,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
- ``"parsing_error"``: Optional[BaseException]
|
- ``"parsing_error"``: Optional[BaseException]
|
||||||
|
|
||||||
Example: schema=Pydantic class, method="function_calling", include_raw=False:
|
Example: schema=Pydantic class, method="function_calling", include_raw=False:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
@ -826,6 +827,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
# )
|
# )
|
||||||
|
|
||||||
Example: schema=Pydantic class, method="function_calling", include_raw=True:
|
Example: schema=Pydantic class, method="function_calling", include_raw=True:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from langchain_fireworks import ChatFireworks
|
from langchain_fireworks import ChatFireworks
|
||||||
@ -854,6 +856,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
Example: schema=TypedDict class, method="function_calling", include_raw=False:
|
Example: schema=TypedDict class, method="function_calling", include_raw=False:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# IMPORTANT: If you are using Python <=3.8, you need to import Annotated
|
# IMPORTANT: If you are using Python <=3.8, you need to import Annotated
|
||||||
@ -884,6 +887,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
Example: schema=OpenAI function schema, method="function_calling", include_raw=False:
|
Example: schema=OpenAI function schema, method="function_calling", include_raw=False:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from langchain_fireworks import ChatFireworks
|
from langchain_fireworks import ChatFireworks
|
||||||
@ -913,6 +917,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
Example: schema=Pydantic class, method="json_mode", include_raw=True:
|
Example: schema=Pydantic class, method="json_mode", include_raw=True:
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
from langchain_fireworks import ChatFireworks
|
from langchain_fireworks import ChatFireworks
|
||||||
@ -941,6 +946,7 @@ class ChatFireworks(BaseChatModel):
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
Example: schema=None, method="json_mode", include_raw=True:
|
Example: schema=None, method="json_mode", include_raw=True:
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)
|
structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)
|
||||||
|
@ -11,6 +11,7 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
|||||||
"""Fireworks embedding model integration.
|
"""Fireworks embedding model integration.
|
||||||
|
|
||||||
Setup:
|
Setup:
|
||||||
|
|
||||||
Install ``langchain_fireworks`` and set environment variable
|
Install ``langchain_fireworks`` and set environment variable
|
||||||
``FIREWORKS_API_KEY``.
|
``FIREWORKS_API_KEY``.
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
|||||||
See full list of supported init args and their descriptions in the params section.
|
See full list of supported init args and their descriptions in the params section.
|
||||||
|
|
||||||
Instantiate:
|
Instantiate:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from langchain_fireworks import FireworksEmbeddings
|
from langchain_fireworks import FireworksEmbeddings
|
||||||
@ -41,6 +43,7 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
|||||||
)
|
)
|
||||||
|
|
||||||
Embed multiple texts:
|
Embed multiple texts:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
vectors = embeddings.embed_documents(['hello', 'goodbye'])
|
vectors = embeddings.embed_documents(['hello', 'goodbye'])
|
||||||
@ -53,8 +56,8 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
|||||||
2
|
2
|
||||||
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
||||||
|
|
||||||
|
|
||||||
Embed single text:
|
Embed single text:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
input_text = "The meaning of life is 42"
|
input_text = "The meaning of life is 42"
|
||||||
@ -76,7 +79,7 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
|||||||
)
|
)
|
||||||
"""Fireworks API key.
|
"""Fireworks API key.
|
||||||
|
|
||||||
Automatically read from env variable `FIREWORKS_API_KEY` if not provided.
|
Automatically read from env variable ``FIREWORKS_API_KEY`` if not provided.
|
||||||
"""
|
"""
|
||||||
model: str = "nomic-ai/nomic-embed-text-v1.5"
|
model: str = "nomic-ai/nomic-embed-text-v1.5"
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import logging
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession, ClientTimeout
|
||||||
from langchain_core.callbacks import (
|
from langchain_core.callbacks import (
|
||||||
AsyncCallbackManagerForLLMRun,
|
AsyncCallbackManagerForLLMRun,
|
||||||
CallbackManagerForLLMRun,
|
CallbackManagerForLLMRun,
|
||||||
@ -22,15 +22,17 @@ logger = logging.getLogger(__name__)
|
|||||||
class Fireworks(LLM):
|
class Fireworks(LLM):
|
||||||
"""LLM models from `Fireworks`.
|
"""LLM models from `Fireworks`.
|
||||||
|
|
||||||
To use, you'll need an API key which you can find here:
|
To use, you'll need an `API key <https://fireworks.ai>`__. This can be passed in as
|
||||||
https://fireworks.ai This can be passed in as init param
|
init param ``fireworks_api_key`` or set as environment variable
|
||||||
``fireworks_api_key`` or set as environment variable ``FIREWORKS_API_KEY``.
|
``FIREWORKS_API_KEY``.
|
||||||
|
|
||||||
Fireworks AI API reference: https://readme.fireworks.ai/
|
`Fireworks AI API reference <https://readme.fireworks.ai/>`__
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
response = fireworks.generate(["Tell me a joke."])
|
response = fireworks.generate(["Tell me a joke."])
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
base_url: str = "https://api.fireworks.ai/inference/v1/completions"
|
base_url: str = "https://api.fireworks.ai/inference/v1/completions"
|
||||||
@ -48,40 +50,40 @@ class Fireworks(LLM):
|
|||||||
)
|
)
|
||||||
"""Fireworks API key.
|
"""Fireworks API key.
|
||||||
|
|
||||||
Automatically read from env variable `FIREWORKS_API_KEY` if not provided.
|
Automatically read from env variable ``FIREWORKS_API_KEY`` if not provided.
|
||||||
"""
|
"""
|
||||||
model: str
|
model: str
|
||||||
"""Model name. Available models listed here:
|
"""Model name. `(Available models) <https://readme.fireworks.ai/>`__"""
|
||||||
https://readme.fireworks.ai/
|
|
||||||
"""
|
|
||||||
temperature: Optional[float] = None
|
temperature: Optional[float] = None
|
||||||
"""Model temperature."""
|
"""Model temperature."""
|
||||||
top_p: Optional[float] = None
|
top_p: Optional[float] = None
|
||||||
"""Used to dynamically adjust the number of choices for each predicted token based
|
"""Used to dynamically adjust the number of choices for each predicted token based
|
||||||
on the cumulative probabilities. A value of 1 will always yield the same
|
on the cumulative probabilities. A value of ``1`` will always yield the same output.
|
||||||
output. A temperature less than 1 favors more correctness and is appropriate
|
A temperature less than ``1`` favors more correctness and is appropriate for
|
||||||
for question answering or summarization. A value greater than 1 introduces more
|
question answering or summarization. A value greater than ``1`` introduces more
|
||||||
randomness in the output.
|
randomness in the output.
|
||||||
"""
|
"""
|
||||||
model_kwargs: dict[str, Any] = Field(default_factory=dict)
|
model_kwargs: dict[str, Any] = Field(default_factory=dict)
|
||||||
"""Holds any model parameters valid for `create` call not explicitly specified."""
|
"""Holds any model parameters valid for ``create`` call not explicitly specified."""
|
||||||
top_k: Optional[int] = None
|
top_k: Optional[int] = None
|
||||||
"""Used to limit the number of choices for the next predicted word or token. It
|
"""Used to limit the number of choices for the next predicted word or token. It
|
||||||
specifies the maximum number of tokens to consider at each step, based on their
|
specifies the maximum number of tokens to consider at each step, based on their
|
||||||
probability of occurrence. This technique helps to speed up the generation
|
probability of occurrence. This technique helps to speed up the generation process
|
||||||
process and can improve the quality of the generated text by focusing on the
|
and can improve the quality of the generated text by focusing on the most likely
|
||||||
most likely options.
|
options.
|
||||||
"""
|
"""
|
||||||
max_tokens: Optional[int] = None
|
max_tokens: Optional[int] = None
|
||||||
"""The maximum number of tokens to generate."""
|
"""The maximum number of tokens to generate."""
|
||||||
repetition_penalty: Optional[float] = None
|
repetition_penalty: Optional[float] = None
|
||||||
"""A number that controls the diversity of generated text by reducing the
|
"""A number that controls the diversity of generated text by reducing the likelihood
|
||||||
likelihood of repeated sequences. Higher values decrease repetition.
|
of repeated sequences. Higher values decrease repetition.
|
||||||
"""
|
"""
|
||||||
logprobs: Optional[int] = None
|
logprobs: Optional[int] = None
|
||||||
"""An integer that specifies how many top token log probabilities are included in
|
"""An integer that specifies how many top token log probabilities are included in
|
||||||
the response for each token generation step.
|
the response for each token generation step.
|
||||||
"""
|
"""
|
||||||
|
timeout: Optional[int] = 30
|
||||||
|
"""Timeout in seconds for requests to the Fireworks API."""
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
extra="forbid",
|
extra="forbid",
|
||||||
@ -132,7 +134,7 @@ class Fireworks(LLM):
|
|||||||
prompt: The prompt to pass into the model.
|
prompt: The prompt to pass into the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The string generated by the model..
|
The string generated by the model.
|
||||||
"""
|
"""
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.fireworks_api_key.get_secret_value()}",
|
"Authorization": f"Bearer {self.fireworks_api_key.get_secret_value()}",
|
||||||
@ -148,7 +150,9 @@ class Fireworks(LLM):
|
|||||||
|
|
||||||
# filter None values to not pass them to the http payload
|
# filter None values to not pass them to the http payload
|
||||||
payload = {k: v for k, v in payload.items() if v is not None}
|
payload = {k: v for k, v in payload.items() if v is not None}
|
||||||
response = requests.post(url=self.base_url, json=payload, headers=headers)
|
response = requests.post(
|
||||||
|
url=self.base_url, json=payload, headers=headers, timeout=self.timeout
|
||||||
|
)
|
||||||
|
|
||||||
if response.status_code >= 500:
|
if response.status_code >= 500:
|
||||||
raise Exception(f"Fireworks Server: Error {response.status_code}")
|
raise Exception(f"Fireworks Server: Error {response.status_code}")
|
||||||
@ -196,7 +200,10 @@ class Fireworks(LLM):
|
|||||||
payload = {k: v for k, v in payload.items() if v is not None}
|
payload = {k: v for k, v in payload.items() if v is not None}
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with session.post(
|
async with session.post(
|
||||||
self.base_url, json=payload, headers=headers
|
self.base_url,
|
||||||
|
json=payload,
|
||||||
|
headers=headers,
|
||||||
|
timeout=ClientTimeout(total=self.timeout),
|
||||||
) as response:
|
) as response:
|
||||||
if response.status >= 500:
|
if response.status >= 500:
|
||||||
raise Exception(f"Fireworks Server: Error {response.status}")
|
raise Exception(f"Fireworks Server: Error {response.status}")
|
||||||
|
@ -52,7 +52,7 @@ disallow_untyped_defs = "True"
|
|||||||
target-version = "py39"
|
target-version = "py39"
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
select = ["E", "F", "I", "T201", "UP"]
|
select = ["E", "F", "I", "T201", "UP", "S"]
|
||||||
ignore = [ "UP007", ]
|
ignore = [ "UP007", ]
|
||||||
|
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
@ -65,3 +65,9 @@ markers = [
|
|||||||
"compile: mark placeholder test used to compile integration tests without running them",
|
"compile: mark placeholder test used to compile integration tests without running them",
|
||||||
]
|
]
|
||||||
asyncio_mode = "auto"
|
asyncio_mode = "auto"
|
||||||
|
|
||||||
|
[tool.ruff.lint.extend-per-file-ignores]
|
||||||
|
"tests/**/*.py" = [
|
||||||
|
"S101", # Tests need assertions
|
||||||
|
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||||
|
]
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user