langchain-b12 0.1.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.
@@ -0,0 +1,105 @@
1
+ # These are some examples of commonly ignored file patterns.
2
+ # You should customize this list as applicable to your project.
3
+ # Learn more about .gitignore:
4
+ # https://www.atlassian.com/git/tutorials/saving-changes/gitignore
5
+
6
+ pyrightconfig.json
7
+ /data
8
+ /outputs
9
+
10
+ # google
11
+ service_account.json
12
+
13
+ # folder for experimental scripts you don't want to commit
14
+ dev/
15
+
16
+ # postgres
17
+ *.sql
18
+ initdb
19
+ postgres_data
20
+
21
+ # minio
22
+ minio_data
23
+
24
+ # langchain
25
+ .langgraph_api/
26
+
27
+ # coverage
28
+ coverage.xml
29
+ .coverage
30
+
31
+ *.egg-info
32
+ *.env
33
+ *.bin
34
+ *.sqlite3
35
+ .vscode/
36
+
37
+ # Node artifact files
38
+ node_modules/
39
+ dist/
40
+
41
+ # Compiled Java class files
42
+ *.class
43
+
44
+ # Compiled Python bytecode
45
+ *.py[cod]
46
+
47
+ # Log files
48
+ *.log
49
+ logs
50
+ npm-debug.log*
51
+ yarn-debug.log*
52
+ yarn-error.log*
53
+ pnpm-debug.log*
54
+ lerna-debug.log*
55
+
56
+ # Package files
57
+ *.jar
58
+
59
+ # Maven
60
+ target/
61
+ dist/
62
+
63
+ # JetBrains IDE
64
+ .idea/
65
+
66
+ # Unit test reports
67
+ TEST*.xml
68
+
69
+ # Generated by MacOS
70
+ .DS_Store
71
+
72
+ # Generated by Windows
73
+ Thumbs.db
74
+
75
+ # Applications
76
+ *.app
77
+ *.exe
78
+ *.war
79
+
80
+ # Large media files
81
+ *.mp4
82
+ *.tiff
83
+ *.avi
84
+ *.flv
85
+ *.mov
86
+ *.wmv
87
+
88
+ notebooks/.ipynb_checkpoints/
89
+
90
+ # Editor directories and files
91
+ .vscode/*
92
+ !.vscode/extensions.json
93
+ *.suo
94
+ *.ntvs*
95
+ *.njsproj
96
+ *.sln
97
+ *.sw?
98
+
99
+ # LangGraph API
100
+ .next/
101
+ next-env.d.ts
102
+
103
+ # Python
104
+ __pycache__/
105
+ *.local
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,22 @@
1
+ {
2
+ // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3
+ // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4
+
5
+ // List of extensions which should be recommended for users of this workspace.
6
+ "recommendations": [
7
+ "charliermarsh.ruff",
8
+ "github.copilot",
9
+ "github.copilot-chat",
10
+ "ms-python.python",
11
+ "ms-python.vscode-pylance",
12
+ "ms-vscode-remote.remote-containers",
13
+ "ms-vscode-remote.remote-ssh",
14
+ "ms-vscode-remote.remote-ssh-edit",
15
+ "ms-vscode.remote-explorer",
16
+ "sonarsource.sonarlint-vscode",
17
+ ],
18
+ // List of extensions recommended by VS Code that should not be recommended for users of this workspace.
19
+ "unwantedRecommendations": [
20
+
21
+ ]
22
+ }
@@ -0,0 +1,5 @@
1
+ install:
2
+ uv sync --frozen --all-groups
3
+
4
+ test:
5
+ uv run pytest
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: langchain-b12
3
+ Version: 0.1.0
4
+ Summary: A reusable collection of tools and implementations for Langchain
5
+ Author-email: Vincent Min <vincent.min@b12-consulting.com>
6
+ Requires-Python: >=3.11
7
+ Requires-Dist: langchain-core>=0.3.60
8
+ Description-Content-Type: text/markdown
9
+
10
+ # Langchain B12
11
+
12
+ This repo hosts a collection of custom LangChain components.
13
+
14
+ - ChatGenAI converts a sequence of LangChain messages to Google GenAI Content objects and vice versa.
15
+
16
+ ## Installation
17
+
18
+ To install this package, run
19
+ ```bash
20
+ pip install langchain-b12
21
+ ```
22
+
23
+ Some components rely on additional packages that may be installed as extras.
24
+ For example, to use the Google chatmodel `ChatGenAI`, you can run
25
+ ```bash
26
+ pip install langchain-b12[google]
27
+ ```
28
+
29
+ ## Components
30
+
31
+ The repo contains these components:
32
+
33
+ - `ChatGenAI`: An implementation of `BaseChatModel` that uses the `google-genai` package. Note that `langchain-google-genai` and `langchain-google-vertexai` exist, but neither uses the latest and recommended `google-genai` package.
34
+ - `GenAIEmbeddings`: An implementation of `Embeddings` that uses the `google-genai` package.
35
+
36
+ ## Comments
37
+
38
+ This repo exists for easy reuse and extension of custom LangChain components.
39
+ When appropriate, we will create PRs to merge these components directly into LangChain.
@@ -0,0 +1,30 @@
1
+ # Langchain B12
2
+
3
+ This repo hosts a collection of custom LangChain components.
4
+
5
+ - ChatGenAI converts a sequence of LangChain messages to Google GenAI Content objects and vice versa.
6
+
7
+ ## Installation
8
+
9
+ To install this package, run
10
+ ```bash
11
+ pip install langchain-b12
12
+ ```
13
+
14
+ Some components rely on additional packages that may be installed as extras.
15
+ For example, to use the Google chatmodel `ChatGenAI`, you can run
16
+ ```bash
17
+ pip install langchain-b12[google]
18
+ ```
19
+
20
+ ## Components
21
+
22
+ The repo contains these components:
23
+
24
+ - `ChatGenAI`: An implementation of `BaseChatModel` that uses the `google-genai` package. Note that `langchain-google-genai` and `langchain-google-vertexai` exist, but neither uses the latest and recommended `google-genai` package.
25
+ - `GenAIEmbeddings`: An implementation of `Embeddings` that uses the `google-genai` package.
26
+
27
+ ## Comments
28
+
29
+ This repo exists for easy reuse and extension of custom LangChain components.
30
+ When appropriate, we will create PRs to merge these components directly into LangChain.
@@ -0,0 +1,61 @@
1
+ [project]
2
+ name = "langchain-b12"
3
+ version = "0.1.0"
4
+ description = "A reusable collection of tools and implementations for Langchain"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Vincent Min", email = "vincent.min@b12-consulting.com" }
8
+ ]
9
+ requires-python = ">=3.11"
10
+ dependencies = [
11
+ "langchain-core>=0.3.60",
12
+ ]
13
+
14
+ [dependency-groups]
15
+ google = [
16
+ "google-genai>=1.16.1",
17
+ ]
18
+ dev = [
19
+ "pytest>=8.3.5",
20
+ ]
21
+
22
+ [build-system]
23
+ requires = ["hatchling"]
24
+ build-backend = "hatchling.build"
25
+
26
+ [tool.ruff]
27
+ line-length = 88
28
+ target-version = "py312"
29
+ exclude = ["__pycache__", "**/scripts/*", "**/notebooks/*"]
30
+ show-fixes = true
31
+
32
+ [tool.ruff.format]
33
+ quote-style = "double"
34
+ indent-style = "space"
35
+ line-ending = "auto"
36
+
37
+ [tool.ruff.lint]
38
+ select = [
39
+ "F", # Pyflakes
40
+ "W", # pycodestyle
41
+ "E", # pycodestyle
42
+ "I", # isort
43
+ "UP", # pyupgrade
44
+ "PL", # Pylint
45
+ "T201", # Print Statement
46
+ ]
47
+ ignore = [
48
+ # Ignore complexity
49
+ "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915",
50
+ "EM101",
51
+ "PLR2004",
52
+ # Ignore f literals in exceptions
53
+ "EM102",
54
+ ]
55
+
56
+ # Add Pyright configuration
57
+ [tool.pyright]
58
+ reportUnknownParameterType = false
59
+ reportUnknownMemberType = false
60
+ reportUnknownArgumentType = false
61
+
File without changes
@@ -0,0 +1,99 @@
1
+ import os
2
+
3
+ from google.genai import Client
4
+ from google.oauth2 import service_account
5
+ from langchain_core.embeddings import Embeddings
6
+ from pydantic import BaseModel, ConfigDict, Field
7
+
8
+
9
+ class GenAIEmbeddings(Embeddings, BaseModel):
10
+ """Embeddings implementation using `google-genai`."""
11
+
12
+ model_name: str = Field(default="text-multilingual-embedding-002")
13
+ client: Client = Field(
14
+ default_factory=lambda: Client(
15
+ credentials=service_account.Credentials.from_service_account_file(
16
+ filename=os.getenv("GOOGLE_APPLICATION_CREDENTIALS"),
17
+ scopes=["https://www.googleapis.com/auth/cloud-platform"],
18
+ )
19
+ ),
20
+ exclude=True,
21
+ )
22
+ model_config = ConfigDict(
23
+ arbitrary_types_allowed=True,
24
+ )
25
+
26
+ def embed_documents(self, texts: list[str]) -> list[list[float]]:
27
+ """Embed a list of text strings using the Google GenAI API.
28
+
29
+ Args:
30
+ texts (list[str]): The text strings to embed.
31
+
32
+ Returns:
33
+ list[list[float]]: The embedding vectors.
34
+ """
35
+ embeddings = []
36
+ for text in texts:
37
+ response = self.client.models.embed_content(
38
+ model=self.model_name,
39
+ contents=[text],
40
+ )
41
+ assert (
42
+ response.embeddings is not None
43
+ ), "No embeddings found in the response."
44
+ for embedding in response.embeddings:
45
+ assert (
46
+ embedding.values is not None
47
+ ), "No embedding values found in the response."
48
+ embeddings.append(embedding.values)
49
+ assert len(embeddings) == len(
50
+ texts
51
+ ), "The number of embeddings does not match the number of texts."
52
+ return embeddings
53
+
54
+ def embed_query(self, text: str) -> list[float]:
55
+ """Embed a text string using the Google GenAI API.
56
+
57
+ Args:
58
+ text (str): The text to embed.
59
+
60
+ Returns:
61
+ list[float]: The embedding vector.
62
+ """
63
+ return self.embed_documents([text])[0]
64
+
65
+ async def aembed_documents(self, texts: list[str]) -> list[list[float]]:
66
+ """Embed a list of text strings using the Google GenAI API asynchronously.
67
+
68
+ Args:
69
+ texts (list[str]): The text strings to embed.
70
+
71
+ Returns:
72
+ list[list[float]]: The embedding vectors.
73
+ """
74
+ embeddings = []
75
+ response = await self.client.aio.models.embed_content(
76
+ model=self.model_name,
77
+ contents=texts,
78
+ )
79
+ assert response.embeddings is not None, "No embeddings found in the response."
80
+ for embedding in response.embeddings:
81
+ assert (
82
+ embedding.values is not None
83
+ ), "No embedding values found in the response."
84
+ embeddings.append(embedding.values)
85
+ assert len(embeddings) == len(
86
+ texts
87
+ ), "The number of embeddings does not match the number of texts."
88
+ return embeddings
89
+
90
+ async def aembed_query(self, text: str) -> list[float]:
91
+ """Embed a text string using the Google GenAI API asynchronously.
92
+
93
+ Args:
94
+ text (str): The text to embed.
95
+
96
+ Returns:
97
+ list[float]: The embedding vector.
98
+ """
99
+ return (await self.aembed_documents([text]))[0]