Spaces:
Sleeping
Sleeping
| from langchain_openai import OpenAIEmbeddings | |
| from pinecone import Pinecone | |
| import streamlit as st | |
| from openai import OpenAI | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Initialize OpenAI client | |
| client = OpenAI(api_key=os.getenv('OPENAI_API_KEY')) | |
| # Initialize embeddings | |
| embeddings = OpenAIEmbeddings(model="text-embedding-ada-002", api_key=os.getenv('OPENAI_API_KEY')) | |
| # Initialize Pinecone | |
| pc = Pinecone(api_key=os.getenv('PINECONE_API_KEY')) | |
| # Check if index exists and connect to it | |
| index_name = "aido-hybrid" | |
| if index_name not in pc.list_indexes().names(): | |
| print("Creating a new Pinecone index...") | |
| pc.create_index( | |
| name=index_name, | |
| dimension=1536, # dimensionality of text-embedding-ada-002 | |
| metric="cosine" | |
| ) | |
| # Connect to the existing Pinecone index | |
| index = pc.Index(index_name) | |
| def find_match(input): | |
| # Get embeddings for the input query | |
| input_em = embeddings.embed_query(input) | |
| # Query Pinecone | |
| result = index.query(vector=input_em, top_k=5, include_metadata=True) | |
| # Return the top 2 matches | |
| return result['matches'][0]['metadata']['text'] + "\n" + result['matches'][1]['metadata']['text'] | |
| def query_refiner(conversation, query): | |
| # Using the new ChatCompletion API instead of the deprecated Completion API | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", | |
| "content": "You are a helpful assistant that refines user queries based on conversation context."}, | |
| {"role": "user", | |
| "content": f"Given the following user query and conversation log, formulate a question that would be the most relevant to provide the user with an answer from a knowledge base.\n\nCONVERSATION LOG: \n{conversation}\n\nQuery: {query}\n\nRefined Query:"} | |
| ], | |
| temperature=0.7, | |
| max_tokens=256 | |
| ) | |
| return response.choices[0].message.content | |
| def get_conversation_string(): | |
| conversation_string = "" | |
| for i in range(len(st.session_state['responses']) - 1): | |
| conversation_string += "Human: " + st.session_state['requests'][i] + "\n" | |
| conversation_string += "Bot: " + st.session_state['responses'][i + 1] + "\n" | |
| return conversation_string |