|
|
|
"""Chatbot using Python Project.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1tNZAGbjtsEUqIylR9_nzpBLhj22zIWJy |
|
""" |
|
|
|
|
|
|
|
API_KEY = "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" |
|
|
|
import requests |
|
|
|
def chat_with_mistral(user_input): |
|
url = "https://openrouter.ai/api/v1/chat/completions" |
|
|
|
headers = { |
|
"Authorization": f"Bearer {API_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
data = { |
|
"model": "mistralai/mistral-small-24b-instruct-2501:free", |
|
"messages": [{"role": "user", "content": user_input}] |
|
} |
|
|
|
response = requests.post(url, json=data, headers=headers) |
|
|
|
if response.status_code == 200: |
|
return response.json()["choices"][0]["message"]["content"] |
|
else: |
|
return f"Error: {response.status_code} - {response.text}" |
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
def mistral_chatbot(user_input): |
|
return chat_with_mistral(user_input) |
|
|
|
|
|
chatbot_ui = gr.Interface( |
|
fn=mistral_chatbot, |
|
inputs="text", |
|
outputs="text", |
|
title="Mistral AI Chatbot", |
|
description="Chat with an AI-powered assistant using Mistral 7B." |
|
) |
|
|
|
|
|
chatbot_ui.launch() |
|
|
|
chat_history = [] |
|
|
|
def chat_with_mistral_context(user_input): |
|
global chat_history |
|
|
|
url = "https://api.mistral.ai/v1/chat/completions" |
|
headers = { |
|
"Authorization": f"Bearer {API_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
chat_history.append({"role": "user", "content": user_input}) |
|
|
|
data = { |
|
"model": "mistralai/mistral-small-24b-instruct-2501:free", |
|
"messages": chat_history |
|
} |
|
|
|
response = requests.post(url, json=data, headers=headers) |
|
|
|
if response.status_code == 200: |
|
bot_response = response.json()["choices"][0]["message"]["content"] |
|
chat_history.append({"role": "assistant", "content": bot_response}) |
|
return bot_response |
|
else: |
|
return f"Error: {response.status_code} - {response.text}" |