Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
from typing import List, Tuple
|
| 3 |
-
import openai # Assuming you're using OpenAI's API (make sure to install the OpenAI package)
|
| 4 |
from flask import Flask, request, jsonify
|
|
|
|
| 5 |
|
| 6 |
# Initialize Flask app
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
# Set the
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
SYSTEM_MESSAGE = "You are a helpful assistant."
|
| 14 |
|
| 15 |
-
# Function to generate AI response
|
| 16 |
def generate_response(
|
| 17 |
user_input: str,
|
| 18 |
history: List[Tuple[str, str]],
|
|
@@ -21,7 +28,7 @@ def generate_response(
|
|
| 21 |
top_p: float = 1.0
|
| 22 |
) -> str:
|
| 23 |
"""
|
| 24 |
-
Generates a response
|
| 25 |
Args:
|
| 26 |
user_input: The user's input message.
|
| 27 |
history: A list of tuples containing the conversation history
|
|
@@ -33,41 +40,32 @@ def generate_response(
|
|
| 33 |
str: The generated response from the AI model.
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Iterate through the history list and format accordingly
|
| 40 |
for user_message, assistant_message in history:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
# Add the current user input
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
response =
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
top_p=top_p,
|
| 55 |
-
stream=True
|
| 56 |
-
):
|
| 57 |
-
# Check if 'choices' is present and non-empty
|
| 58 |
-
if msg and 'choices' in msg and msg['choices']:
|
| 59 |
-
# Ensure the 'delta' and 'content' properties exist before using them
|
| 60 |
-
token = msg['choices'][0].get('delta', {}).get('content', '')
|
| 61 |
-
if token:
|
| 62 |
-
response += token
|
| 63 |
-
else:
|
| 64 |
-
# Handle unexpected response format or empty choices
|
| 65 |
-
print("Warning: Unexpected response format or empty 'choices'.")
|
| 66 |
-
break
|
| 67 |
-
return response or "Sorry, I couldn't generate a response. Please try again."
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
# Log the error for debugging purposes
|
| 71 |
print(f"An error occurred: {e}")
|
| 72 |
return "Error: An unexpected error occurred while processing your request."
|
| 73 |
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import List, Tuple
|
|
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
+
from google.cloud import vertex_ai # Ensure to install the Google Cloud SDK (vertex-ai)
|
| 5 |
|
| 6 |
# Initialize Flask app
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Set the Google Cloud project ID and location (Make sure to replace with your own)
|
| 10 |
+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT_ID") # Make sure to set this in your environment
|
| 11 |
+
location = os.getenv("GOOGLE_CLOUD_LOCATION", "us-central1") # Default location if not set
|
| 12 |
|
| 13 |
+
# Initialize Vertex AI client
|
| 14 |
+
vertex_ai_client = vertex_ai.PredictionServiceClient(client_options={"api_endpoint": f"{location}-aiplatform.googleapis.com"})
|
| 15 |
+
|
| 16 |
+
# Define the endpoint for your model deployment
|
| 17 |
+
endpoint = "projects/{project_id}/locations/{location}/endpoints/{endpoint_id}" # Replace with your actual endpoint ID
|
| 18 |
+
|
| 19 |
+
# Define a system message (if necessary)
|
| 20 |
SYSTEM_MESSAGE = "You are a helpful assistant."
|
| 21 |
|
| 22 |
+
# Function to generate AI response using Google Gemini (Vertex AI)
|
| 23 |
def generate_response(
|
| 24 |
user_input: str,
|
| 25 |
history: List[Tuple[str, str]],
|
|
|
|
| 28 |
top_p: float = 1.0
|
| 29 |
) -> str:
|
| 30 |
"""
|
| 31 |
+
Generates a response using the Google Gemini (Vertex AI) API.
|
| 32 |
Args:
|
| 33 |
user_input: The user's input message.
|
| 34 |
history: A list of tuples containing the conversation history
|
|
|
|
| 40 |
str: The generated response from the AI model.
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
+
# Prepare the history and current input for the model
|
| 44 |
+
conversation = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
|
|
|
|
|
|
| 45 |
for user_message, assistant_message in history:
|
| 46 |
+
conversation.append({"role": "user", "content": user_message})
|
| 47 |
+
conversation.append({"role": "assistant", "content": assistant_message})
|
| 48 |
+
|
| 49 |
# Add the current user input
|
| 50 |
+
conversation.append({"role": "user", "content": user_input})
|
| 51 |
+
|
| 52 |
+
# Prepare the payload for the request to Vertex AI
|
| 53 |
+
instances = [{"content": conversation}]
|
| 54 |
+
parameters = {
|
| 55 |
+
"temperature": temperature,
|
| 56 |
+
"max_output_tokens": max_tokens,
|
| 57 |
+
"top_p": top_p,
|
| 58 |
+
}
|
| 59 |
|
| 60 |
+
# Send the request to the Vertex AI API
|
| 61 |
+
response = vertex_ai_client.predict(endpoint=endpoint, instances=instances, parameters=parameters)
|
| 62 |
+
|
| 63 |
+
# Extract the response from the API output
|
| 64 |
+
ai_response = response.predictions[0].get('content', 'Sorry, I couldn’t generate a response.')
|
| 65 |
+
|
| 66 |
+
return ai_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
except Exception as e:
|
|
|
|
| 69 |
print(f"An error occurred: {e}")
|
| 70 |
return "Error: An unexpected error occurred while processing your request."
|
| 71 |
|