{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Answer: How can I assist you today?\n" ] } ], "source": [ "# from langchain.llms import openai\n", "import openai\n", "from dotenv import load_dotenv\n", "import os\n", "load_dotenv()\n", "\n", "import streamlit as st\n", "\n", "openai.api_key = os.environ[\"OPEN_API_KEY\"]\n", "\n", "# def get_openai_response(question):\n", "# llm = openai()\n", "\n", "# Function to ask a question using the ChatGPT model\n", "def ask_question(question):\n", " try:\n", " response = openai.ChatCompletion.create(\n", " model=\"ft:gpt-3.5-turbo-0125:shubham-gupta::AqKlxnFo\", # Replace with your fine-tuned chat model if applicable\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": question}],\n", " max_tokens=100,\n", " temperature=0.5\n", " )\n", " # Extract assistant's response and add to chat history\n", " assistant_message = response[\"choices\"][0][\"message\"][\"content\"]\n", " return assistant_message\n", " except openai.OpenAIError as e: # Handle API-related errors\n", " return f\"An OpenAI API error occurred: {e}\"\n", " except Exception as e: # Handle other unexpected errors\n", " return f\"An unexpected error occurred: {e}\"\n", "\n", "# Example usage\n", "if __name__ == \"__main__\":\n", " user_question = input(\"Ask a question: \")\n", " answer = ask_question(user_question)\n", " print(f\"Answer: {answer}\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 3\n", "\n", "# Instead of downgrading pydantic, consider upgrading langchain or llama-index. For example, the error in the title doesn't occur with pydantic==2.8.2 and llama_index.core==0.10.67. Similar errors involving langchain also doesn't occur for newer versions of langchain, e.g. langchain>=0.3.0 and langchain-core>=0.3.0.1\n", "\n", "# So pip install langchain langchain-core -U should really solve the issue." ] } ], "metadata": { "kernelspec": { "display_name": "env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }