Spaces:
Running
Running
File size: 1,347 Bytes
fecab9d |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
""" llm_utils.py
Utilities for working with Large Language Models
:author: Didier Guillevic
:email: didier@guillevic.net
:creation: 2024-12-28
"""
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
import os
from mistralai import Mistral
#
# Mistral AI client
#
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model_id = "mistral-large-latest" # 128k context window
#
# Some functions
#
def generate_chat_response_streaming(
query: str,
context: str,
max_new_tokens=1_024,
temperature=0.0
):
"""
"""
# Instruction
instruction = (
f"You will be given a question and list of context that might "
f"be relevant to the question. "
f"Do not include facts not contained in the provided context. "
f"If no such relecant context provided to answer the question, "
f"then soimply say so. Do not invent anything.\n\n"
f"Question: {query}\n\n\n"
f"Context:\n\n{context}"
)
# messages
messages = []
messages.append({'role': 'user', 'content': instruction})
#logger.info(messages)
# Yield the model response as the tokens are being generated
stream_reponse = client.chat.stream(model=model_id, messages=messages)
return stream_reponse
|