Skip to content

Commit

Permalink
MERGE development INTO master (#80)
Browse files Browse the repository at this point in the history
* MERGE feature/50-adds-settings-and-updates-history-endpoints INTO development (#51)

* Endpoints reconfigurged for settings and history

* Update changelog

* MERGE Feature/rag chat fixed to not be agent INTO development (#53)

* Rag chat fixed

* Remove body from create for now, may change how this works later

* Works for saas, may stop workign on vercel

* MERGE feature/55-update-ollama-rag-chat INTO development (#56)

* Contains updates for ollama rag but is a bit more custom than intended, doesn't include return docs atm.

* Rag working for both openai and ollama

* MERGE feature/55-update-ollama-rag-chat INTO development (#57)

* Contains updates for ollama rag but is a bit more custom than intended, doesn't include return docs atm.

* Rag working for both openai and ollama

* Update changelog

* Update changlog

* fixing the retrieval routes

* Update changelog

* Update retrieval controller with file upload

* Adds better error handling for tools in agent

* Changes made (#62)

* MERGE feature/64-retricts-what-can-be-passed-to-settings-and-history INTO development (#65)

* Updates to restrict the keys that can be saved

* Adds the keys as args so could be altered

* adds some type hinting

* Small fix in history controller

* Need to account for tags and title on history and settings

* Switch to a set to be more efficient (#71)

* A couple adjustments

* Had to update the PINECONE_KEY to PINECONE_API_KEY

* Had to do some updates

* update HistoryController with user_id for webhook

* Update changelog

* MERGE feature/76-updates-params-for-ability-to-fetch-from-other-properties INTO development (#77)

* MERGE development INTO master (#74)

* MERGE feature/50-adds-settings-and-updates-history-endpoints INTO development (#51)

* Endpoints reconfigurged for settings and history

* Update changelog

* MERGE Feature/rag chat fixed to not be agent INTO development (#53)

* Rag chat fixed

* Remove body from create for now, may change how this works later

* Works for saas, may stop workign on vercel

* MERGE feature/55-update-ollama-rag-chat INTO development (#56)

* Contains updates for ollama rag but is a bit more custom than intended, doesn't include return docs atm.

* Rag working for both openai and ollama

* MERGE feature/55-update-ollama-rag-chat INTO development (#57)

* Contains updates for ollama rag but is a bit more custom than intended, doesn't include return docs atm.

* Rag working for both openai and ollama

* Update changelog

* Update changlog

* fixing the retrieval routes

* Update changelog

* Update retrieval controller with file upload

* Adds better error handling for tools in agent

* Changes made (#62)

* MERGE feature/64-retricts-what-can-be-passed-to-settings-and-history INTO development (#65)

* Updates to restrict the keys that can be saved

* Adds the keys as args so could be altered

* adds some type hinting

* Small fix in history controller

* Need to account for tags and title on history and settings

* Switch to a set to be more efficient (#71)

* A couple adjustments

* Had to update the PINECONE_KEY to PINECONE_API_KEY

* Had to do some updates

* update HistoryController with user_id for webhook

* Update changelog

* Made, may need to take a second look edit mode is not working in the other repo

* Updates changelog

* Updates so UserRepo can be passed

* MERGE feature/15-can-pass-tools-and-user-repo-to-include-router INTO development (#79)

* Will close out issue 15, updates saas

* Changelog updated
  • Loading branch information
ryaneggz authored Jan 2, 2024
1 parent f162939 commit 5c2e40b
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.12
### Added
- feature/15-can-pass-tools-and-user-repo-to-include-router (2024-01-01)

## 1.1.11
### Added
- feature/76-updates-params-for-ability-to-fetch-from-other-properties (2023-12-31)
Expand Down
2 changes: 2 additions & 0 deletions promptengineers/core/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
class ValidationException(Exception):
pass

class NotFoundException(Exception):
pass
34 changes: 29 additions & 5 deletions promptengineers/fastapi/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@
from promptengineers.core.utils import logger
from promptengineers.llms.utils import gather_tools
from promptengineers.tools.utils import format_agent_actions
from promptengineers.core.exceptions import NotFoundException

router = APIRouter()
TAG = "Chat"

def get_controller(request: Request) -> ChatController:
return ChatController(request=request)
try:
return ChatController(
request=request,
user_repo=request.state.user_repo,
available_tools=request.state.available_tools
)
except NotFoundException as e:
# Handle specific NotFoundException with a custom message or logging
logger.warn(f"Failed to initialize HistoryController: {str(e)}")
raise HTTPException(status_code=404, detail=f"Initialization failed: {str(e)}") from e
except Exception as e:
# Catch all other exceptions
logger.error(f"Unexpected error initializing HistoryController: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error") from e

#################################################
# ChatGPT
#################################################
@router.post(
"/chat",
tags=[TAG],
name='chat',
response_model=ResponseChat,
responses={
200: {
Expand Down Expand Up @@ -96,6 +111,7 @@ async def chat(
@router.post(
"/chat/agent",
tags=[TAG],
name='chat_agent',
response_model=ResponseAgentChat,
responses={
200: {
Expand Down Expand Up @@ -196,6 +212,7 @@ async def agent(
@router.post(
"/chat/vectorstore",
tags=[TAG],
name='chat_vectorstore',
response_model=ResponseVectorstoreChat,
responses={
200: {
Expand All @@ -221,7 +238,7 @@ async def vector_search(
user_id = getattr(request.state, "user_id", None)

# Retrieve User Tokens
token = chat_controller.user_repo.find_token(user_id, ['OPENAI_API_KEY']).get('OPENAI_API_KEY')
token = chat_controller.user_repo.find_token(user_id, 'OPENAI_API_KEY')

# Generate Embeddings
embeddings = EmbeddingFactory(body.model, token)
Expand All @@ -231,9 +248,10 @@ async def vector_search(
provider=body.provider,
index_name=body.vectorstore,
embeddings=embeddings(),
user_id=user_id,
user_id=chat_controller.user_id,
user_repo=chat_controller.user_repo,
)
vectostore_service = VectorstoreContext(vectorstore_strategy)
vectostore_service = VectorstoreContext(vectorstore_strategy())
vectorstore = vectostore_service.load()

# Check if the retrieved file is empty
Expand All @@ -252,9 +270,15 @@ async def vector_search(
temperature=body.temperature,
vectorstore=vectorstore,
)
formatted_docs = []
for doc in result['source_documents']:
formatted_docs.append({
'page_content': doc.page_content,
'metadata': doc.metadata,
})
data = ujson.dumps({
'message': result['answer'],
'documents': result['source_documents'],
'documents': formatted_docs,
'usage': {
'total_tokens': cb.total_tokens,
'prompt_tokens': cb.prompt_tokens,
Expand Down
19 changes: 17 additions & 2 deletions promptengineers/fastapi/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
import traceback

from fastapi import APIRouter, HTTPException, Response, Depends, Request, status
from promptengineers.core.exceptions import NotFoundException
from promptengineers.core.utils import logger
from promptengineers.fastapi.controllers import HistoryController
from promptengineers.models.request import ReqBodyHistory
from promptengineers.models.response import (ResponseHistoryShow, ResponseHistoryIndex,
ResponseCreate, ResponseUpdate)
from promptengineers.mongo.utils import JSONEncoder
from promptengineers.core.utils import logger

router = APIRouter()
TAG = "Chat"

def get_controller(request: Request) -> HistoryController:
return HistoryController(request=request)
try:
return HistoryController(request=request, user_repo=request.state.user_repo)
except NotFoundException as e:
# Handle specific NotFoundException with a custom message or logging
logger.warn(f"Failed to initialize HistoryController: {str(e)}")
raise HTTPException(status_code=404, detail=f"Initialization failed: {str(e)}") from e
except Exception as e:
# Catch all other exceptions
logger.error(f"Unexpected error initializing HistoryController: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error") from e

#################################################
# List Chat Histories
#################################################
@router.get(
"/chat/history",
tags=[TAG],
name='history_list',
response_model=ResponseHistoryIndex
)
async def list_chat_histories(
Expand Down Expand Up @@ -60,6 +71,7 @@ async def list_chat_histories(
@router.post(
"/chat/history",
tags=[TAG],
name='history_create',
response_model=ResponseCreate
)
async def create_chat_history(
Expand Down Expand Up @@ -95,6 +107,7 @@ async def create_chat_history(
@router.get(
"/chat/history/{history_id}",
tags=[TAG],
name='history_show',
response_model=ResponseHistoryShow,
)
async def show_chat_history(
Expand Down Expand Up @@ -131,6 +144,7 @@ async def show_chat_history(
@router.put(
"/chat/history/{history_id}",
tags=[TAG],
name='history_update',
response_model=ResponseUpdate,
)
async def update_chat_history(
Expand Down Expand Up @@ -163,6 +177,7 @@ async def update_chat_history(
@router.delete(
"/chat/history/{history_id}",
tags=[TAG],
name='history_delete',
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete_chat_history(
Expand Down
21 changes: 18 additions & 3 deletions promptengineers/fastapi/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@
from promptengineers.models.response import ResponsePromptSystemList, ResponsePromptSystem
from promptengineers.mongo.utils import JSONEncoder
from promptengineers.core.utils import logger
from promptengineers.core.exceptions import NotFoundException

router = APIRouter()
TAG = "Prompt"

def get_controller(request: Request) -> PromptController:
return PromptController(request=request)
try:
return PromptController(request=request, user_repo=request.state.user_repo)
except NotFoundException as e:
# Handle specific NotFoundException with a custom message or logging
logger.warn(f"Failed to initialize HistoryController: {str(e)}")
raise HTTPException(status_code=404, detail=f"Initialization failed: {str(e)}") from e
except Exception as e:
# Catch all other exceptions
logger.error(f"Unexpected error initializing HistoryController: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error") from e

#################################################
# List Chat Histories
#################################################
@router.get(
"/prompt/system",
tags=[TAG],
name='prompt_system_list',
response_model=ResponsePromptSystemList
)
async def index(
Expand Down Expand Up @@ -57,6 +68,7 @@ async def index(
@router.post(
"/prompt/system",
tags=[TAG],
name='prompt_system_create',
response_model=ResponsePromptSystem
)
async def create(
Expand Down Expand Up @@ -92,11 +104,12 @@ async def create(
@router.get(
"/prompt/system/{prompt_id}",
tags=[TAG],
name='prompt_system_show',
response_model=ResponsePromptSystem,
)
async def show(
prompt_id: str,
controller: PromptController = Depends(get_controller),
prompt_id: str,
controller: PromptController = Depends(get_controller),
):
"""Retrieve resource"""
try:
Expand Down Expand Up @@ -128,6 +141,7 @@ async def show(
@router.put(
"/prompt/system/{prompt_id}",
tags=[TAG],
name='prompt_system_update',
response_model=ResponsePromptSystem,
)
async def update(
Expand Down Expand Up @@ -160,6 +174,7 @@ async def update(
@router.delete(
"/prompt/system/{prompt_id}",
tags=[TAG],
name='prompt_system_delete',
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete(
Expand Down
22 changes: 20 additions & 2 deletions promptengineers/fastapi/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@
Response, UploadFile, File, HTTPException)

from promptengineers.core.exceptions import ValidationException
from promptengineers.core.interfaces.controllers import IController
from promptengineers.models.request import RequestDataLoader, RequestMultiLoader
from promptengineers.models.response import (ResponseFileLoader, ResponseCreateVectorStore,
ResponseListPineconeVectorStores)
from promptengineers.fastapi.controllers import VectorSearchController, AuthController
from promptengineers.core.utils import logger
from promptengineers.core.exceptions import NotFoundException

TAG = "Retrieval"
router = APIRouter()
auth_controller = AuthController()

def get_controller(request: Request) -> VectorSearchController:
return VectorSearchController(request=request)
def get_controller(request: Request) -> IController:
try:
return VectorSearchController(request=request, user_repo=request.state.user_repo)
except NotFoundException as e:
# Handle specific NotFoundException with a custom message or logging
logger.warn(f"Failed to initialize HistoryController: {str(e)}")
raise HTTPException(status_code=404, detail=f"Initialization failed: {str(e)}") from e
except Exception as e:
# Catch all other exceptions
logger.error(f"Unexpected error initializing HistoryController: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error") from e

#################################################
# Create vectorstore from files
#################################################
@router.post(
"/vectorstores",
name='retrieval_vectorstore_create',
response_model=ResponseCreateVectorStore,
tags=[TAG]
)
Expand Down Expand Up @@ -68,6 +80,7 @@ async def create_vectorstore(
#################################################
@router.post(
"/vectorstores/file",
name='retrieval_vectorstore_file_create',
response_model=ResponseFileLoader,
tags=[TAG]
)
Expand Down Expand Up @@ -117,6 +130,7 @@ async def create_vectorstore_from_file(
#################################################
@router.post(
"/vectorstores/multi",
name='retrieval_vectorstore_multi_create',
response_model=ResponseCreateVectorStore,
tags=[TAG],
# include_in_schema=False # TODO: Needs some work
Expand Down Expand Up @@ -164,6 +178,7 @@ async def create_vectorstore_from_multiple_sources(
######################################
@router.get(
"/vectorstores/pinecone",
name='retrieval_vectorstore_pinecone_list',
response_model=ResponseListPineconeVectorStores,
tags=[TAG]
)
Expand Down Expand Up @@ -202,6 +217,7 @@ async def list_pinecone_vectorstores(controller: VectorSearchController = Depend
######################################
@router.delete(
"/vectorstores/pinecone",
name='retrieval_vectorstore_pinecone_delete',
status_code=status.HTTP_204_NO_CONTENT,
tags=[TAG]
)
Expand All @@ -226,6 +242,7 @@ async def delete_pinecone_vectorstore(
######################################
@router.get(
"/vectorstores/redis",
name='retrieval_vectorstore_redis_list',
response_model=ResponseListPineconeVectorStores,
tags=[TAG]
)
Expand Down Expand Up @@ -264,6 +281,7 @@ async def list_redis_vectorstores(controller: VectorSearchController = Depends(g
######################################
@router.delete(
"/vectorstores/redis",
name='retrieval_vectorstore_redis_delete',
status_code=status.HTTP_204_NO_CONTENT,
tags=[TAG]
)
Expand Down
Loading

0 comments on commit 5c2e40b

Please sign in to comment.