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

Added LangChain_Chat module #650

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ milvus = ["pymilvus[model]"]
bedrock = ["boto3", "botocore"]
weaviate = ["weaviate-client"]
azuresearch = ["azure-search-documents", "azure-identity", "azure-common", "fastembed"]
langchain = ["langchain>=0.3.0"]
21 changes: 19 additions & 2 deletions src/vanna/flask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import importlib.metadata
import json
import logging
import os
import sys
import uuid
from abc import ABC, abstractmethod
from functools import wraps
import importlib.metadata

import flask
import requests
from flasgger import Swagger
from flask import Flask, Response, jsonify, request, send_from_directory
from flask_sock import Sock
from langchain_core.messages import BaseMessage

from ..base import VannaBase
from .assets import css_content, html_content, js_content
Expand Down Expand Up @@ -190,7 +191,23 @@ def __init__(

if self.debug:
def log(message, title="Info"):
[ws.send(json.dumps({'message': message, 'title': title})) for ws in self.ws_clients]
if (
isinstance(message, list)
and len(message) > 0
and isinstance(message[0], BaseMessage)
):
message = [dict(m) for m in message]
[
ws.send(
json.dumps(
{
"message": message,
"title": title,
}
)
)
for ws in self.ws_clients
]

self.vn.log = log

Expand Down
1 change: 1 addition & 0 deletions src/vanna/langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .langchain_chat import LangChain_Chat
69 changes: 69 additions & 0 deletions src/vanna/langchain/langchain_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import List

from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
)

from ..base import VannaBase


class LangChain_Chat(VannaBase):
def __init__(self, chat_model: BaseChatModel, config=None):
VannaBase.__init__(self, config=config)
self.llm = chat_model
self.model_name = (
self.llm.model_name
if hasattr(self.llm, "model_name")
else type(self.llm).__name__
)

def system_message(self, message: str) -> any:
return SystemMessage(message)

def user_message(self, message: str) -> any:
return HumanMessage(message)

def assistant_message(self, message: str) -> any:
return AIMessage(message)

def count_prompt_tokens(
self, input_messages: List[BaseMessage], output_message: AIMessage
) -> int:
# OpenAI
if (
"token_usage" in output_message.response_metadata
and "prompt_tokens" in output_message.response_metadata["token_usage"]
):
return output_message.response_metadata["token_usage"]["prompt_tokens"]
# Anthropic
elif (
"usage" in output_message.response_metadata
and "input_tokens" in output_message.response_metadata["usage"]
):
return output_message.response_metadata["usage"]["input_tokens"]
# Other
else:
num_tokens = 0
for message in input_messages:
num_tokens += len(message.content) / 4
return num_tokens

def submit_prompt(self, prompt: List[BaseMessage], **kwargs) -> str:
if prompt is None:
raise Exception("Prompt is None")

if len(prompt) == 0:
raise Exception("Prompt is empty")

response = self.llm.invoke(prompt)
num_tokens = self.count_prompt_tokens(prompt, response)
self.log(
f"Used model {self.model_name} for {num_tokens} tokens (approx)",
title="Model Used",
)

return response.content