-
Notifications
You must be signed in to change notification settings - Fork 0
Spotlight Base branch #108
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SpotlightConfig(AppConfig): | ||
name = 'spotlight' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import boto3 | ||
import openai | ||
import json | ||
from typing import Dict | ||
|
||
|
||
AWS_REGION = os.environ["AWS_REGION"] | ||
AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"] | ||
AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"] | ||
|
||
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"] | ||
KNOWLEDGE_BASE_ID = os.environ["KNOWLEDGE_BASE_ID"] | ||
|
||
|
||
bedrock_session = boto3.Session( | ||
region_name=AWS_REGION, | ||
aws_access_key_id=AWS_ACCESS_KEY_ID, | ||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY | ||
) | ||
|
||
openai_client = openai.OpenAI( | ||
api_key=OPENAI_API_KEY, | ||
max_retries=5, | ||
timeout=10 | ||
) | ||
|
||
|
||
def get_openai_response(*, system_prompt: str) -> dict: | ||
try: | ||
chat_completion_resp = openai_client.chat.completions.create( | ||
model="gpt-4o", | ||
response_format={ | ||
"type": "json_object" | ||
}, | ||
messages=[ | ||
{"role": "system", "content": system_prompt} | ||
], | ||
temperature=0, | ||
max_tokens=256, | ||
top_p=0, | ||
frequency_penalty=0, | ||
presence_penalty=0 | ||
) | ||
|
||
return json.loads( | ||
chat_completion_resp.choices[0].message.content | ||
) | ||
|
||
except (openai.OpenAIError, json.JSONDecodeError) as e: | ||
raise Exception(message=str(e)) | ||
|
||
|
||
|
||
def get_support_response_from_bedrock(*, prompt_template: str, input_message: str) -> Dict: | ||
try: | ||
bedrock_agent_runtime_client = bedrock_session.client( | ||
'bedrock-agent-runtime' | ||
) | ||
|
||
response = bedrock_agent_runtime_client.retrieve_and_generate( | ||
input={ | ||
'text': input_message | ||
}, | ||
retrieveAndGenerateConfiguration={ | ||
'type': 'KNOWLEDGE_BASE', | ||
'knowledgeBaseConfiguration': { | ||
'knowledgeBaseId': KNOWLEDGE_BASE_ID, | ||
'modelArn': 'arn:aws:bedrock:ap-south-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0', | ||
'generationConfiguration': { | ||
'inferenceConfig': { | ||
'textInferenceConfig': { | ||
'maxTokens': 2048, | ||
'stopSequences': [], | ||
'temperature': 0, | ||
'topP': 1 | ||
} | ||
}, | ||
'promptTemplate': { | ||
'textPromptTemplate': prompt_template | ||
} | ||
}, | ||
'retrievalConfiguration': { | ||
'vectorSearchConfiguration': { | ||
'numberOfResults': 5, | ||
'overrideSearchType': 'HYBRID', | ||
} | ||
} | ||
} | ||
} | ||
) | ||
|
||
return response | ||
|
||
except json.JSONDecodeError as e: | ||
print(e) | ||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve the error handling. Printing the exception is not an ideal way to handle errors. Consider the following improvements:
Here's an example of how you can improve the error handling: import logging
logger = logging.getLogger(__name__)
try:
# ...
except json.JSONDecodeError as e:
logger.error(f"Error decoding JSON response from AWS Bedrock: {str(e)}")
raise Exception("Failed to decode JSON response from AWS Bedrock") from e |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 3.1.14 on 2024-09-10 05:28 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Query', | ||
fields=[ | ||
('id', models.AutoField(primary_key=True, serialize=False)), | ||
('query', models.TextField()), | ||
('workspace_id', models.IntegerField(help_text='Workspace id of the organization')), | ||
('_llm_response', models.JSONField(default={})), | ||
('created_at', models.DateTimeField(auto_now_add=True, help_text='Created at datetime')), | ||
('updated_at', models.DateTimeField(auto_now=True, help_text='Updated at datetime')), | ||
('user', models.ForeignKey(help_text='Reference to users table', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'db_table': 'queries', | ||
}, | ||
), | ||
] |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||
from django.db import models | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the unused import. The import statement Remove the unused import: -from django.db import models Committable suggestion
Suggested change
ToolsRuff
|
||||
from django.contrib.auth import get_user_model | ||||
|
||||
|
||||
User = get_user_model() | ||||
|
||||
|
||||
# Create your models here. | ||||
class Query(models.Model): | ||||
id = models.AutoField(primary_key=True) | ||||
query = models.TextField() | ||||
workspace_id = models.IntegerField(help_text="Workspace id of the organization") | ||||
_llm_response = models.JSONField(default={}) | ||||
user = models.ForeignKey(User, on_delete=models.CASCADE, help_text='Reference to users table') | ||||
created_at = models.DateTimeField( | ||||
auto_now_add=True, help_text="Created at datetime" | ||||
) | ||||
updated_at = models.DateTimeField( | ||||
auto_now=True, help_text="Updated at datetime" | ||||
) | ||||
|
||||
class Meta: | ||||
db_table = 'queries' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the unused import.
The import statement
from django.contrib import admin
is unused in this file.Remove the unused import:
-from django.contrib import admin
Committable suggestion
Tools
Ruff