Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,6 @@ import requests
|
|
| 34 |
import streamlit as st
|
| 35 |
from fastapi import FastAPI, HTTPException
|
| 36 |
from langchain.chains import ConversationalRetrievalChain
|
| 37 |
-
from langchain.chat_models import ChatAnthropic
|
| 38 |
from langchain.vectorstores import Pinecone
|
| 39 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 40 |
from langchain.memory import ConversationBufferMemory
|
|
@@ -45,6 +44,7 @@ from PIL import Image
|
|
| 45 |
from langchain_community.vectorstores import Pinecone
|
| 46 |
from pinecone import Pinecone as PineconeClient
|
| 47 |
from anthropic import Anthropic
|
|
|
|
| 48 |
|
| 49 |
# Load environment variables
|
| 50 |
load_dotenv()
|
|
@@ -53,9 +53,6 @@ load_dotenv()
|
|
| 53 |
app = FastAPI()
|
| 54 |
|
| 55 |
# API Keys
|
| 56 |
-
import os
|
| 57 |
-
from dotenv import load_dotenv
|
| 58 |
-
|
| 59 |
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 60 |
PINECONE_ENV = os.getenv("PINECONE_ENV")
|
| 61 |
INDEX_NAME = "agenticrag"
|
|
@@ -63,29 +60,45 @@ INDEX_NAME = "agenticrag"
|
|
| 63 |
if not PINECONE_API_KEY:
|
| 64 |
raise ValueError("Pinecone API Key is missing. Please set it in environment variables.")
|
| 65 |
|
| 66 |
-
#Initialize Hugging Face Embeddings
|
| 67 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 68 |
|
| 69 |
vector_store = Pinecone.from_existing_index(index_name=INDEX_NAME, embedding=embeddings)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
self.anthropic_client = Anthropic(api_key=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def count_tokens(self, text: str) -> int:
|
| 80 |
return self.anthropic_client.count_tokens(text)
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
model="claude-2",
|
| 84 |
temperature=0,
|
| 85 |
-
|
| 86 |
)
|
| 87 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 88 |
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Build RAG Chain
|
| 91 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
|
@@ -167,4 +180,4 @@ for user_q, ai_r in st.session_state.chat_history:
|
|
| 167 |
st.markdown("---")
|
| 168 |
|
| 169 |
st.markdown("---")
|
| 170 |
-
st.markdown("๐ Powered by Anthropic Claude, Pinecone, and LangChain.")
|
|
|
|
| 34 |
import streamlit as st
|
| 35 |
from fastapi import FastAPI, HTTPException
|
| 36 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
|
| 37 |
from langchain.vectorstores import Pinecone
|
| 38 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 39 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 44 |
from langchain_community.vectorstores import Pinecone
|
| 45 |
from pinecone import Pinecone as PineconeClient
|
| 46 |
from anthropic import Anthropic
|
| 47 |
+
from langchain.schema import BaseLLM
|
| 48 |
|
| 49 |
# Load environment variables
|
| 50 |
load_dotenv()
|
|
|
|
| 53 |
app = FastAPI()
|
| 54 |
|
| 55 |
# API Keys
|
|
|
|
|
|
|
|
|
|
| 56 |
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 57 |
PINECONE_ENV = os.getenv("PINECONE_ENV")
|
| 58 |
INDEX_NAME = "agenticrag"
|
|
|
|
| 60 |
if not PINECONE_API_KEY:
|
| 61 |
raise ValueError("Pinecone API Key is missing. Please set it in environment variables.")
|
| 62 |
|
| 63 |
+
# Initialize Hugging Face Embeddings
|
| 64 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 65 |
|
| 66 |
vector_store = Pinecone.from_existing_index(index_name=INDEX_NAME, embedding=embeddings)
|
| 67 |
|
| 68 |
+
# Custom Anthropic LLM Wrapper
|
| 69 |
+
class AnthropicLLM(BaseLLM):
|
| 70 |
+
def __init__(self, model: str, temperature: float, api_key: str):
|
| 71 |
+
super().__init__()
|
| 72 |
+
self.model = model
|
| 73 |
+
self.temperature = temperature
|
| 74 |
+
self.anthropic_client = Anthropic(api_key=api_key)
|
| 75 |
+
|
| 76 |
+
def _call(self, prompt: str, stop: list = None) -> str:
|
| 77 |
+
response = self.anthropic_client.completions.create(
|
| 78 |
+
model=self.model,
|
| 79 |
+
prompt=prompt,
|
| 80 |
+
temperature=self.temperature,
|
| 81 |
+
max_tokens_to_sample=500, # Adjust as needed
|
| 82 |
+
stop_sequences=stop or [],
|
| 83 |
+
)
|
| 84 |
+
return response.completion
|
| 85 |
|
| 86 |
def count_tokens(self, text: str) -> int:
|
| 87 |
return self.anthropic_client.count_tokens(text)
|
| 88 |
|
| 89 |
+
@property
|
| 90 |
+
def _llm_type(self) -> str:
|
| 91 |
+
return "anthropic"
|
| 92 |
+
|
| 93 |
+
# Initialize Anthropic LLM
|
| 94 |
+
llm = AnthropicLLM(
|
| 95 |
model="claude-2",
|
| 96 |
temperature=0,
|
| 97 |
+
api_key=os.getenv("ANTHROPIC_API_KEY")
|
| 98 |
)
|
|
|
|
| 99 |
|
| 100 |
+
# Initialize memory
|
| 101 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 102 |
|
| 103 |
# Build RAG Chain
|
| 104 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
|
|
|
| 180 |
st.markdown("---")
|
| 181 |
|
| 182 |
st.markdown("---")
|
| 183 |
+
st.markdown("๐ Powered by Anthropic Claude, Pinecone, and LangChain.")
|