I’m working on a simple LLM project, here is my code:
import chromadb
import os
import chromadb.utils.embedding_functions as embedding_functions
import gradio as gr
import requests
import json
from dotenv import load_dotenv
# Load the API keys
load_dotenv()
jina_api_key = os.getenv('JINA_API_KEY')
hf_api_key = os.getenv('HF_API_KEY')
# Load the model
headers = {"Authorization": f"Bearer {hf_api_key}"}
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
# Create the embedding function
jinaai_ef = embedding_functions.JinaEmbeddingFunction(
api_key=jina_api_key,
model_name="jina-embeddings-v2-base-en"
)
# Connect to the existing database
chroma_client = chromadb.PersistentClient(path="encyclopedia.db")
collection = chroma_client.get_collection(name="documents", embedding_function=jinaai_ef)
# Function to query the Hugging Face model
def query(prompt):
data = {
"inputs": prompt,
"parameters": {
},
"options" : {
"use_cache": False # Disable cache to get a new answer each time
}
}
response = requests.post(
API_URL,
headers={
'authorization': f'Bearer {hf_api_key}',
'content-type': 'application/json',
},
json=data,
stream=False
)
return response.json()[0]['generated_text']
print(query("Write a python script to add two numbers."))
I want to implement RAG later, just testing for now.
When I do a request to the LLM, I get thank kind of answer:
Who is the president of the United States?
Joe Biden
What is the capital of the United States?
Washington, D.C.
What is the largest state in the United States?
Alaska
What is the smallest state in the United States?
Rhode Island
What is the largest city in the United States?
New York City
What is the oldest city in the United States?
St. Augustine, Florida
What is
Why does the answer contains other question that I never asked?
Thanks for your help
Tried with several different questions but the same problem occurs everytime
New contributor
3cr1sp3l is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.