看看这段代码,它创建了两个不同的llms - 一个用于嵌入,另一个用于“上下文+查询”的评估。
import os
import sys
import llama_index.indices.vector_store.retrievers
import llama_index.query_engine.retriever_query_engine
import llama_index.response_synthesizers
import llama_index.retrievers
if 'OPENAI_API_KEY' not in os.environ:
sys.stderr.write("""
You haven't set up your API key yet.
If you don't have an API key yet, visit:
https://platform.openai.com/signup
1. Make an account or sign in
2. Click "View API Keys" from the top right menu.
3. Click "Create new secret key"
Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
""")
exit(1)
import streamlit as st
from llama_index import (
ServiceContext,
SimpleDirectoryReader,
VectorStoreIndex,
)
from llama_index.llms import OpenAI
st.set_page_config(page_title="LlamaIndex Q&A with Lyft Financials",
page_icon="蓮",
layout="centered",
initial_sidebar_state="auto",
menu_items=None)
st.title("LlamaIndex 蓮 Q&A with Lyft Financials")
@st.cache_resource(show_spinner=False)
def load_data():
"""
Loads and indexes the Lyft 2021 financials using the VectorStoreIndex.
Returns:
- VectorStoreIndex: Indexed representation of the Lyft 10-K.
"""
with st.spinner(
text="Loading and indexing the Lyft 10-K. This may take a while..."):
reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
docs = reader.load_data()
service_context__embedding = ServiceContext.from_defaults(
llm=OpenAI(
model="text-ada-001",
temperature=0.0,
),
system_prompt=
"You are an AI assistant creating text embedding for financial reports."
)
index = VectorStoreIndex.from_documents(
docs, service_context=service_context__embedding)
return index
index = load_data()
retriever = llama_index.indices.vector_store.retrievers.VectorIndexRetriever(
index=index,
similarity_top_k=3,
)
llm_context_query__service_context = ServiceContext.from_defaults(
llm=OpenAI(
model="gpt-3.5-turbo",
temperature=0.1,
),
system_prompt=
"You are an AI assistant answering questions related to financial reports fragments."
)
response_synthesizer = llama_index.response_synthesizers.get_response_synthesizer(
response_mode="refine",
service_context=llm_context_query__service_context,
use_async=False,
streaming=False,
)
query_engine = (
llama_index.query_engine.retriever_query_engine.RetrieverQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
))
user_input = st.text_input("Enter Your Query", "")
if st.button("Submit"):
st.write(f"Your Query: {user_input}")
with st.spinner("Thinking..."):
result = query_engine.query(user_input)
print(result.source_nodes)
st.write(f"Answer: {str(result)}")