mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 04:07:54 +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
|
||||
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"]
|
||||
|
||||
@property
|
||||
@ -327,7 +327,7 @@ class ChatFireworks(BaseChatModel):
|
||||
)
|
||||
"""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(
|
||||
@ -338,8 +338,8 @@ class ChatFireworks(BaseChatModel):
|
||||
request_timeout: Union[float, tuple[float, float], Any, None] = Field(
|
||||
default=None, alias="timeout"
|
||||
)
|
||||
"""Timeout for requests to Fireworks completion API. Can be float, httpx.Timeout or
|
||||
None."""
|
||||
"""Timeout for requests to Fireworks completion API. Can be ``float``,
|
||||
``httpx.Timeout`` or ``None``."""
|
||||
streaming: bool = False
|
||||
"""Whether to stream the results or not."""
|
||||
n: int = 1
|
||||
@ -636,8 +636,8 @@ class ChatFireworks(BaseChatModel):
|
||||
|
||||
Assumes model is compatible with Fireworks function-calling API.
|
||||
|
||||
NOTE: Using bind_tools is recommended instead, as the `functions` and
|
||||
`function_call` request parameters are officially marked as deprecated by
|
||||
NOTE: Using bind_tools is recommended instead, as the ``functions`` and
|
||||
``function_call`` request parameters are officially marked as deprecated by
|
||||
Fireworks.
|
||||
|
||||
Args:
|
||||
@ -699,10 +699,10 @@ class ChatFireworks(BaseChatModel):
|
||||
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
|
||||
tool_choice: Which tool to require the model to call.
|
||||
Must be the name of the single provided function,
|
||||
"auto" to automatically determine which function to call
|
||||
with the option to not call any function, "any" to enforce that some
|
||||
``'auto'`` to automatically determine which function to call
|
||||
with the option to not call any function, ``'any'`` to enforce that some
|
||||
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
|
||||
:meth:`~langchain_fireworks.chat_models.ChatFireworks.bind`
|
||||
"""
|
||||
@ -760,11 +760,11 @@ class ChatFireworks(BaseChatModel):
|
||||
|
||||
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>`_.
|
||||
- "json_schema":
|
||||
- ``'json_schema'``:
|
||||
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>`_.
|
||||
|
||||
.. versionchanged:: 0.2.8
|
||||
@ -793,6 +793,7 @@ class ChatFireworks(BaseChatModel):
|
||||
- ``"parsing_error"``: Optional[BaseException]
|
||||
|
||||
Example: schema=Pydantic class, method="function_calling", include_raw=False:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from typing import Optional
|
||||
@ -826,6 +827,7 @@ class ChatFireworks(BaseChatModel):
|
||||
# )
|
||||
|
||||
Example: schema=Pydantic class, method="function_calling", include_raw=True:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_fireworks import ChatFireworks
|
||||
@ -854,6 +856,7 @@ class ChatFireworks(BaseChatModel):
|
||||
# }
|
||||
|
||||
Example: schema=TypedDict class, method="function_calling", include_raw=False:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# 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:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_fireworks import ChatFireworks
|
||||
@ -897,9 +901,9 @@ class ChatFireworks(BaseChatModel):
|
||||
'answer': {'type': 'string'},
|
||||
'justification': {'description': 'A justification for the answer.', 'type': 'string'}
|
||||
},
|
||||
'required': ['answer']
|
||||
}
|
||||
}
|
||||
'required': ['answer']
|
||||
}
|
||||
}
|
||||
|
||||
llm = ChatFireworks(model="accounts/fireworks/models/firefunction-v1", temperature=0)
|
||||
structured_llm = llm.with_structured_output(oai_schema)
|
||||
@ -913,6 +917,7 @@ class ChatFireworks(BaseChatModel):
|
||||
# }
|
||||
|
||||
Example: schema=Pydantic class, method="json_mode", include_raw=True:
|
||||
|
||||
.. code-block::
|
||||
|
||||
from langchain_fireworks import ChatFireworks
|
||||
@ -941,6 +946,7 @@ class ChatFireworks(BaseChatModel):
|
||||
# }
|
||||
|
||||
Example: schema=None, method="json_mode", include_raw=True:
|
||||
|
||||
.. code-block::
|
||||
|
||||
structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)
|
||||
|
@ -10,60 +10,63 @@ from typing_extensions import Self
|
||||
class FireworksEmbeddings(BaseModel, Embeddings):
|
||||
"""Fireworks embedding model integration.
|
||||
|
||||
Setup:
|
||||
Install ``langchain_fireworks`` and set environment variable
|
||||
``FIREWORKS_API_KEY``.
|
||||
Setup:
|
||||
|
||||
.. code-block:: bash
|
||||
Install ``langchain_fireworks`` and set environment variable
|
||||
``FIREWORKS_API_KEY``.
|
||||
|
||||
pip install -U langchain_fireworks
|
||||
export FIREWORKS_API_KEY="your-api-key"
|
||||
.. code-block:: bash
|
||||
|
||||
Key init args — completion params:
|
||||
model: str
|
||||
Name of Fireworks model to use.
|
||||
pip install -U langchain_fireworks
|
||||
export FIREWORKS_API_KEY="your-api-key"
|
||||
|
||||
Key init args — completion params:
|
||||
model: str
|
||||
Name of Fireworks model to use.
|
||||
|
||||
Key init args — client params:
|
||||
fireworks_api_key: SecretStr
|
||||
Fireworks API key.
|
||||
|
||||
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:
|
||||
.. code-block:: python
|
||||
Instantiate:
|
||||
|
||||
from langchain_fireworks import FireworksEmbeddings
|
||||
.. code-block:: python
|
||||
|
||||
model = FireworksEmbeddings(
|
||||
model='nomic-ai/nomic-embed-text-v1.5'
|
||||
# Use FIREWORKS_API_KEY env var or pass it in directly
|
||||
# fireworks_api_key="..."
|
||||
)
|
||||
from langchain_fireworks import FireworksEmbeddings
|
||||
|
||||
Embed multiple texts:
|
||||
.. code-block:: python
|
||||
model = FireworksEmbeddings(
|
||||
model='nomic-ai/nomic-embed-text-v1.5'
|
||||
# Use FIREWORKS_API_KEY env var or pass it in directly
|
||||
# fireworks_api_key="..."
|
||||
)
|
||||
|
||||
vectors = embeddings.embed_documents(['hello', 'goodbye'])
|
||||
# Showing only the first 3 coordinates
|
||||
print(len(vectors))
|
||||
print(vectors[0][:3])
|
||||
Embed multiple texts:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
2
|
||||
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
||||
vectors = embeddings.embed_documents(['hello', 'goodbye'])
|
||||
# Showing only the first 3 coordinates
|
||||
print(len(vectors))
|
||||
print(vectors[0][:3])
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
Embed single text:
|
||||
.. code-block:: python
|
||||
2
|
||||
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
||||
|
||||
input_text = "The meaning of life is 42"
|
||||
vector = embeddings.embed_query('hello')
|
||||
print(vector[:3])
|
||||
Embed single text:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: python
|
||||
|
||||
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
||||
input_text = "The meaning of life is 42"
|
||||
vector = embeddings.embed_query('hello')
|
||||
print(vector[:3])
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
|
||||
"""
|
||||
|
||||
client: OpenAI = Field(default=None, exclude=True) # type: ignore[assignment] # :meta private:
|
||||
@ -76,7 +79,7 @@ class FireworksEmbeddings(BaseModel, Embeddings):
|
||||
)
|
||||
"""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"
|
||||
|
||||
|
@ -4,7 +4,7 @@ import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
import requests
|
||||
from aiohttp import ClientSession
|
||||
from aiohttp import ClientSession, ClientTimeout
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@ -22,15 +22,17 @@ logger = logging.getLogger(__name__)
|
||||
class Fireworks(LLM):
|
||||
"""LLM models from `Fireworks`.
|
||||
|
||||
To use, you'll need an API key which you can find here:
|
||||
https://fireworks.ai This can be passed in as init param
|
||||
``fireworks_api_key`` or set as environment variable ``FIREWORKS_API_KEY``.
|
||||
To use, you'll need an `API key <https://fireworks.ai>`__. This can be passed in as
|
||||
init param ``fireworks_api_key`` or set as environment variable
|
||||
``FIREWORKS_API_KEY``.
|
||||
|
||||
Fireworks AI API reference: https://readme.fireworks.ai/
|
||||
`Fireworks AI API reference <https://readme.fireworks.ai/>`__
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
response = fireworks.generate(["Tell me a joke."])
|
||||
|
||||
"""
|
||||
|
||||
base_url: str = "https://api.fireworks.ai/inference/v1/completions"
|
||||
@ -48,40 +50,40 @@ class Fireworks(LLM):
|
||||
)
|
||||
"""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 name. Available models listed here:
|
||||
https://readme.fireworks.ai/
|
||||
"""
|
||||
"""Model name. `(Available models) <https://readme.fireworks.ai/>`__"""
|
||||
temperature: Optional[float] = None
|
||||
"""Model temperature."""
|
||||
top_p: Optional[float] = None
|
||||
"""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
|
||||
output. A temperature less than 1 favors more correctness and is appropriate
|
||||
for question answering or summarization. A value greater than 1 introduces more
|
||||
randomness in the output.
|
||||
"""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 output.
|
||||
A temperature less than ``1`` favors more correctness and is appropriate for
|
||||
question answering or summarization. A value greater than ``1`` introduces more
|
||||
randomness in the output.
|
||||
"""
|
||||
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
|
||||
"""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
|
||||
probability of occurrence. This technique helps to speed up the generation
|
||||
process and can improve the quality of the generated text by focusing on the
|
||||
most likely options.
|
||||
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 process
|
||||
and can improve the quality of the generated text by focusing on the most likely
|
||||
options.
|
||||
"""
|
||||
max_tokens: Optional[int] = None
|
||||
"""The maximum number of tokens to generate."""
|
||||
repetition_penalty: Optional[float] = None
|
||||
"""A number that controls the diversity of generated text by reducing the
|
||||
likelihood of repeated sequences. Higher values decrease repetition.
|
||||
"""A number that controls the diversity of generated text by reducing the likelihood
|
||||
of repeated sequences. Higher values decrease repetition.
|
||||
"""
|
||||
logprobs: Optional[int] = None
|
||||
"""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(
|
||||
extra="forbid",
|
||||
@ -132,7 +134,7 @@ class Fireworks(LLM):
|
||||
prompt: The prompt to pass into the model.
|
||||
|
||||
Returns:
|
||||
The string generated by the model..
|
||||
The string generated by the model.
|
||||
"""
|
||||
headers = {
|
||||
"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
|
||||
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:
|
||||
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}
|
||||
async with ClientSession() as session:
|
||||
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:
|
||||
if response.status >= 500:
|
||||
raise Exception(f"Fireworks Server: Error {response.status}")
|
||||
|
@ -52,7 +52,7 @@ disallow_untyped_defs = "True"
|
||||
target-version = "py39"
|
||||
|
||||
[tool.ruff.lint]
|
||||
select = ["E", "F", "I", "T201", "UP"]
|
||||
select = ["E", "F", "I", "T201", "UP", "S"]
|
||||
ignore = [ "UP007", ]
|
||||
|
||||
[tool.coverage.run]
|
||||
@ -65,3 +65,9 @@ markers = [
|
||||
"compile: mark placeholder test used to compile integration tests without running them",
|
||||
]
|
||||
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