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

With_guardrails #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
guardrails package and rail file added.
Luca-Blight committed Jul 10, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 848aa9b538effbe3aed00e87c11c84a5ed72a9c9
33 changes: 18 additions & 15 deletions backend/app/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import os
import time
import openai

from fastapi import APIRouter, UploadFile
from fastapi.responses import HTMLResponse
from langchain.chat_models import ChatOpenAI
@@ -9,6 +11,9 @@
from dotenv import load_dotenv, find_dotenv
from .prompts import product_prompt_template, final_product_prompt_template
from utils.document_utils import extract_text_from_pdf, split_into_chunks
from rich import print

import guardrails as gd

_ = load_dotenv(find_dotenv())

@@ -48,31 +53,29 @@ async def analyze_document(file: UploadFile) -> dict:

with ThreadPoolExecutor() as executor:
if filename.endswith(".pdf"):

extracted_text = await loop.run_in_executor(
executor, extract_text_from_pdf, file.file
)

guard = gd.Guard.from_rail('/Users/Zachary_Royals/Code/zelta-challenge/backend/app/api/sales_transcript.rail')

chunks = await loop.run_in_executor(
executor, split_into_chunks, extracted_text
)
chat = ChatOpenAI(temperature=0.0, model="gpt-4")

# run tasks in parallel
start_chat_tasks = time.time()
tasks = [loop.run_in_executor(executor, chat, product_prompt_template.format_messages(text=chunk)) for chunk in chunks]
insights = await asyncio.gather(*tasks)
print(f'Time taken for initial chunk insights: {time.time() - start_chat_tasks} seconds')

#append insights into final product prompt
start_agg_insights = time.time()
agg_insights = final_product_prompt_template.format_messages(text=insights)
final_insights = await loop.run_in_executor(executor, chat, agg_insights)
print(f'Time taken for final insights: {time.time() - start_agg_insights} seconds')

for chunk in chunks:
raw_llm_output, validated_output = guard(
openai.ChatCompletion.create,
prompt_params={"sales_transcript": chunk},
engine="chat-gpt4",
max_tokens=1024,
temperature=0.3,

)

execution_time = time.time() - start
print(f'Time taken: {execution_time} seconds')
return final_insights
return validated_output

elif file.endswith(".txt"):
return "This is a text file."
27 changes: 27 additions & 0 deletions backend/app/api/sales_transcript.rail
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<rail version="0.1">

<output>
<object name="customer_feedback">
<string name="delivery_days" description="delivery days of the product"/>
<string name="price_value" description="Any sentences about the price or value"/>
<string name="feature_requests" description="Any sentences about features or improvements that could be added"/>
<list name="competitor_mentions" description="Mentions of a competitor. Each company should be classified into separate item in the list.">
<object>
<string name="name" description="Name of the company" />
<string name="advantages" description="Describes what strengths of the competition's company has to offer" />
<string name="disadvtanges" description="Describes what the competitor's company is bad at" />
</object>
</list>
</object>
</output>


<prompt>

Given the following sales transcript extract the following information from speaker two, please extract a dictionary that contains the customer's feedback.

{{sales_transcript}}

</prompt>

</rail>