Added chat history and chat item

This commit is contained in:
Saurab-Shrestha9639*969**9858//852 2024-04-03 17:58:27 +05:45
parent 542ed0ef4e
commit 4bc9dd7870
19 changed files with 409 additions and 121 deletions

View file

@ -129,9 +129,7 @@ class ChatService:
else None
)
system_prompt = (
chat_engine_input.system_message.content
if chat_engine_input.system_message
else None
"You can only answer questions about the provided context. If you know the answer but it is not based in the provided context, don't provide the answer, just state the answer is not in the context provided."
)
chat_history = (
chat_engine_input.chat_history if chat_engine_input.chat_history else None
@ -165,9 +163,7 @@ class ChatService:
else None
)
system_prompt = (
chat_engine_input.system_message.content
if chat_engine_input.system_message
else None
"You can only answer questions about the provided context. If you know the answer but it is not based in the provided context, don't provide the answer, just state the answer is not in the context provided."
)
chat_history = (
chat_engine_input.chat_history if chat_engine_input.chat_history else None

View file

@ -22,6 +22,7 @@ completions_router = APIRouter(prefix="/v1", dependencies=[Depends(authenticated
class CompletionsBody(BaseModel):
conversation_id: int
prompt: str
system_prompt: str | None = None
use_context: bool = False
@ -33,6 +34,7 @@ class CompletionsBody(BaseModel):
"json_schema_extra": {
"examples": [
{
"conversation_id": 123,
"prompt": "How do you fry an egg?",
"system_prompt": "You are a rapper. Always answer with a rap.",
"stream": False,
@ -92,6 +94,14 @@ class CompletionsBody(BaseModel):
# )
# return chat_completion(request, chat_body)
def create_chat_item(db, sender, content, conversation_id):
chat_item_create = schemas.ChatItemCreate(
sender=sender,
content=content,
conversation_id=conversation_id
)
return crud.chat_item.create(db, obj_in=chat_item_create)
@completions_router.post(
"/chat",
@ -137,33 +147,47 @@ async def prompt_completion(
docs_ids.extend(doc_id)
body.context_filter = {"docs_ids": docs_ids}
chat_history = crud.chat.get_by_id(
db, id=body.conversation_id
)
if (chat_history is None) and (chat_history.user_id != current_user.id):
raise HTTPException(
status_code=404, detail="Chat history not found")
messages = [OpenAIMessage(content=body.prompt, role="user")]
create_chat_item(db, "user", body.prompt, body.conversation_id)
if body.system_prompt:
messages.insert(0, OpenAIMessage(
content=body.system_prompt, role="system"))
chat_body = ChatBody(
messages=messages,
use_context=body.use_context,
stream=body.stream,
include_sources=body.include_sources,
context_filter=body.context_filter,
)
log_audit(
model='Chat',
action='Chat',
details={
"query": body.prompt,
'user': current_user.username,
},
user_id=current_user.id
)
chat_response = await chat_completion(request, chat_body)
print(chat_response)
create_chat_item(db, "assistant", chat_response.choices[0].message.content, body.conversation_id)
return chat_response
except Exception as e:
print(traceback.format_exc())
logger.error(f"There was an error: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Internal Server Error",
)
messages = [OpenAIMessage(content=body.prompt, role="user")]
if body.system_prompt:
messages.insert(0, OpenAIMessage(
content=body.system_prompt, role="system"))
chat_body = ChatBody(
messages=messages,
use_context=body.use_context,
stream=body.stream,
include_sources=body.include_sources,
context_filter=body.context_filter,
)
log_audit(
model='Chat',
action='Chat',
details={
"query": body.prompt,
'user': current_user.username,
},
user_id=current_user.id
)
return await chat_completion(request, chat_body)
)