Skip to content

Commit

Permalink
refactor + add text generation webui internal client
Browse files Browse the repository at this point in the history
  • Loading branch information
psyb0t committed Jul 1, 2024
1 parent abe6d15 commit bd5870c
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ TODO
- prompt - add prompt enhancer
- prompt - add prompt compression using LLMLingua
- llm provider - optionally use NuExtract text to json model to get structured response(like instead of embedding the instruction to the base llm, take the resp and send it to nuextract)
- llm provider - text generation web ui - support multiple instances hosted in diff locations
32 changes: 21 additions & 11 deletions src/ezpyai/_constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
from typing import Dict
LIB_NAME: str = "ezpyai"

_LIB_NAME: str = "ezpyai"
# ENVIRONMENT VARIABLES
ENV_VAR_NAME_OPENAI_API_KEY: str = "OPENAI_API_KEY"
ENV_VAR_NAME_OPENAI_ORGANIZATION: str = "OPENAI_ORGANIZATION"
ENV_VAR_NAME_OPENAI_PROJECT: str = "OPENAI_PROJECT"
ENV_VAR_NAME_TEXT_GENERATION_WEBUI_BASE_URL: str = "TEXT_GENERATION_WEBUI_BASE_URL"
ENV_VAR_NAME_TEXT_GENERATION_WEBUI_API_KEY: str = "TEXT_GENERATION_WEBUI_API_KEY"

_ENV_VAR_NAME_OPENAI_API_KEY: str = "OPENAI_API_KEY"
_ENV_VAR_NAME_OPENAI_ORGANIZATION: str = "OPENAI_ORGANIZATION"
_ENV_VAR_NAME_OPENAI_PROJECT: str = "OPENAI_PROJECT"
_ENV_VAR_NAME_TEXT_GENERATION_WEBUI_API_KEY: str = "TEXT_GENERATION_WEBUI_API_KEY"
_ENV_VAR_NAME_TEXT_GENERATION_WEBUI_BASE_URL: str = "TEXT_GENERATION_WEBUI_BASE_URL"
# DICTIONARY KEYS
DICT_KEY_ID: str = "id"
DICT_KEY_METADATA: str = "metadata"
DICT_KEY_CONTENT: str = "content"
DICT_KEY_SUMMARY: str = "summary"

# HTTP METHODS
HTTP_METHOD_GET: str = "GET"
HTTP_METHOD_POST: str = "POST"

_DICT_KEY_ID: str = "id"
_DICT_KEY_METADATA: str = "metadata"
_DICT_KEY_CONTENT: str = "content"
_DICT_KEY_SUMMARY: str = "summary"
# HTTP CONTENT TYPES
HTTP_CONTENT_TYPE_JSON: str = "application/json"

# HTTP HEADERS
HTTP_HEADER_CONTENT_TYPE: str = "Content-Type"
HTTP_HEADER_AUTHORIZATION: str = "Authorization"
4 changes: 2 additions & 2 deletions src/ezpyai/_logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from ezpyai._constants import _LIB_NAME
from ezpyai._constants import LIB_NAME

logger = logging.getLogger(_LIB_NAME)
logger = logging.getLogger(LIB_NAME)
13 changes: 9 additions & 4 deletions src/ezpyai/llm/knowledge/_knowledge_gatherer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from PyPDF2 import PdfReader
from docx import Document
from ezpyai._logger import logger
from ezpyai._constants import _DICT_KEY_SUMMARY
from ezpyai._constants import DICT_KEY_SUMMARY
from ezpyai.llm.providers._llm_provider import LLMProvider
from ezpyai.llm.prompt import Prompt, get_summarizer_prompt
from ezpyai.llm.knowledge.knowledge_item import KnowledgeItem
Expand Down Expand Up @@ -45,7 +45,12 @@ class KnowledgeGatherer:
"""

def __init__(self, summarizer: LLMProvider = None) -> None:
"""Initialize the KnowledgeGatherer with an empty _items dictionary."""
"""
Initialize the KnowledgeGatherer with an empty _items dictionary.
Args:
summarizer (LLMProvider, optional): The LLMProvider hosting the summarizer model to use for knowledge collection. Defaults to None.
"""

self._items: Dict[str, KnowledgeItem] = {}
self._summarizer: LLMProvider = summarizer
Expand Down Expand Up @@ -114,8 +119,8 @@ def _summarize(self, knowledge_item: KnowledgeItem) -> None:
prompt: Prompt = get_summarizer_prompt(knowledge_item.content)

knowledge_item.summary = self._summarizer.get_structured_response(
prompt, response_format={_DICT_KEY_SUMMARY: ""}
)[_DICT_KEY_SUMMARY]
prompt, response_format={DICT_KEY_SUMMARY: ""}
)[DICT_KEY_SUMMARY]

logger.debug(f"Summarized knowledge item: {knowledge_item}")

Expand Down
8 changes: 4 additions & 4 deletions src/ezpyai/llm/knowledge/chroma_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import Dict, List
from ezpyai._logger import logger
from ezpyai._constants import _DICT_KEY_SUMMARY
from ezpyai._constants import DICT_KEY_SUMMARY
from ezpyai.llm.providers._llm_provider import LLMProvider
from ezpyai.llm.knowledge._knowledge_db import BaseKnowledgeDB
from ezpyai.llm.knowledge._knowledge_gatherer import KnowledgeGatherer
Expand Down Expand Up @@ -88,7 +88,7 @@ def store(
logger.debug(f"Pre-processing item: {knowledge_item}")

metadata = knowledge_item.metadata
metadata[_DICT_KEY_SUMMARY] = knowledge_item.summary
metadata[DICT_KEY_SUMMARY] = knowledge_item.summary

document_ids.append(knowledge_item.id)
documents.append(knowledge_item.content)
Expand Down Expand Up @@ -141,8 +141,8 @@ def search(

for i in range(len(documents)):
summary = ""
if _DICT_KEY_SUMMARY in metadatas[i]:
summary = metadatas[i].pop(_DICT_KEY_SUMMARY, "")
if DICT_KEY_SUMMARY in metadatas[i]:
summary = metadatas[i].pop(DICT_KEY_SUMMARY, "")

knowledge_items.append(
KnowledgeItem(
Expand Down
16 changes: 8 additions & 8 deletions src/ezpyai/llm/knowledge/knowledge_item.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Dict
from ezpyai._constants import (
_DICT_KEY_ID,
_DICT_KEY_METADATA,
_DICT_KEY_CONTENT,
_DICT_KEY_SUMMARY,
DICT_KEY_ID,
DICT_KEY_METADATA,
DICT_KEY_CONTENT,
DICT_KEY_SUMMARY,
)


Expand Down Expand Up @@ -38,8 +38,8 @@ def __str__(self):

def to_dict(self) -> Dict[str, str]:
return {
_DICT_KEY_ID: self.id,
_DICT_KEY_METADATA: self.metadata,
_DICT_KEY_CONTENT: self.content,
_DICT_KEY_SUMMARY: self.summary,
DICT_KEY_ID: self.id,
DICT_KEY_METADATA: self.metadata,
DICT_KEY_CONTENT: self.content,
DICT_KEY_SUMMARY: self.summary,
}
Empty file.
Loading

0 comments on commit bd5870c

Please sign in to comment.