Skip to content

Commit

Permalink
partners: add lint docstrings for azure-dynamic-sessions/together mod…
Browse files Browse the repository at this point in the history
…ules (langchain-ai#23303)

Description: add lint docstrings for azure-dynamic-sessions/together
modules
Issue: langchain-ai#23188 @baskaryan

test: ruff check passed.
<img width="782" alt="image"
src="https://github.com/langchain-ai/langchain/assets/76683249/bf11783d-65b3-4e56-a563-255eae89a3e4">

---------

Co-authored-by: gongwn1 <[email protected]>
  • Loading branch information
2 people authored and asafgardin committed Jun 25, 2024
1 parent e2d3768 commit 7ae0fbc
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 497 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This package provides tools for managing dynamic sessions in Azure."""

from langchain_azure_dynamic_sessions.tools.sessions import SessionsPythonREPLTool

__all__ = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This package provides tools for managing dynamic sessions in Azure."""

from langchain_azure_dynamic_sessions.tools.sessions import SessionsPythonREPLTool

__all__ = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""This is the Azure Dynamic Sessions module.
This module provides the SessionsPythonREPLTool class for
managing dynamic sessions in Azure.
"""

import importlib.metadata
import json
import os
Expand Down Expand Up @@ -27,7 +33,6 @@ def _access_token_provider_factory() -> Callable[[], Optional[str]]:
Returns:
Callable[[], Optional[str]]: The access token provider function
"""

access_token: Optional[AccessToken] = None

def access_token_provider() -> Optional[str]:
Expand All @@ -44,6 +49,7 @@ def access_token_provider() -> Optional[str]:

def _sanitize_input(query: str) -> str:
"""Sanitize input to the python REPL.
Remove whitespace, backtick & python (if llm mistakes python console as terminal)
Args:
Expand All @@ -52,7 +58,6 @@ def _sanitize_input(query: str) -> str:
Returns:
str: The sanitized query
"""

# Removes `, whitespace & python from start
query = re.sub(r"^(\s|`)*(?i:python)?\s*", "", query)
# Removes whitespace & ` from end
Expand Down Expand Up @@ -86,11 +91,11 @@ def from_dict(data: dict) -> "RemoteFileMetadata":


class SessionsPythonREPLTool(BaseTool):
"""A tool for running Python code in an Azure Container Apps dynamic sessions
code interpreter.
"""A tool for running Python code.
Example:
Run python code in an Azure Container Apps dynamic sessions code interpreter.
Example:
.. code-block:: python
from langchain_azure_dynamic_sessions import SessionsPythonREPLTool
Expand Down Expand Up @@ -135,7 +140,6 @@ def _build_url(self, path: str) -> str:

def execute(self, python_code: str) -> Any:
"""Execute Python code in the session."""

if self.sanitize_input:
python_code = _sanitize_input(python_code)

Expand Down Expand Up @@ -197,7 +201,6 @@ def upload_file(
Returns:
RemoteFileMetadata: The metadata for the uploaded file
"""

if data and local_file_path:
raise ValueError("data and local_file_path cannot be provided together")

Expand Down
634 changes: 320 additions & 314 deletions libs/partners/azure-dynamic-sessions/poetry.lock

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion libs/partners/azure-dynamic-sessions/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,21 @@ ipykernel = "^6.29.4"
langchain-openai = { path = "../openai", develop = true }
langchainhub = "^0.1.15"

[tool.ruff]
[tool.ruff.lint]
select = [
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"D", # pydocstyle

]

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["D"] # ignore docstring checks for tests

[tool.mypy]
disallow_untyped_defs = "True"

Expand Down
2 changes: 2 additions & 0 deletions libs/partners/azure-dynamic-sessions/scripts/check_imports.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This module checks for specific import statements in the codebase."""

import sys
import traceback
from importlib.machinery import SourceFileLoader
Expand Down
2 changes: 2 additions & 0 deletions libs/partners/together/langchain_together/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This package provides the Together integration for LangChain."""

from langchain_together.chat_models import ChatTogether
from langchain_together.embeddings import TogetherEmbeddings
from langchain_together.llms import Together
Expand Down
10 changes: 10 additions & 0 deletions libs/partners/together/langchain_together/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ class ChatTogether(BaseChatOpenAI):

@property
def lc_secrets(self) -> Dict[str, str]:
"""A map of constructor argument names to secret ids.
For example,
{"together_api_key": "TOGETHER_API_KEY"}
"""
return {"together_api_key": "TOGETHER_API_KEY"}

@classmethod
def get_lc_namespace(cls) -> List[str]:
"""Get the namespace of the langchain object."""
return ["langchain", "chat_models", "together"]

@property
def lc_attributes(self) -> Dict[str, Any]:
"""List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor.
"""
attributes: Dict[str, Any] = {}

if self.together_api_base:
Expand Down
3 changes: 2 additions & 1 deletion libs/partners/together/langchain_together/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class TogetherEmbeddings(BaseModel, Embeddings):
http_client as well if you'd like a custom client for sync invocations."""

class Config:
"""Configuration for this pydantic object."""

extra = Extra.forbid
allow_population_by_field_name = True

Expand Down Expand Up @@ -143,7 +145,6 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""

together_api_key = get_from_dict_or_env(
values, "together_api_key", "TOGETHER_API_KEY"
)
Expand Down
16 changes: 13 additions & 3 deletions libs/partners/together/langchain_together/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def validate_environment(cls, values: Dict) -> Dict:

@root_validator()
def validate_max_tokens(cls, values: Dict) -> Dict:
"""
The v1 completions endpoint, has max_tokens as required parameter.
"""The v1 completions endpoint, has max_tokens as required parameter.
Set a default value and warn if the parameter is missing.
"""
if values.get("max_tokens") is None:
Expand All @@ -108,6 +108,11 @@ def _format_output(self, output: dict) -> str:

@property
def default_params(self) -> Dict[str, Any]:
"""Return the default parameters for the Together model.
Returns:
A dictionary containing the default parameters.
"""
return {
"model": self.model,
"temperature": self.temperature,
Expand All @@ -128,11 +133,13 @@ def _call(
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
run_manager: The CallbackManager for LLM run, it's not used at the moment.
**kwargs: Additional parameters to pass to the model.
Returns:
The string generated by the model..
"""

headers = {
"Authorization": f"Bearer {self.together_api_key.get_secret_value()}",
"Content-Type": "application/json",
Expand Down Expand Up @@ -176,6 +183,9 @@ async def _acall(
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
run_manager: The CallbackManager for LLM run, it's not used at the moment.
**kwargs: Additional parameters to pass to the model.
Returns:
The string generated by the model.
Expand Down
Loading

0 comments on commit 7ae0fbc

Please sign in to comment.