File size: 2,835 Bytes
6f14d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from helpers.generate_embbedings import vector_store
from langchain_aws import ChatBedrock
import os

def make_prompt(history, prompt, context=None):
    formatted_history = ""

    if context:
        formatted_history += f"[CONTEXT] {context} [/CONTEXT]\n"

    for history_item in history:
        if history_item.from_ == 'user':
            formatted_history += f"[INST] {history_item.message} [/INST]\n"
        else:
            formatted_history += f"{history_item.message}\n"

    formatted_history += f"[INST] {prompt} [/INST]\n"

    return formatted_history

prompt = ChatPromptTemplate.from_template("{prompt}")

model = ChatBedrock(
    model="mistral.mistral-7b-instruct-v0:2",
    aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
    region=os.environ.get("AWS_DEFAULT_REGION"),
    max_tokens=8000,
    temperature=0
)

output_parser = StrOutputParser()

chain = prompt | model | output_parser

async def ask_question(question: str, history: list = [], project_id=None):
    """
    Generate a response for a given question based on history and project-specific context.
    """
    try:
        context = ""
        if project_id is not None:
            context = vector_store.similarity_search(
                query=question, k=4, filter={"project_id": project_id}
            )
        
        prompt = make_prompt(history, question, context)

        stream = chain.astream({"prompt": prompt})
        async for chunk in stream:
            yield chunk
    except Exception as e:
        raise RuntimeError(f"Error generating response: {str(e)}")


gpt_model = ChatOpenAI(
    temperature=0.7,
    model='gpt-4o-mini'
)
chain = prompt | gpt_model | StrOutputParser()

async def ask_openai(question: str, history: list = []):
    """
    Generate a response for a given question based on history and project-specific context.
    """
    ai_response = "I am the CloudMod Solutions Architect, an expert in AWS, Azure & GCP. How can I help you?"
    try:
        context = ("You are a AI Assistant for CloudMod Soluctions Architect, an expert in AWS, Azure & GCP \n"
                   "If asked question such as `what the chat does, what they are`\n"
                   "Answer question as per the context \n\n"
                   f"Here is the user query : {question}"
                   f"here is the previous chat history: {history}"
                   )

        prompt = context
        stream = chain.astream({"prompt": prompt})
        async for chunk in stream:
            yield chunk
    except Exception as e:
        raise RuntimeError(f"Error generating response: {str(e)}")