Skip to content

Commit

Permalink
migrate examples to pydantic 2 (#745)
Browse files Browse the repository at this point in the history
Migrate examples to pydantic 2
  • Loading branch information
eyurtsev authored Sep 9, 2024
1 parent 5aedbf7 commit b528955
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/agent/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
from langchain.agents import AgentExecutor
from langchain.agents.format_scratchpad import format_to_openai_functions
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
from langchain.pydantic_v1 import BaseModel
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain_core.utils.function_calling import format_tool_to_openai_function
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from pydantic import BaseModel

from langserve import add_routes

Expand Down
2 changes: 1 addition & 1 deletion examples/agent_custom_streaming/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
from langchain_core.tools import tool
from langchain_core.utils.function_calling import format_tool_to_openai_tool
from langchain_openai import ChatOpenAI
from pydantic import BaseModel

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel

prompt = ChatPromptTemplate.from_messages(
[
Expand Down
2 changes: 1 addition & 1 deletion examples/agent_with_history/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
from langchain_core.tools import tool
from langchain_core.utils.function_calling import format_tool_to_openai_tool
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

prompt = ChatPromptTemplate.from_messages(
[
Expand Down
9 changes: 4 additions & 5 deletions examples/auth/api_handler/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
)
from langchain_core.vectorstores import VectorStore
from langchain_openai import OpenAIEmbeddings
from pydantic import BaseModel, ConfigDict
from typing_extensions import Annotated

from langserve import APIHandler
from langserve.pydantic_v1 import BaseModel


class User(BaseModel):
Expand Down Expand Up @@ -150,10 +150,9 @@ class PerUserVectorstore(RunnableSerializable):
user_id: Optional[str]
vectorstore: VectorStore

class Config:
# Allow arbitrary types since VectorStore is an abstract interface
# and not a pydantic model
arbitrary_types_allowed = True
model_config = ConfigDict(
arbitrary_types_allowed=True,
)

def _invoke(
self, input: str, config: Optional[RunnableConfig] = None, **kwargs: Any
Expand Down
9 changes: 4 additions & 5 deletions examples/auth/per_req_config_modifier/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
)
from langchain_core.vectorstores import VectorStore
from langchain_openai import OpenAIEmbeddings
from pydantic import BaseModel, ConfigDict
from typing_extensions import Annotated

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel


class User(BaseModel):
Expand Down Expand Up @@ -147,10 +147,9 @@ class PerUserVectorstore(RunnableSerializable):
user_id: Optional[str]
vectorstore: VectorStore

class Config:
# Allow arbitrary types since VectorStore is an abstract interface
# and not a pydantic model
arbitrary_types_allowed = True
model_config = ConfigDict(
arbitrary_types_allowed=True,
)

def _invoke(
self, input: str, config: Optional[RunnableConfig] = None, **kwargs: Any
Expand Down
2 changes: 1 addition & 1 deletion examples/chat_playground/legacy_input/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

app = FastAPI(
title="LangChain Server",
Expand Down
2 changes: 1 addition & 1 deletion examples/chat_playground/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from langchain_anthropic.chat_models import ChatAnthropic
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

app = FastAPI(
title="LangChain Server",
Expand Down
2 changes: 1 addition & 1 deletion examples/chat_with_persistence/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field


def _is_valid_identifier(value: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion examples/configurable_agent_executor/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from langchain.agents import AgentExecutor
from langchain.agents.format_scratchpad import format_to_openai_functions
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
from langchain.pydantic_v1 import BaseModel
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import (
Expand All @@ -33,6 +32,7 @@
from langchain_core.tools import tool
from langchain_core.utils.function_calling import format_tool_to_openai_function
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from pydantic import BaseModel

from langserve import add_routes

Expand Down
2 changes: 1 addition & 1 deletion examples/configurable_retrieval/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
)
from langchain_core.vectorstores import VectorStore
from langchain_openai import OpenAIEmbeddings
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

vectorstore1 = FAISS.from_texts(
["cats like fish", "dogs like sticks"], embedding=OpenAIEmbeddings()
Expand Down
2 changes: 1 addition & 1 deletion examples/conversational_retrieval_chain/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, format_document
from langchain_core.runnables import RunnableMap, RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

_TEMPLATE = """Given the following conversation and a follow up question, rephrase the
follow up question to be a standalone question, in its original language.
Expand Down
2 changes: 1 addition & 1 deletion examples/file_processing/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import base64

from fastapi import FastAPI
from langchain.pydantic_v1 import Field
from langchain_community.document_loaders.parsers.pdf import PDFMinerParser
from langchain_core.document_loaders import Blob
from langchain_core.runnables import RunnableLambda
from pydantic import Field

from langserve import CustomUserType, add_routes

Expand Down
2 changes: 1 addition & 1 deletion examples/widgets/chat/message_list/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from pydantic import BaseModel, Field

from langserve import add_routes
from langserve.pydantic_v1 import BaseModel, Field

app = FastAPI(
title="LangChain Server",
Expand Down
2 changes: 1 addition & 1 deletion examples/widgets/chat/tuples/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from langchain.pydantic_v1 import BaseModel, Field
from langchain_community.document_loaders.parsers.pdf import PDFMinerParser
from langchain_core.document_loaders import Blob
from langchain_core.messages import (
Expand All @@ -17,6 +16,7 @@
)
from langchain_core.runnables import RunnableLambda, RunnableParallel
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

from langserve import CustomUserType
from langserve.server import add_routes
Expand Down

0 comments on commit b528955

Please sign in to comment.