-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_openai.py
More file actions
24 lines (18 loc) · 803 Bytes
/
rag_openai.py
File metadata and controls
24 lines (18 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
from llama_index.core import StorageContext, load_index_from_storage
from llama_index.llms.openai import OpenAI
def get_response(question):
openai_api_key = os.environ.get("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("A variável de ambiente OPENAI_API_KEY não está definida.")
# Carregar o índice salvo
persist_dir = "index_storage"
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
vector_index = load_index_from_storage(storage_context)
# Configurar o modelo da OpenAI
llm = OpenAI(model="gpt-4o", api_key=openai_api_key)
# Criar o mecanismo de consulta
query_engine = vector_index.as_query_engine(llm=llm)
# Fazer a consulta
response = query_engine.query(question)
return str(response)