From f3fca3552724b85062cd39c081b2b26f1837d9b2 Mon Sep 17 00:00:00 2001 From: earthenvessel <49989763+earthenvessel@users.noreply.github.com> Date: Fri, 29 May 2026 15:22:27 -0400 Subject: [PATCH] fix: update Ollama embedding API usage --- app.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index 59a3b4a..d55a44e 100644 --- a/app.py +++ b/app.py @@ -98,8 +98,8 @@ def embed_documents(ollama_url: str, model: str, texts: List[str]) -> Union[List """ Generate embeddings for a list of text documents using the specified Ollama model. - This function sends POST requests to the Ollama server to generate embeddings - for each text document in the input list. + This function sends a POST request to the Ollama server to generate embeddings + for the input documents. Args: ollama_url (str): The URL of the Ollama server. @@ -111,24 +111,28 @@ def embed_documents(ollama_url: str, model: str, texts: List[str]) -> Union[List if successful, or None if there's an error. Note: - If there's an error generating embeddings for any document, the function will - display an error message in the Streamlit sidebar and return None. + If there's an error generating embeddings, the function will display an error + message in the Streamlit sidebar and return None. """ - embeddings = [] - for text in texts: - try: - response = requests.post( - f'{ollama_url}/api/embeddings', - json={'model': model, 'prompt': text}, - timeout=30 - ) - response.raise_for_status() - embedding = response.json()['embedding'] - embeddings.append(embedding) - except requests.RequestException as e: - st.sidebar.error(f"Error getting embedding: {str(e)}") - return None - return embeddings + try: + response = requests.post( + f'{ollama_url}/api/embed', + json={'model': model, 'input': texts}, + timeout=30 + ) + response.raise_for_status() + + response_json = response.json() + if 'embeddings' in response_json: + return response_json['embeddings'] + if 'embedding' in response_json: + return [response_json['embedding']] + + st.sidebar.error("Error getting embedding: Unexpected Ollama response format.") + return None + except requests.RequestException as e: + st.sidebar.error(f"Error getting embedding: {str(e)}") + return None def chat_with_documents(ollama_url: str, model: str, prompt: str, temperature: float): """ @@ -384,6 +388,8 @@ def hybrid_search(query: str, collection, documents: List[Dict[str, Union[str, D """ # Embed the query question_embedding = embed_documents(st.session_state.ollama_url, st.session_state.embedding_model, [query]) + if not question_embedding: + return [] # Perform semantic search over all documents total_docs = len(documents)