import openai
from langchain_community.document_loaders import PyPDFLoader, TextLoader, WebBaseLoader

# Load documents
text_path = "test.txt"
pdf_path = "test.pdf"
web_url = "https://fa.wikipedia.org/wiki/پایتون_(زبان_برنامه‌نویسی)"
pdf_loader = PyPDFLoader(pdf_path)
text_loader = TextLoader(text_path)
web_loader = WebBaseLoader(web_url)
pdf_documents = pdf_loader.load()
text_documents = text_loader.load()
web_documents = web_loader.load()

documents = pdf_documents + text_documents + web_documents

# Set the OpenAI API key
openai.api_key = "<TRUFFLEHOG_REDACTED_OPENAI>"

# Dictionary to store conversation history for each user
conversations_history = {}

def ask_question(user_id, question, documents):
    # Retrieve the user’s conversation history if it exists
    if user_id in conversations_history:
        user_history = conversations_history[user_id]
    else:
        user_history = []

    # Add the previous context to the current question
    context = " ".join(["Q: " + q + " A: " + a for q, a in user_history])
    full_question = context + " Q: " + question

    from openai import OpenAI
    client = OpenAI(
        api_key= '<TRUFFLEHOG_REDACTED_OPENAI>'
    )
    completion = client.chat.completions.create(
    model="gpt-4o",
    messages = full_question,
    max_tokens = 4000
    )

    answer = completion.choices[0].message
    # Update the conversation history
    user_history.append((question, answer))
    conversations_history[user_id] = user_history

    return answer

# Example usage
user_id = "user_123"
question = "قیمت لحظه ای اتریوم چقدر است؟"
response = ask_question(user_id, question, text_documents)
print(response)

user_id = "user_123"
question = "قیمت حمایت و مقاومت این جفت ارز چقدر است؟"
response = ask_question(user_id, question, text_documents)
print(response)

این کد ارور زیر و میده 
File "/Users/milad-golshan/Desktop/ai_project/main copy 2.py", line 54, in <module>
    response = ask_question(user_id, question, text_documents)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/main copy 2.py", line 38, in ask_question
    completion = client.chat.completions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/.venv/lib/python3.11/site-packages/openai/_utils/_utils.py", line 277, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/.venv/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 643, in create
    return self._post(
           ^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1266, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 942, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/Users/milad-golshan/Desktop/ai_project/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1046, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid type for 'messages': expected an array of objects, but got a string instead.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'invalid_type'}}