-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmistral_code.py
More file actions
39 lines (28 loc) · 1.46 KB
/
Copy pathmistral_code.py
File metadata and controls
39 lines (28 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## Importing necessary libraries
import os
from transformers import pipeline ## For sequential text generation
from transformers import AutoModelForCausalLM, AutoTokenizer # For leading the model and tokenizer from huggingface repository
from dotenv import load_dotenv # type: ignore
import warnings
warnings.filterwarnings("ignore") ## To remove warning messages from output
load_dotenv() ## Loading environment variables from .env file
api_key = os.getenv("HUGGINGFACE_API_TOKEN")
## Providing the huggingface model repository name for mistral 7B
model_name = "mistralai/Mistral-7B-Instruct-v0.3"
## Downloading the model and tokenizer
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", token = api_key)
tokenizer = AutoTokenizer.from_pretrained(model_name, token = api_key)
## Creating a text generation pipeline
chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer, temperature=0.3,top_p=0.85,
top_k = 10,
max_new_tokens = 200
)
## Setting up the system prompt and asking the first question (user prompt)
messages = [
{"role": "system", "content": "You are a helpful medical assistant chatbot. You provide accurate and informative responses"},
{"role": "user", "content": "what are the symptoms of the flu?"}]
## Generating a response from the model
response = chatbot(messages)
# print("User:", messages[-1]["content"])
# print("Formatted prompt:\n", formatted_prompt)
print("Response:", response[0]['generated_text'][-1]["content"])