File size: 1,806 Bytes
785c4f7
11f2d5b
5b3d26d
 
7a7ebad
453eba8
5b3d26d
6a1a7af
11f2d5b
453eba8
7d3dbed
5b3d26d
7a7ebad
5b3d26d
 
7a7ebad
5b3d26d
 
 
 
7a7ebad
5b3d26d
 
 
 
 
 
 
 
 
 
 
 
 
 
7a7ebad
5b3d26d
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoTokenizer
from model import EvoTransformerV22
from search_utils import web_search
import openai

# Load Evo model and tokenizer
model = EvoTransformerV22()
model.load_state_dict(torch.load("evo_hellaswag.pt", map_location="cpu"))
model.eval()

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

# GPT Setup
openai.api_key = "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"  # 🔑 Set your actual key securely

def get_evo_response(query, options, user_context=""):
    context_texts = web_search(query) + ([user_context] if user_context else [])
    context_str = "\n".join(context_texts)
    input_pairs = [f"{query} [SEP] {opt} [CTX] {context_str}" for opt in options]

    scores = []
    for pair in input_pairs:
        encoded = tokenizer(pair, return_tensors="pt", truncation=True, padding="max_length", max_length=128)
        with torch.no_grad():
            output = model(encoded["input_ids"])
            score = torch.sigmoid(output).item()
            scores.append(score)

    best_idx = int(scores[1] > scores[0])
    return (
        options[best_idx],
        f"{options[0]}: {scores[0]:.3f} vs {options[1]}: {scores[1]:.3f}",
        max(scores),
        context_str
    )

def get_gpt_response(query, user_context=""):
    try:
        context_block = f"\n\nContext:\n{user_context}" if user_context else ""
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": query + context_block}]
        )
        return completion.choices[0].message.content.strip()
    except Exception as e:
        return f"⚠️ GPT error: {str(e)}"