Skip to content

Commit

Permalink
Merge branch 'master' into google-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan committed Feb 22, 2024
2 parents b6f504e + b0cfb86 commit 3e49825
Show file tree
Hide file tree
Showing 22 changed files with 802 additions and 172 deletions.
98 changes: 66 additions & 32 deletions docs/docs/integrations/llms/tongyi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"Tongyi Qwen is a large-scale language model developed by Alibaba's Damo Academy. It is capable of understanding user intent through natural language understanding and semantic analysis, based on user input in natural language. It provides services and assistance to users in different domains and tasks. By providing clear and detailed instructions, you can obtain results that better align with your expectations."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting up"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -25,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2023-07-10T19:55:38.553933Z",
Expand All @@ -34,10 +41,10 @@
},
"outputs": [
{
"name": "stdout",
"name": "stdin",
"output_type": "stream",
"text": [
"········\n"
" ········\n"
]
}
],
Expand All @@ -50,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2023-07-10T19:55:38.554152Z",
Expand All @@ -66,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2023-07-10T19:55:39.812664Z",
Expand All @@ -75,14 +82,57 @@
},
"outputs": [],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain_community.llms import Tongyi"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Justin Bieber was born on March 1, 1994. The Super Bowl that took place in the same year was Super Bowl XXVIII, which was played on January 30, 1994. The winner of that Super Bowl was the Dallas Cowboys, who defeated the Buffalo Bills with a score of 30-13.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Tongyi().invoke(\"What NFL team won the Super Bowl in the year Justin Bieber was born?\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using in a chain"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import PromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"llm = Tongyi()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2023-07-10T19:55:39.817327Z",
Expand All @@ -100,50 +150,34 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"llm = Tongyi()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
"chain = prompt | llm"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"The year Justin Bieber was born was 1994. The Denver Broncos won the Super Bowl in 1997, which means they would have been the team that won the Super Bowl during Justin Bieber's birth year. So the answer is the Denver Broncos.\""
"'Justin Bieber was born on March 1, 1994. The Super Bowl that took place in the same calendar year was Super Bowl XXVIII, which was played on January 30, 1994. The winner of Super Bowl XXVIII was the Dallas Cowboys, who defeated the Buffalo Bills with a score of 30-13.'"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"question = \"What NFL team won the Super Bowl in the year Justin Bieber was born?\"\n",
"\n",
"llm_chain.run(question)"
"chain.invoke({\"question\": question})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -162,9 +196,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
18 changes: 12 additions & 6 deletions docs/docs/integrations/platforms/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ LangChain integrates with many providers.

These providers have standalone `langchain-{provider}` packages for improved versioning, dependency management and testing.

- [OpenAI](/docs/integrations/platforms/openai)
- [AI21](/docs/integrations/providers/ai21)
- [Anthropic](/docs/integrations/platforms/anthropic)
- [Google](/docs/integrations/platforms/google)
- [MistralAI](/docs/integrations/providers/mistralai)
- [NVIDIA AI](/docs/integrations/providers/nvidia)
- [Together AI](/docs/integrations/providers/together)
- [Robocorp](/docs/integrations/providers/robocorp)
- [Astra DB](/docs/integrations/providers/astradb)
- [Exa Search](/docs/integrations/providers/exa_search)
- [Google Generative AI](/docs/integrations/platforms/google)
- [Google Vertex AI](/docs/integrations/platforms/google)
- [IBM](/docs/integrations/providers/ibm)
- [MistralAI](/docs/integrations/providers/mistralai)
- [Nomic](/docs/integrations/providers/nomic)
- [Nvidia AI Endpoints](/docs/integrations/providers/nvidia)
- [Nvidia AI](/docs/integrations/providers/nvidia)
- [OpenAI](/docs/integrations/platforms/openai)
- [Pinecone](/docs/integrations/providers/pinecone)
- [Robocorp](/docs/integrations/providers/robocorp)
- [Together AI](/docs/integrations/providers/together)


## Featured Community Providers
Expand Down
6 changes: 3 additions & 3 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const baseDarkCodeBlockTheme = require("prism-react-renderer/themes/vsDark");
const config = {
title: "🦜️🔗 Langchain",
tagline: "LangChain Python Docs",
favicon: "img/favicon.ico",
favicon: "img/brand/favicon.png",
// Set the production url of your site here
url: "https://python.langchain.com",
// Set the /<baseUrl>/ pathname under which your site is served
Expand Down Expand Up @@ -144,9 +144,9 @@ const config = {
},
},
},
image: "img/parrot-chainlink-icon.png",
image: "img/brand/theme-image.png",
navbar: {
title: "🦜️🔗 LangChain",
logo: {src: "img/brand/wordmark.png", srcDark: "img/brand/wordmark-dark.png"},
items: [
{
to: "/docs/get_started/introduction",
Expand Down
Binary file added docs/static/img/brand/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/img/brand/theme-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/img/brand/wordmark-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/img/brand/wordmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NeuralDBVectorStore(VectorStore):
vectorstore = NeuralDBVectorStore(db=db)
"""

def __init__(self, db):
def __init__(self, db: Any) -> None:
self.db = db

db: Any = None #: :meta private:
Expand Down
12 changes: 11 additions & 1 deletion libs/core/langchain_core/language_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Optional,
Sequence,
Set,
Type,
TypeVar,
Union,
)

from typing_extensions import TypeAlias

from langchain_core._api import deprecated
from langchain_core._api import beta, deprecated
from langchain_core.messages import (
AnyMessage,
BaseMessage,
MessageLikeRepresentation,
get_buffer_string,
)
from langchain_core.prompt_values import PromptValue
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import Runnable, RunnableSerializable
from langchain_core.utils import get_pydantic_field_names

Expand Down Expand Up @@ -155,6 +158,13 @@ async def agenerate_prompt(
prompt and additional model provider-specific output.
"""

@beta()
def with_structured_output(
self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:
"""Implement this if there is a way of steering the model to generate responses that match a given schema.""" # noqa: E501
raise NotImplementedError()

@deprecated("0.1.7", alternative="invoke", removal="0.2.0")
@abstractmethod
def predict(
Expand Down
2 changes: 2 additions & 0 deletions libs/core/langchain_core/output_parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
MarkdownListOutputParser,
NumberedListOutputParser,
)
from langchain_core.output_parsers.pydantic import PydanticOutputParser
from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.output_parsers.transform import (
BaseCumulativeTransformOutputParser,
Expand All @@ -45,4 +46,5 @@
"SimpleJsonOutputParser",
"XMLOutputParser",
"JsonOutputParser",
"PydanticOutputParser",
]
8 changes: 5 additions & 3 deletions libs/core/langchain_core/output_parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@

from typing_extensions import get_args

from langchain_core.language_models import LanguageModelOutput
from langchain_core.messages import AnyMessage, BaseMessage
from langchain_core.outputs import ChatGeneration, Generation
from langchain_core.runnables import RunnableConfig, RunnableSerializable
from langchain_core.runnables import Runnable, RunnableConfig, RunnableSerializable
from langchain_core.runnables.config import run_in_executor

if TYPE_CHECKING:
from langchain_core.prompt_values import PromptValue

T = TypeVar("T")
OutputParserLike = Runnable[LanguageModelOutput, T]


class BaseLLMOutputParser(Generic[T], ABC):
Expand Down Expand Up @@ -57,7 +59,7 @@ async def aparse_result(


class BaseGenerationOutputParser(
BaseLLMOutputParser, RunnableSerializable[Union[str, BaseMessage], T]
BaseLLMOutputParser, RunnableSerializable[LanguageModelOutput, T]
):
"""Base class to parse the output of an LLM call."""

Expand Down Expand Up @@ -116,7 +118,7 @@ async def ainvoke(


class BaseOutputParser(
BaseLLMOutputParser, RunnableSerializable[Union[str, BaseMessage], T]
BaseLLMOutputParser, RunnableSerializable[LanguageModelOutput, T]
):
"""Base class to parse the output of an LLM call.
Expand Down
62 changes: 62 additions & 0 deletions libs/core/langchain_core/output_parsers/pydantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
from typing import Any, List, Type

from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.outputs import Generation
from langchain_core.pydantic_v1 import BaseModel, ValidationError


class PydanticOutputParser(JsonOutputParser):
"""Parse an output using a pydantic model."""

pydantic_object: Type[BaseModel]
"""The pydantic model to parse.
Attention: To avoid potential compatibility issues, it's recommended to use
pydantic <2 or leverage the v1 namespace in pydantic >= 2.
"""

def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
json_object = super().parse_result(result)
try:
return self.pydantic_object.parse_obj(json_object)
except ValidationError as e:
name = self.pydantic_object.__name__
msg = f"Failed to parse {name} from completion {json_object}. Got: {e}"
raise OutputParserException(msg, llm_output=json_object)

def get_format_instructions(self) -> str:
# Copy schema to avoid altering original Pydantic schema.
schema = {k: v for k, v in self.pydantic_object.schema().items()}

# Remove extraneous fields.
reduced_schema = schema
if "title" in reduced_schema:
del reduced_schema["title"]
if "type" in reduced_schema:
del reduced_schema["type"]
# Ensure json in context is well-formed with double quotes.
schema_str = json.dumps(reduced_schema)

return _PYDANTIC_FORMAT_INSTRUCTIONS.format(schema=schema_str)

@property
def _type(self) -> str:
return "pydantic"

@property
def OutputType(self) -> Type[BaseModel]:
"""Return the pydantic model."""
return self.pydantic_object


_PYDANTIC_FORMAT_INSTRUCTIONS = """The output should be formatted as a JSON instance that conforms to the JSON schema below.
As an example, for the schema {{"properties": {{"foo": {{"title": "Foo", "description": "a list of strings", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}
the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
Here is the output schema:
```
{schema}
```""" # noqa: E501
Loading

0 comments on commit 3e49825

Please sign in to comment.