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

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

Merged
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
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
24 changes: 19 additions & 5 deletions promptengineers/fastapi/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

def get_controller(request: Request) -> ChatController:
try:
return ChatController(request=request, user_repo=request.state.user_repo)
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)}")
Expand All @@ -38,6 +42,7 @@ def get_controller(request: Request) -> ChatController:
@router.post(
"/chat",
tags=[TAG],
name='chat',
response_model=ResponseChat,
responses={
200: {
Expand Down Expand Up @@ -106,6 +111,7 @@ async def chat(
@router.post(
"/chat/agent",
tags=[TAG],
name='chat_agent',
response_model=ResponseAgentChat,
responses={
200: {
Expand Down Expand Up @@ -206,6 +212,7 @@ async def agent(
@router.post(
"/chat/vectorstore",
tags=[TAG],
name='chat_vectorstore',
response_model=ResponseVectorstoreChat,
responses={
200: {
Expand All @@ -231,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 @@ -241,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 @@ -262,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
5 changes: 5 additions & 0 deletions promptengineers/fastapi/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_controller(request: Request) -> HistoryController:
@router.get(
"/chat/history",
tags=[TAG],
name='history_list',
response_model=ResponseHistoryIndex
)
async def list_chat_histories(
Expand Down Expand Up @@ -70,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 @@ -105,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 @@ -141,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 @@ -173,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
5 changes: 5 additions & 0 deletions promptengineers/fastapi/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_controller(request: Request) -> PromptController:
@router.get(
"/prompt/system",
tags=[TAG],
name='prompt_system_list',
response_model=ResponsePromptSystemList
)
async def index(
Expand Down Expand Up @@ -67,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 @@ -102,6 +104,7 @@ async def create(
@router.get(
"/prompt/system/{prompt_id}",
tags=[TAG],
name='prompt_system_show',
response_model=ResponsePromptSystem,
)
async def show(
Expand Down Expand Up @@ -138,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 @@ -170,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
7 changes: 7 additions & 0 deletions promptengineers/fastapi/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_controller(request: Request) -> IController:
#################################################
@router.post(
"/vectorstores",
name='retrieval_vectorstore_create',
response_model=ResponseCreateVectorStore,
tags=[TAG]
)
Expand Down Expand Up @@ -79,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 @@ -128,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 @@ -175,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 @@ -213,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 @@ -237,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 @@ -275,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
5 changes: 5 additions & 0 deletions promptengineers/fastapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def get_controller(request: Request) -> IController:
@router.get(
"/chat/settings",
tags=[TAG],
name='settings_list',
response_model=ResponseSettingsList
)
async def index(
Expand Down Expand Up @@ -69,6 +70,7 @@ async def index(
@router.post(
"/chat/settings",
tags=[TAG],
name='settings_create',
response_model=ResponseCreate
)
async def create(
Expand Down Expand Up @@ -104,6 +106,7 @@ async def create(
@router.get(
"/chat/settings/{setting_id}",
tags=[TAG],
name='settings_show',
response_model=ResponseSetting,
)
async def show(
Expand Down Expand Up @@ -140,6 +143,7 @@ async def show(
@router.put(
"/chat/settings/{setting_id}",
tags=[TAG],
name='settings_update',
response_model=ResponseUpdate,
)
async def update(
Expand Down Expand Up @@ -172,6 +176,7 @@ async def update(
@router.delete(
"/chat/settings/{setting_id}",
tags=[TAG],
name='settings_delete',
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete(
Expand Down
3 changes: 3 additions & 0 deletions promptengineers/fastapi/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"/files",
response_model=ResponseRetrieveFiles,
tags=[TAG],
name='storage_list_files',
)
async def list_files():
try:
Expand Down Expand Up @@ -54,6 +55,7 @@ async def list_files():
"/files",
response_model=ResponseFileStorage,
tags=[TAG],
name='storage_add_files',
)
async def save_files(
files: List[UploadFile] = File(...)
Expand Down Expand Up @@ -87,6 +89,7 @@ async def save_files(
"/files",
status_code=status.HTTP_204_NO_CONTENT,
tags=[TAG],
name='storage_delete_files',
)
async def delete_file(
prefix: str,
Expand Down