Reputation: 4575
I have the python code below. I'm using llama_index to create an index from a text corpus. I'm then submitting a query to the index to get a response. I'm getting the error below that it cannot import Chatcompletion from openai. It looks like llama_index is trying to import it from openai in some step. This code was working previously on another machine that was running llama-index 0.6.9, this machine is running 0.7.2. I'm guessing they changed something recently. Can anyone suggest how to fix this issue?
code:
# function to index corpus and get response
def index_response(api_key, text_path,query):
# api key you generate in your openai account
import os
# add your openai api key here
os.environ['OPENAI_API_KEY']=api_key
# Load your data into 'Documents' a custom type by llamaIndex
from llama_index import SimpleDiretoryReader, Document
with open(text_path,'r') as file:
text_history = file.read()
documents = [Document(text_history)]
from llama_index import GPTVectorStoreIndex
index = GPTVectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response=query_engine.query(query)
return response.response
# initial query
prompt="This is some text to help clarify my search. "
query1="Here is my question?"
prompt_submit=prompt+query1
# prompt_submit
response_string=index_response(api_key=apikey,
text_path='~/path/data/',
query=prompt_submit)
response_string
error:
ImportError: cannot import name 'ChatCompletion' from 'openai' (/home/user/anaconda3/envs/LLMenv/lib/python3.10/site-packages/openai/__init__.py)
Upvotes: 0
Views: 930
Reputation: 2836
You need to upgrade you openai library. Also, make sure after upgrade you are restart your code editor
pip install --upgrade openai
If you are using Jupyter notebook
pip uninstall openai
close kernel (shut down kernel)
open notebook
!pip install --upgrade openai
Upvotes: 1