Skip to content

Commit

Permalink
Code quality check improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dividor committed May 8, 2024
1 parent b16aee5 commit 1840cda
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 410 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ RECIPES_BASE_URL=
RECIPES_MODEL=gpt-4-turbo
RECIPES_OPENAI_TEXT_COMPLETION_DEPLOYMENT_NAME=text-embedding-ada-002

#==================================================#
# Needed for displaying images #
#==================================================#
IMAGE_HOST=http://localhost:3080/images

#==================================================#
# Deployment to Azure #
# (Only needed if deploying to Azure) #
#==================================================#
AZURE_CONTAINER_REGISTRY=
AZURE_CONTAINER_REGISTRY_USERNAME=

#===============#
# JSON Logging #
#===============#
Expand Down
54 changes: 53 additions & 1 deletion actions/actions_plugins/postgres-universal/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os

import psycopg2
from robocorp.actions import action

Expand All @@ -10,24 +11,55 @@

class ReadOnlyConnection:
def __init__(self, conn_params):
"""
Initializes a new instance of the PostgresUniversalActions class.
Args:
conn_params (dict): A dictionary containing the connection parameters for the PostgreSQL database.
"""
self.conn_params = conn_params
self.conn = None

def __enter__(self):
"""
Establishes a connection to the PostgreSQL database and sets the transaction mode to READ ONLY.
Returns:
psycopg2.extensions.connection: The connection object to the PostgreSQL database.
"""
self.conn = psycopg2.connect(**self.conn_params)
cur = self.conn.cursor()
cur.execute("SET TRANSACTION READ ONLY;")
cur.close()
return self.conn

def __exit__(self, exc_type, exc_val, exc_tb):
"""
Exit the context manager and perform necessary cleanup operations.
Args:
exc_type (type): The type of the exception raised, if any.
exc_val (Exception): The exception raised, if any.
exc_tb (traceback): The traceback object associated with the exception, if any.
"""
self.conn.rollback() # Rollback any changes (though there shouldn't be any)
self.conn.close()


def truncate_output_with_beginning_clue(
output: str, max_chars: int = MAX_CHARS_TOT
) -> str:
"""
Truncates the given output string if its length exceeds the maximum number of characters.
Adds a beginning clue and a truncated message to indicate possible truncation.
Args:
output (str): The output string to be truncated.
max_chars (int, optional): The maximum number of characters allowed. Defaults to MAX_CHARS_TOT.
Returns:
str: The truncated output string with the beginning clue and truncated message.
"""
beginning_clue = (
"[Cut] " # A very short clue at the beginning to indicate possible truncation
)
Expand All @@ -42,6 +74,16 @@ def truncate_output_with_beginning_clue(


def get_database_schema(conn):
"""
Retrieves the schema information of a PostgreSQL database.
Args:
conn (psycopg2.extensions.connection): The connection object to the PostgreSQL database.
Returns:
str: The database schema information.
"""
schema_info = "Database Schema:\n"

with conn.cursor() as cursor:
Expand Down Expand Up @@ -116,7 +158,7 @@ def get_database_schema(conn):
cursor.execute(
"""
SELECT kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints AS tc
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
Expand All @@ -136,6 +178,16 @@ def get_database_schema(conn):


def truncate_query_results(results, max_chars=MAX_CHARS_TOT):
"""
Truncates the query results to a specified maximum number of characters.
Args:
results (list): The query results to be truncated.
max_chars (int, optional): The maximum number of characters allowed. Defaults to MAX_CHARS_TOT.
Returns:
str: The truncated query results.
"""
if not results:
return ""

Expand Down
19 changes: 11 additions & 8 deletions actions/actions_plugins/recipe-server/actions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ast
import base64
import io
import json
import logging
import sys
import os
import base64
from PIL import Image
import io
import sys

from dotenv import load_dotenv
from langchain.schema import HumanMessage, SystemMessage
from langchain_community.vectorstores.pgvector import PGVector
from langchain_openai import (
Expand All @@ -14,8 +15,8 @@
ChatOpenAI,
OpenAIEmbeddings,
)
from PIL import Image
from robocorp.actions import action
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()
Expand Down Expand Up @@ -448,7 +449,7 @@ def get_memory(user_input, chat_history, generate_intent=True) -> str:
logging.info("Python HTTP trigger function processed a request.")
# Retrieve the CSV file from the request

if generate_intent is not None and generate_intent == True:
if generate_intent is not None and generate_intent is True:
# chat history is passed from promptflow as a string representation of a list and this has to be converted back to a list for the intent generation to work!
history_list = ast.literal_eval(chat_history)
history_list.append({"inputs": {"question": user_input}})
Expand All @@ -462,8 +463,10 @@ def get_memory(user_input, chat_history, generate_intent=True) -> str:
recipe_id = result["metadata"]["custom_id"]
print("Recipe ID: ", recipe_id)
if response_image is not None and response_image != "":
process_image(response_image.replace("data:image/png;base64,", ""), recipe_id)
#result = "http://localhost:9999/memory_image.png"
process_image(
response_image.replace("data:image/png;base64,", ""), recipe_id
)
# result = "http://localhost:9999/memory_image.png"
result = f"{os.getenv('IMAGE_HOST')}/memory_image_{recipe_id}.png"

else:
Expand Down
62 changes: 0 additions & 62 deletions deploy_azure.py

This file was deleted.

77 changes: 0 additions & 77 deletions docker-compose-azure.yml

This file was deleted.

Loading

0 comments on commit 1840cda

Please sign in to comment.