Skip to content

Commit

Permalink
upgrade to langchain-core 0.3 (#317)
Browse files Browse the repository at this point in the history
Upgrade to pydantic >= 2, langchain 0.3
  • Loading branch information
eyurtsev committed Sep 16, 2024
1 parent 55d02ef commit bb7c5f6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 396 deletions.
82 changes: 0 additions & 82 deletions .github/workflows/_pydantic_compatibility.yml

This file was deleted.

8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
Expand All @@ -43,10 +42,3 @@ jobs:
- name: Run unit tests
run: |
poetry run poe test
pydantic-compatibility:
uses:
./.github/workflows/_pydantic_compatibility.yml
with:
working-directory: .
secrets: inherit
12 changes: 6 additions & 6 deletions kor/extraction/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional

from langchain_core.output_parsers import BaseOutputParser
from pydantic import Extra
from pydantic import ConfigDict

from kor.encoders import Encoder
from kor.exceptions import ParseError
Expand All @@ -22,6 +22,7 @@ class KorParser(BaseOutputParser[Extraction]):
encoder: Encoder
schema_: Object
validator: Optional[Validator] = None
model_config = ConfigDict(arbitrary_types_allowed=True)

@property
def _type(self) -> str:
Expand Down Expand Up @@ -66,8 +67,7 @@ def parse(self, text: str) -> Extraction:
"validated_data": validated_data,
}

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

extra = Extra.forbid
arbitrary_types_allowed = True
model_config = ConfigDict(
extra="forbid",
arbitrary_types_allowed=True,
)
25 changes: 9 additions & 16 deletions kor/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_core.prompt_values import PromptValue
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
from pydantic import ConfigDict

from kor.encoders import Encoder
from kor.encoders.encode import InputFormatter, encode_examples, format_text
Expand All @@ -14,12 +15,6 @@
from kor.nodes import Object
from kor.type_descriptors import TypeDescriptor

try:
# Use pydantic v1 namespace since working with langchain
from pydantic.v1 import Extra # type: ignore[assignment]
except ImportError:
from pydantic import Extra # type: ignore[assignment]

from .validators import Validator

DEFAULT_INSTRUCTION_TEMPLATE = PromptTemplate(
Expand All @@ -41,11 +36,10 @@ class ExtractionPromptValue(PromptValue):
string: str
messages: List[BaseMessage]

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

extra = Extra.forbid
arbitrary_types_allowed = True
model_config = ConfigDict(
extra="forbid",
arbitrary_types_allowed=True,
)

def to_string(self) -> str:
"""Format the prompt to a string."""
Expand All @@ -65,11 +59,10 @@ class ExtractionPromptTemplate(BasePromptTemplate):
input_formatter: InputFormatter
instruction_template: PromptTemplate

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

extra = Extra.forbid
arbitrary_types_allowed = True
model_config = ConfigDict(
extra="forbid",
arbitrary_types_allowed=True,
)

def format_prompt( # type: ignore[override]
self,
Expand Down
Loading

0 comments on commit bb7c5f6

Please sign in to comment.