|
6 | 6 | from rest_framework import status |
7 | 7 | from django.utils.decorators import method_decorator |
8 | 8 | from django.views.decorators.csrf import csrf_exempt |
9 | | -import openai |
10 | 9 | import anthropic |
11 | 10 |
|
12 | 11 | from api.models.model_embeddings import Embeddings |
@@ -59,65 +58,6 @@ def anthropic_citations(client, content_chunks, user_prompt): |
59 | 58 | return texts, cited_texts |
60 | 59 |
|
61 | 60 |
|
62 | | -@method_decorator(csrf_exempt, name='dispatch') |
63 | | -class TextExtractionAPIView(APIView): |
64 | | - |
65 | | - permission_classes = [IsAuthenticated] |
66 | | - |
67 | | - def post(self, request): |
68 | | - try: |
69 | | - guid = request.query_params.get('guid') |
70 | | - field = request.query_params.get('field') |
71 | | - |
72 | | - query = Embeddings.objects.filter(upload_file__guid=guid) |
73 | | - |
74 | | - #TODO: Message OpenAI with each chunk separately if there are files that are too large for OpenAI |
75 | | - |
76 | | - query_texts = [ |
77 | | - f"[Start of INFO === GUID: {obj.upload_file.guid}, Page Number: {obj.page_num}, Chunk Number: {obj.chunk_number}, Text: {obj.text} === End of INFO]" |
78 | | - for obj in query] |
79 | | - |
80 | | - query_text = " ".join(query_texts) |
81 | | - |
82 | | - prompt_text = ( |
83 | | - f"""You are an AI assistant tasked with providing detailed, well-structured responses based on the information provided in [PROVIDED-INFO]. Follow these guidelines strictly: |
84 | | - 1. Content: Use information contained within [PROVIDED-INFO] to answer the question. |
85 | | - 2. Organization: Structure your response with clear sections and paragraphs. |
86 | | - 3. Citations: After EACH sentence that uses information from [PROVIDED-INFO], include a citation in this exact format:***[{{file_id}}], Page {{page_number}}, Chunk {{chunk_number}}*** . Only use citations that correspond to the information you're presenting. |
87 | | - 4. Clarity: Ensure your answer is well-structured and easy to follow. |
88 | | - 5. Direct Response: Answer the user's question directly without unnecessary introductions or filler phrases. |
89 | | - Here's an example of the required response format: |
90 | | - ________________________________________ |
91 | | - See's Candy in the context of sales during a specific event. The candy counters rang up 2,690 individual sales on a Friday, and an additional 3,931 transactions on a Saturday ***[16s848as-vcc1-85sd-r196-7f820a4s9de1, Page 5, Chunk 26]***. |
92 | | - People like the consumption of fudge and peanut brittle the most ***[130714d7-b9c1-4sdf-b146-fdsf854cad4f, Page 9, Chunk 19]***. |
93 | | - Here is the history of See's Candy: the company was purchased in 1972, and its products have not been materially altered in 101 years ***[895sdsae-b7v5-416f-c84v-7f9784dc01e1, Page 2, Chunk 13]***. |
94 | | - Bipolar disorder treatment often involves mood stabilizers. Lithium is a commonly prescribed mood stabilizer effective in reducing manic episodes ***[b99988ac-e3b0-4d22-b978-215e814807f4, Page 29, Chunk 122]***. For acute hypomania or mild to moderate mania, initial treatment with risperidone or olanzapine monotherapy is suggested ***[b99988ac-e3b0-4d22-b978-215e814807f4, Page 24, Chunk 101]***. |
95 | | - ________________________________________ |
96 | | - Please provide your response to the user's question following these guidelines precisely. |
97 | | - [PROVIDED-INFO] = {query_text}""" |
98 | | - ) |
99 | | - |
100 | | - |
101 | | - openai.api_key = os.getenv("OPENAI_API_KEY") |
102 | | - response = openai.ChatCompletion.create( |
103 | | - model="gpt-4o-mini", |
104 | | - temperature=0.2, |
105 | | - messages=[{"role": "system", "content": prompt_text}, |
106 | | - {"role": "user", "content": f"List out the {field}"} |
107 | | - ] |
108 | | - ) |
109 | | - |
110 | | - answer = response["choices"][0]["message"]["content"] |
111 | | - |
112 | | - return Response({ |
113 | | - "query": query_text, |
114 | | - "answer": answer |
115 | | - }, status=status.HTTP_200_OK) |
116 | | - |
117 | | - except Exception as e: |
118 | | - print(f"An error occurred: {e}") |
119 | | - return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
120 | | - |
121 | 61 | @method_decorator(csrf_exempt, name='dispatch') |
122 | 62 | class RuleExtractionAPIView(APIView): |
123 | 63 |
|
|
0 commit comments