File size: 2,304 Bytes
785c4f7
7d3dbed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbd71f6
 
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
import torch
from evo_model import EvoTransformer
from transformers import AutoTokenizer, pipeline
from rag_utils import RAGRetriever, extract_text_from_file
import os

# Load Evo model
def load_evo_model(model_path="evo_hellaswag.pt", device=None):
    if device is None:
        device = "cuda" if torch.cuda.is_available() else "cpu"

    model = EvoTransformer()
    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()
    return model, device

evo_model, device = load_evo_model()
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

# Load GPT-3.5 (via OpenAI API)
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

# RAG Retriever
retriever = RAGRetriever()

def get_context_from_file(file_obj):
    file_path = file_obj.name
    text = extract_text_from_file(file_path)
    retriever.add_document(text)
    return text

# Evo prediction
def get_evo_response(prompt, file=None):
    # Step 1: augment context if document is uploaded
    context = ""
    if file is not None:
        context_list = retriever.retrieve(prompt)
        context = "\n".join(context_list)

    full_prompt = f"{prompt}\n{context}"

    # Step 2: use Evo to predict
    options = ["Yes, proceed with the action.", "No, maintain current strategy."]
    inputs = [f"{full_prompt} {opt}" for opt in options]

    encoded = tokenizer(inputs, padding=True, truncation=True, return_tensors="pt").to(device)

    with torch.no_grad():
        logits = evo_model(encoded["input_ids"]).squeeze(-1)
        probs = torch.softmax(logits, dim=0)
        best = torch.argmax(probs).item()

    return f"Evo suggests: {options[best]} (Confidence: {probs[best]:.2f})"

# GPT-3.5 response
def get_gpt_response(prompt, file=None):
    context = ""
    if file is not None:
        context_list = retriever.retrieve(prompt)
        context = "\n".join(context_list)

    full_prompt = (
        f"Question: {prompt}\n"
        f"Relevant Context:\n{context}\n"
        f"Answer like a financial advisor."
    )

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "user", "content": full_prompt}
        ],
        temperature=0.4,
    )

    return response.choices[0].message.content.strip()

    #