Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/llm responses #376

Open
wants to merge 167 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
f15be68
Started working on llm_responses
NotBioWaste905 Jul 19, 2024
56b7789
Created class, created 1st tutorial
NotBioWaste Jul 22, 2024
af60115
Added dependecies for langchain
NotBioWaste Jul 22, 2024
b3b79a5
Fixed adding custom prompt for each node
NotBioWaste Jul 22, 2024
6eb910d
Added image processing, updated tutorial
NotBioWaste Jul 22, 2024
1f8cddc
Added typehint
NotBioWaste Jul 22, 2024
74cd954
Added llm_response, LLM_API, history management
NotBioWaste Jul 22, 2024
1fd31a2
Fixed image reading
NotBioWaste Jul 22, 2024
2c48490
Started llm condition
NotBioWaste Jul 24, 2024
a1884e5
Added message_to_langchain
NotBioWaste Jul 24, 2024
61f302e
Implementing deepeval integration
NotBioWaste Jul 29, 2024
38a8f8f
Figured out how to implement DeepEval functions
NotBioWaste905 Jul 30, 2024
592267f
Adding conditions
NotBioWaste Jul 31, 2024
baccc47
Implemented simple conditions call, added BaseMethod class, renaming,…
NotBioWaste Aug 1, 2024
8e84ba1
Fixed history extraction
NotBioWaste Aug 2, 2024
2b2847b
Delete test_bot.py
NotBioWaste905 Aug 2, 2024
7e336ac
Fixed prompt handling, switched to AIMessage in LLM response
NotBioWaste Aug 5, 2024
71babbf
Merge branch 'feat/llm_responses' of https://github.com/deeppavlov/di…
NotBioWaste Aug 5, 2024
351ae06
Fixed conditions call
NotBioWaste Aug 5, 2024
e3d0d15
Working on autotesting
NotBioWaste Aug 5, 2024
0405998
Added tests
NotBioWaste Aug 7, 2024
3dbfd0c
Removed unused method
NotBioWaste Aug 7, 2024
5c876ba
Added annotations
NotBioWaste Aug 7, 2024
8f1932c
Added structured output support, tweaked tests
NotBioWaste Aug 7, 2024
aedf47e
Reworking tutorials
NotBioWaste Aug 7, 2024
adadb05
Reworked prompt usage and hierarchy, reworked filters and methods
NotBioWaste Aug 12, 2024
0288896
No idea how to make script smaller in tutorials
NotBioWaste Aug 12, 2024
67e2758
Small fixes in tutorials and structured generation
NotBioWaste Aug 13, 2024
428a9f0
Working on user guide
NotBioWaste Aug 14, 2024
5e26b4b
Fixed some tutorials, finished user guide
NotBioWaste Aug 14, 2024
5dbb6cd
Bugfixes in docs
NotBioWaste Aug 14, 2024
db63d1a
Lint
NotBioWaste Aug 14, 2024
2b9080f
Removed type annotation that broke docs building
NotBioWaste Aug 14, 2024
2bcda71
Tests and bugfixes
NotBioWaste Aug 15, 2024
d2f28ed
Deleted DeepEval references
NotBioWaste Aug 15, 2024
7318c91
Numpy versions trouble
NotBioWaste Aug 15, 2024
27eae27
Fixed dependecies
NotBioWaste Aug 16, 2024
3fed1fc
Made everything asynchronous
NotBioWaste Aug 16, 2024
30862ca
Added and unified docstring
NotBioWaste Aug 16, 2024
06ab5bc
Added 4th tutorial, fixed message_schema parameter passing
NotBioWaste Aug 16, 2024
798a77b
Bugfix, added max_size to the message_to_langchain function
NotBioWaste Aug 20, 2024
3343159
Made even more everything asynchronous
NotBioWaste Aug 21, 2024
014ff7e
Remade condition, added logprob check
NotBioWaste Aug 21, 2024
761bd81
Async bugfix, added model_result_to_text, working on message_schema f…
NotBioWaste Aug 22, 2024
90a811e
Minor fixes, tinkering tests
NotBioWaste Aug 23, 2024
5bff191
Merge branch 'refs/heads/dev' into feat/llm_responses
RLKRo Aug 23, 2024
8b88ba6
update lock file
RLKRo Aug 23, 2024
20c4afd
Merge remote-tracking branch 'origin/feat/llm_responses' into feat/ll…
RLKRo Aug 23, 2024
0139421
Merge remote-tracking branch 'origin/master' into feat/llm_responses
NotBioWaste905 Sep 18, 2024
9bb0cba
Updating to v1.0
NotBioWaste905 Sep 23, 2024
f2d6b68
Finished tests, finished update
NotBioWaste905 Sep 26, 2024
6fddaea
lint
NotBioWaste905 Sep 26, 2024
e06bc2b
Started working on llm slots
NotBioWaste905 Sep 26, 2024
22d8efc
Resolving pydantic errors
NotBioWaste905 Sep 27, 2024
aa735b5
Delete llmslot_test.py
NotBioWaste905 Sep 27, 2024
cc91133
Finished LLMSlot, working on LLMGroupSlot
NotBioWaste905 Sep 27, 2024
8756838
Merge remote-tracking branch 'origin/feat/llm_responses' into feat/ll…
NotBioWaste905 Sep 27, 2024
f1857f6
Added flag to
NotBioWaste905 Oct 1, 2024
c334ff5
First test attempts
NotBioWaste905 Oct 1, 2024
8306bbb
linting
NotBioWaste905 Oct 1, 2024
f842776
Merge branch 'feat/slots_extraction_update' into feat/llm_responses
NotBioWaste905 Oct 1, 2024
ada17ca
Merge remote-tracking branch 'origin/feat/llm_responses' into feat/ll…
NotBioWaste905 Oct 1, 2024
a45f653
File structure fixed
NotBioWaste905 Oct 3, 2024
3838d30
Fixed naming
NotBioWaste905 Oct 3, 2024
0e650f8
Create LLMCondition and LLMResponse classes
NotBioWaste905 Oct 3, 2024
015cb4f
Debugging flattening
NotBioWaste905 Oct 23, 2024
b6e5eeb
Bugfix
NotBioWaste905 Oct 23, 2024
b20137e
Added return_type property for LLMSlot
NotBioWaste905 Oct 23, 2024
25f5b04
Changed return_type from Any to type
NotBioWaste905 Oct 23, 2024
b651087
lint
NotBioWaste905 Oct 23, 2024
1b5a77b
removed deprecated from_script from tutorials
NotBioWaste905 Nov 2, 2024
c18d375
Fixed LLMCondition class
NotBioWaste905 Nov 2, 2024
459f7fc
Fixed missing 'models' field in Pipeline, updated tutorials
NotBioWaste905 Nov 6, 2024
24300e8
create __get_llm_response method in LLM_API, refactoring LLM Conditio…
NotBioWaste905 Nov 7, 2024
03b02be
Merge branch 'refs/heads/dev' into feat/llm_responses
RLKRo Nov 7, 2024
e6663b3
update lock file
RLKRo Nov 7, 2024
2e1c190
remove outdated entries from conf.py
RLKRo Nov 7, 2024
859c57a
small fixes to user guide
RLKRo Nov 7, 2024
fb3142b
minor tutorial changes
RLKRo Nov 7, 2024
ff81267
Moved docstring, removed pipeline parameter
NotBioWaste905 Nov 13, 2024
7518259
Fixed type annotation for models field in Pipeline
NotBioWaste905 Nov 13, 2024
ac28d78
removed unused imports from llm/__init__.py
NotBioWaste905 Nov 13, 2024
2d4998c
Fix redundancy in chatsky/slots/llm.py
NotBioWaste905 Nov 13, 2024
23d6a31
Fixed circular LLM_API<=>Pipeline import
NotBioWaste905 Nov 13, 2024
ef9baa3
Merge remote-tracking branch 'origin/feat/llm_responses' into feat/ll…
NotBioWaste905 Nov 13, 2024
4bf5bba
Update import order chatsky/llm/filters.py
NotBioWaste905 Nov 13, 2024
9188b89
Fixes in filters
NotBioWaste905 Nov 14, 2024
02894f0
Fixes of LLM_API annotations and docs
NotBioWaste905 Nov 14, 2024
8e839a1
Removed __get_llm_response, lint
NotBioWaste905 Nov 14, 2024
210b10a
Added context_to_history util, some tweaks in responses
NotBioWaste905 Nov 14, 2024
784f323
remove llm_response object initialization from tutorials
RLKRo Nov 14, 2024
042d256
fix imports in __init__ files:
RLKRo Nov 14, 2024
10533ed
fix: rename llm_response to LLMResponse, rename llm_condition to LLMC…
RLKRo Nov 14, 2024
8f21069
fix codeblocks in user guide
RLKRo Nov 14, 2024
95e2418
fix: message_to_langchain accepts context instead of pipeline
RLKRo Nov 15, 2024
934a0b8
remove defaults from filter definitions
RLKRo Nov 15, 2024
1be58a0
check field not none in filters
RLKRo Nov 15, 2024
4d68a29
remove model_name from LLM_API.respond
RLKRo Nov 15, 2024
fa0ae70
make LLMResponse prompt AnyResponse, remove __prompt_to_message
RLKRo Nov 15, 2024
8778637
fix return style in LLM_API.respond
RLKRo Nov 15, 2024
d4b67a1
fix LLM_API.condition signature
RLKRo Nov 15, 2024
4a29687
some doc fixes
RLKRo Nov 15, 2024
37aafb3
fix message schema json dumping
RLKRo Nov 15, 2024
54a7376
remove unused imports
RLKRo Nov 15, 2024
86da03e
fix circular import
RLKRo Nov 15, 2024
eac43e0
fix tests
RLKRo Nov 15, 2024
51c66a8
remove cnd.true()
RLKRo Nov 15, 2024
33242ca
Fixed empty prompt popping up
NotBioWaste905 Nov 15, 2024
65f7c8f
Format
NotBioWaste905 Nov 15, 2024
dc92132
Switched model from 3.5-turbo to 4o-mini
NotBioWaste905 Nov 15, 2024
020a7ef
Updated all of the models
NotBioWaste905 Nov 15, 2024
c9891f6
Fixes and logging
NotBioWaste905 Nov 15, 2024
c678f89
Codestyle
NotBioWaste905 Nov 15, 2024
f2df441
update lock file
RLKRo Nov 15, 2024
f20d463
simplify history text
RLKRo Nov 15, 2024
44e5571
fix codestyle
RLKRo Nov 15, 2024
9f97ce2
fix doc building
RLKRo Nov 15, 2024
b9e738a
Merge branch 'refs/heads/dev' into feat/llm_responses
RLKRo Nov 15, 2024
39750ba
update lock file
RLKRo Nov 15, 2024
6603f7d
remove unnecessary langchain extras
RLKRo Nov 15, 2024
3827462
update lock file
RLKRo Nov 15, 2024
f7e7684
protect langchain imports & sort imports in modules
RLKRo Nov 15, 2024
a4e0462
skip llm tests on missing langchain
RLKRo Nov 15, 2024
13923ab
Added docstrings in llm/methods.py
NotBioWaste905 Nov 20, 2024
537d8cc
Docstring fixes
NotBioWaste905 Nov 20, 2024
35d9d7d
Fixes in message_to_langchain
NotBioWaste905 Nov 20, 2024
e5c83fb
lint
NotBioWaste905 Nov 20, 2024
5a7313f
Fixed overseen raise condition
NotBioWaste905 Nov 20, 2024
0000414
Signature fixes
NotBioWaste905 Nov 20, 2024
36a9f54
Responses related fixes
NotBioWaste905 Nov 20, 2024
ba95767
Slot related fixes + lint
NotBioWaste905 Nov 20, 2024
3d79cec
Fixed abstract call
NotBioWaste905 Nov 20, 2024
8e22b97
Adding tests
NotBioWaste905 Nov 20, 2024
b8de244
Bunch of documentation fixes, removed attachment_to_content
NotBioWaste905 Nov 25, 2024
bfba582
Added tests, need fix
NotBioWaste905 Nov 25, 2024
2b3c02b
Renamed FromTheModel to FromModel
NotBioWaste905 Nov 25, 2024
47f3855
Changes in BaseFilter class
NotBioWaste905 Nov 25, 2024
248d77f
Switched to localhost models in tutorials
NotBioWaste905 Nov 26, 2024
b5ecc1a
Renamed BaseFilter into BaseHistoryFilter, added API reference
NotBioWaste905 Nov 26, 2024
34e5536
Lint
NotBioWaste905 Nov 26, 2024
60c7c97
Slots and tutorials update
NotBioWaste905 Nov 27, 2024
3cf1df7
Tutorials and structured output update
NotBioWaste905 Nov 28, 2024
7f00028
More clear instructions in tutorial
NotBioWaste905 Nov 28, 2024
513eb19
Fixes in llm slots and tutorial
NotBioWaste905 Nov 28, 2024
2cd5d41
lint
NotBioWaste905 Nov 28, 2024
6a0845d
Finalizing tweaks
NotBioWaste905 Nov 29, 2024
81a86e9
Lint
NotBioWaste905 Nov 29, 2024
24e65c5
Removed import test
NotBioWaste905 Nov 29, 2024
b6af8f5
Removed dotenv, fixed Union
NotBioWaste905 Nov 29, 2024
ee5f643
Conditions cleanup
NotBioWaste905 Dec 4, 2024
1ff7020
Switched to the '|' operator, IsImportant and FromModel are now inher…
NotBioWaste905 Dec 4, 2024
2f65265
Added partial extraction to the tutorial
NotBioWaste905 Dec 4, 2024
04c5b54
Moved history flag annotation to another tutorial
NotBioWaste905 Dec 4, 2024
0d56e75
Fixed docstrings
NotBioWaste905 Dec 4, 2024
74c6d5e
Quickfix for message_to_langchain
NotBioWaste905 Dec 4, 2024
7e2da91
Fixed signatures in filters, lint
NotBioWaste905 Dec 4, 2024
7a313d1
Fixed tutorial link
NotBioWaste905 Dec 4, 2024
9b31ac9
Actually fixed tutorial link
NotBioWaste905 Dec 4, 2024
1c4aa24
Fixed splitted lines in tutorials, reworked system prompt handling af…
NotBioWaste905 Dec 4, 2024
419ab8d
Added missing docstrings for LLM_API
NotBioWaste905 Dec 9, 2024
e723334
Small docstring fix
NotBioWaste905 Dec 9, 2024
6b1ffed
Added test for conditions + fixed some bugs
NotBioWaste905 Dec 11, 2024
2a7bd4f
Removed return_schema from condition due to not using it for now
NotBioWaste905 Dec 12, 2024
e25e2f8
Experiencing issues with slot testing
NotBioWaste905 Dec 12, 2024
8e553bd
lint
NotBioWaste905 Dec 12, 2024
fea185c
Fixes in LLM Slot testing
NotBioWaste905 Dec 12, 2024
968fe75
Refactor context_to_history function to streamline filtering of dialo…
NotBioWaste905 Dec 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added chatsky/llm/__init__.py
NotBioWaste905 marked this conversation as resolved.
Show resolved Hide resolved
RLKRo marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
155 changes: 155 additions & 0 deletions chatsky/llm/llm_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
LLM responses.
---------
Wrapper around langchain.
"""

try:
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_vertexai import ChatVertexAI
from langchain_cohere import ChatCohere
from langchain_mistralai import ChatMistralAI
langchain_available = True
except ImportError:
langchain_available = False

from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.output_parsers import StrOutputParser

import base64
import httpx

from chatsky.script.core.message import Image, Message
from chatsky.script import Context
from chatsky.pipeline import Pipeline

from pydantic import BaseModel
from typing import Union

import re


class LLM_API(BaseModel):
"""
This class acts as a wrapper for all LLMs from langchain
and handles message exchange between remote model and chatsky classes.
"""

def __init__(
self,
model: Union[
ChatOpenAI, ChatAnthropic, ChatVertexAI, ChatCohere, ChatMistralAI
],
system_prompt: str = "",
) -> None:
"""
:param model: Model object.
:param system_prompt: System prompt for the model.
"""
self.__check_imports()
self.model = model
self.name = ""
self.parser = StrOutputParser()
self.system_prompt = system_prompt


def __check_imports(self):
if not langchain_available:
raise ImportError("Langchain is not available. Please install it with `pip install chatsky[llm]`.")


def respond(self, history: list = []) -> Message:
result = self.parser.invoke(self.model.invoke(history))
result = Message(text=result)
result.annotation.__generated_by_model__ = self.name
return result

def condition(self, prompt, request):
result = self.parser.invoke(self.model.invoke([prompt+'\n'+request.text]))
return result


def llm_response(
ctx: Context,
pipeline: Pipeline,
model_name,
prompt="",
history=10,
filter_non_llm=True
):
"""
Basic function for receiving LLM responses.
:param ctx: Context object. (Assigned automatically)
:param pipeline: Pipeline object. (Assigned automatically)
:param model_name: Name of the model from the `Pipeline.models` dictionary.
:param prompt: Prompt for the model.
:param history: Number of messages to keep in history.
:param filter_non_llm: Whether to filter non-LLM messages from the history.
"""
model = pipeline.get(model_name)
history_messages = []
if history == 0:
return model.respond([prompt + "\n" + ctx.last_request.text])
else:
for req, resp in zip(ctx.requests[-history:], ctx.responses[-history:]):
if filter_non_llm and resp.annotation.__generated_by_model__ != model_name:
continue
if req.attachments != []:
content = [{"type": "text", "text": prompt + "\n" + ctx.last_request.text}]
for image in ctx.last_request.attachments:
if image is not Image:
continue
content.append(
{"type": "image_url", "image_url": {"url": __attachment_to_content(image)}}
)
req_message = HumanMessage(content=content)
else:
req_message = HumanMessage(req.text)

history_messages.append(req_message)
history_messages.append(SystemMessage(resp.text))
return model.respond(history_messages)


def llm_condition(
ctx: Context,
pipeline: Pipeline,
model_name,
prompt="",
method="regex",
threshold=0.9
):
"""
Basic function for using LLM in condition cases.
"""
model = pipeline.get(model_name)
if method == "regex":
return re.match(r"True", model.condition(prompt, ctx.last_request))


def __attachment_to_content(attachment: Image) -> str:
"""
Helper function to convert image to base64 string.
"""
if "http" in attachment.source:
image_data = httpx.get(attachment.source).content
else:
with open(attachment.source, "rb") as image_file:
image_data = image_file.read()
image_b64 = base64.b64encode(image_data).decode("utf-8")
extension = attachment.source.split(".")[-1]
image_b64 = f"data:image/{extension};base64,{image_b64}"
return image_b64


def message_to_langchain(message: Message):
if message.attachments != []:
content = [{"type": "text", "text": message.text}]
for image in message.attachments:
if image is not Image:
continue
content.append(
{"type": "image_url", "image_url": {"url": __attachment_to_content(image)}}
)
return HumanMessage(content=content)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ydb = ["ydb", "six"]
telegram = ["python-telegram-bot"]
stats = ["opentelemetry-exporter-otlp", "opentelemetry-instrumentation", "requests", "tqdm", "omegaconf"]
benchmark = ["pympler", "humanize", "pandas", "altair", "tqdm"]

llm = ["httpx", "langchain", "langchain-openai", "langchain-anthropic", "langchain-google-vertexai", "langchain-cohere", "langchain-groq", "langchain-mistralai", "langchain-fireworks"]

[tool.poetry.group.lint]
optional = true
Expand Down
93 changes: 93 additions & 0 deletions tutorials/script/responses/llm_responses/1_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# %% [markdown]
"""
# LLM Responses: 1. Basics

Using Chatsky you can easily add LLM invocations to your script.
In this tutorial we will see how to use LLM responses.
Chatsky uses langchain under the hood to connect to the remote models.
"""

# %pip install chatsky
# %pip install langchain-openai
# or install langchain for the model of your choise

# %%
from chatsky.script import Message
from chatsky.script.conditions import exact_match
from chatsky.script.conditions import std_conditions as cnd
from chatsky.script import RESPONSE, TRANSITIONS
from chatsky.pipeline import Pipeline
from chatsky.utils.testing import (
is_interactive_mode,
run_interactive_mode,
)
from chatsky.script.responses.llm.llm_response import LLMResponse

import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()

from langchain_openai import ChatOpenAI

# %% [markdown]
"""
Now we need to create a model object.
Keep in mind, that if you instantiate model object outside of the script,
it will be reused across all the nodes and therefore it will store all dialogue history.
This is not advised if you are short on tokens or if you do not need to store all dialogue history.
Alternatively you can instantiate model object inside of RESPONSE field in the nodes you need.
"""

# %%
model = LLMResponse(ChatOpenAI(model="gpt-3.5-turbo"), system_prompt="You are an experienced barista in a local coffeshop. Answer your customers questions about coffee and barista work.")

# %% [markdown]
"""
Also you can pass images to the LLM, just pass them as attachments to your message.
"""

# %%
toy_script = {
"greeting_flow": {
"start_node": {
RESPONSE: Message(""),
TRANSITIONS: {"greeting_node": exact_match("Hi")},
},
"greeting_node": {
RESPONSE: model.respond(),
TRANSITIONS: {"main_node": exact_match("i'm fine, how are you?")},
},
"main_node": {
RESPONSE: model.respond(),
TRANSITIONS: {
"latte_art_node": exact_match("Tell me about latte art."),
"image_desc_node": exact_match("Tell me what coffee is it?")},
},
"latte_art_node": {
RESPONSE: model.respond(prompt="PROMPT: pretend that you have never heard about latte art before."),
TRANSITIONS: {"image_desc_node": exact_match("Ok, goodbye.")},
},
"image_desc_node": {
# we expect user to send some images of coffee.
RESPONSE: model.respond(prompt="PROMPT: user will give you some images of coffee. Describe them."),
TRANSITIONS: {"main_node": cnd.true()},
},
"fallback_node": {
RESPONSE: Message("I didn't quite understand you..."),
TRANSITIONS: {"main_node": cnd.true()},
},
}
}

# %%
pipeline = Pipeline.from_script(
toy_script,
start_label=("greeting_flow", "start_node"),
fallback_label=("greeting_flow", "fallback_node"),
)

if __name__ == "__main__":
# This runs tutorial in interactive mode if not in IPython env
# and if `DISABLE_INTERACTIVE_MODE` is not set
if is_interactive_mode():
run_interactive_mode(pipeline) # This runs tutorial in interactive mode
Loading