date_collected
stringclasses
1 value
repo_name
stringlengths
6
116
file_name
stringlengths
2
220
file_contents
stringlengths
13
357k
prompts
sequence
2024-01-10
il-katta/mIA
utils~chat_tools~loader.py
import json from typing import Optional, Dict, List, Union import faiss from langchain.cache import SQLiteCache from langchain.docstore.base import Docstore, AddableMixin from langchain.embeddings import OpenAIEmbeddings from langchain.schema import Document from langchain.schema.embeddings import Embeddings from langchain.vectorstores import Chroma, FAISS from pydantic.v1 import PrivateAttr from sqlalchemy import Column, String, Engine, create_engine, select from sqlalchemy.orm import declarative_base, Session import config def load_chroma_vectorstore(embedding: Optional[Embeddings] = None) -> Chroma: if embedding is None: embedding = OpenAIEmbeddings() persist_directory = config.DATA_DIR / "vectorstore" / "uploaded_files" if not persist_directory.exists(): persist_directory.mkdir(parents=True) return Chroma( embedding_function=embedding, collection_name="uploaded_files", persist_directory=str(persist_directory) ) Base = declarative_base() class DocStoreModel(Base): # type: ignore __tablename__ = "my_docstore" key = Column(String, primary_key=True) document = Column(String) class MyDockstore(Docstore, AddableMixin): _cache: SQLiteCache = PrivateAttr() def __init__(self, engine: Optional[Engine] = None): if engine is None: dbpath = config.DATA_DIR / "docstore" / "mydocstore.db" if not dbpath.parent.exists(): dbpath.parent.mkdir(parents=True) engine = create_engine(f"sqlite:///{dbpath}") self.engine = engine DocStoreModel.metadata.create_all(self.engine) def add(self, texts: Dict[str, Document]) -> None: items = [ DocStoreModel(key=key, document=json.dumps(doc.dict())) for key, doc in texts.items() ] with Session(self.engine) as session, session.begin(): for item in items: session.merge(item) session.commit() def delete(self, ids: List) -> None: with Session(self.engine) as session: for _id in ids: session.query(DocStoreModel).filter_by(key=_id).delete() session.commit() def search(self, search: str) -> Union[str, Document]: stmt = ( select(DocStoreModel.key).select(DocStoreModel.document) .where(DocStoreModel.key == search) # type: ignore .limit(1) ) with Session(self.engine) as session: result = session.execute(stmt).first() if result is None: return f"ID {search} not found." else: return Document(**json.loads(result[1])) def get_keys(self) -> List[str]: stmt = select(DocStoreModel.key) with Session(self.engine) as session: result = session.execute(stmt).fetchall() return [r[0] for r in result] def __len__(self): with Session(self.engine) as session: return session.query(DocStoreModel).count() EMBEDDING_SIZE = 1536 def load_faiss_vectorstore(embedding: Optional[Embeddings] = None) -> FAISS: if embedding is None: embedding = OpenAIEmbeddings() docstore = MyDockstore() return FAISS( embedding_function=embedding.embed_query, index=faiss.IndexFlatL2(EMBEDDING_SIZE), docstore=docstore, index_to_docstore_id={ i: key for i, key in enumerate(docstore.get_keys()) }, )
[]
2024-01-10
iinux/JohannCarlFriedrichGauss
py~chatgpt~pub.py
# -*- coding: utf-8 -*- import os import time import redis import openai import requests import my_config import SparkApi import sensenova_service from flask import Flask, request, abort, render_template from wechatpy import parse_message, create_reply from wechatpy.utils import check_signature from wechatpy.exceptions import ( InvalidSignatureException, InvalidAppIdException, ) # from bs4 import BeautifulSoup # set token or get from environments TOKEN = os.getenv('WECHAT_TOKEN', my_config.wechat_token) AES_KEY = os.getenv('WECHAT_AES_KEY', my_config.wechat_aes_key) APPID = os.getenv('WECHAT_APPID', my_config.wechat_app_id) THINKING = 'THINKING' # DEFAULT_MODEL = 'text-davinci-003' DEFAULT_MODEL = 'gpt-3.5-turbo' USER_MODEL_CACHE_PREFIX = 'user_model_' CMD_LIST_MODEL = 'list model' CMD_GET_MODEL = 'get model' CMD_SET_MODEL = 'set model ' DEFAULT_SYS = 'xf' USER_SYS_CACHE_PREFIX = 'user_sys_' CMD_GET_SYS = 'get sys' CMD_SET_SYS = 'set sys ' CMD_CALL_API = 'call api ' openai.api_key = my_config.key_openai def chat(msg): sys = get_user_sys(msg.source) if sys == 'chatgpt': return chat_with_gpt3(msg) elif sys == 'writesonic': return chat_with_writesonic(msg) elif sys == 'xf': return chat_with_xf(msg) elif sys == 'sensenova': user_model = get_user_model(msg.source) return sensenova_service.ask_from_wechat(msg, user_model) else: return 'unknown sys' def get_default_model(sys): if sys == 'sensenova': return sensenova_service.get_default_model() else: return DEFAULT_MODEL def get_model_list(sys): if sys == 'sensenova': return sensenova_service.get_model_list() else: return [x.id for x in openai.Model.list().get('data')] def chat_with_xf(msg): # https://www.xfyun.cn/doc/spark/Web.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E # ็”จไบŽ้…็ฝฎๅคงๆจกๅž‹็‰ˆๆœฌ๏ผŒ้ป˜่ฎคโ€œgeneral/generalv2โ€ # domain = "general" # v1.5็‰ˆๆœฌ domain = "generalv2" # v2.0็‰ˆๆœฌ # ไบ‘็ซฏ็Žฏๅขƒ็š„ๆœๅŠกๅœฐๅ€ # Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat" # v1.5็Žฏๅขƒ็š„ๅœฐๅ€ Spark_url = "ws://spark-api.xf-yun.com/v2.1/chat" # v2.0็Žฏๅขƒ็š„ๅœฐๅ€ SparkApi.answer = "" text = [] jsoncon = {} jsoncon["role"] = "user" jsoncon["content"] = msg.content text.append(jsoncon) SparkApi.main(my_config.xf_appid, my_config.xf_api_key, my_config.xf_api_secret, Spark_url, domain, text) return SparkApi.answer def chat_with_writesonic(msg): url = 'https://api.writesonic.com/v2/business/content/chatsonic?engine=premium' res = requests.post(url, json={ 'enable_google_results': 'false', 'enable_memory': 'false', 'input_text': msg.content, }, headers={ "X-API-KEY": my_config.writesonic_api_key, 'accept': 'application/json', 'content-type': 'application/json', }) print(res) return res.json()['message'] def chat_with_gpt3(msg): user_model = get_user_model(msg.source) if user_model in ['gpt-3.5-turbo']: response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=[{'role': 'user', 'content': msg.content}], max_tokens=2048, n=1, stop=None, temperature=.9, user=msg.source, ) message = response.choices[0].message.content else: response = openai.Completion.create( engine=user_model, prompt=msg.content, max_tokens=2048, n=1, stop=None, temperature=0.5, user=msg.source, ) message = response.choices[0].text print(response) return message def get_redis(): r = redis.Redis(host='localhost', port=6379, decode_responses=True) return r def set_user_model(user, model): r = get_redis() if model == 'default': model = get_default_model(get_user_sys(user)) r.set(USER_MODEL_CACHE_PREFIX + user, model) r.close() def get_user_model(user): r = get_redis() user_model = r.get(USER_MODEL_CACHE_PREFIX + user) r.close() user_sys = get_user_sys(user) if user_model is None: user_model = get_default_model(user_sys) if user_model not in get_model_list(user_sys): user_model = get_default_model(user_sys) return user_model def set_user_sys(user, sys): r = get_redis() if sys == 'default': sys = DEFAULT_SYS r.set(USER_SYS_CACHE_PREFIX + user, sys) r.close() def get_user_sys(user): r = get_redis() user_sys = r.get(USER_SYS_CACHE_PREFIX + user) r.close() if user_sys is None: user_sys = DEFAULT_SYS return user_sys def ask(msg, allow_cache=True): r = get_redis() key = 'chatgpt_answer_' + msg.content answer_cache = r.get(key) if answer_cache == THINKING and allow_cache: print('get THINKING') reply_content = 'LOOP' read_times = 10 while read_times > 0: read_times -= 1 time.sleep(1) answer_cache = r.get(key) if answer_cache is None: reply_content = 'loop result fail, try again' break elif answer_cache != THINKING: print('GOT') reply_content = answer_cache break reply_content += '[L]' elif answer_cache and allow_cache: print('answer from cache') reply_content = answer_cache reply_content += '[C]' else: print('call api') r.set(key, THINKING, 300) try: time_start = time.time() reply_content = chat(msg).lstrip('?๏ผŸ').strip() time_end = time.time() reply_content += '\n(time cost %.3f s)' % (time_end - time_start) r.set(key, reply_content, 28800) except openai.error.APIConnectionError as e: print(e) reply_content = 'call openapi fail, try again ' + e.__str__() r.delete(key) reply_content += '[I]' return reply_content app = Flask(__name__) @app.route('/') def index(): host = request.url_root return render_template('index.html', host=host) @app.route('/wechat', methods=['GET', 'POST']) def wechat(): signature = request.args.get('signature', '') timestamp = request.args.get('timestamp', '') nonce = request.args.get('nonce', '') encrypt_type = request.args.get('encrypt_type', 'raw') msg_signature = request.args.get('msg_signature', '') try: check_signature(TOKEN, signature, timestamp, nonce) except InvalidSignatureException: abort(403) if request.method == 'GET': echo_str = request.args.get('echostr', '') return echo_str # POST request if encrypt_type == 'raw': # plaintext mode msg = parse_message(request.data) print(msg) if msg.type == 'text': reply_content = '' if msg.content == CMD_LIST_MODEL: user_sys = get_user_sys(msg.source) reply_content = '\n'.join(get_model_list(user_sys)) elif msg.content.startswith(CMD_SET_MODEL): user_model = msg.content[len(CMD_SET_MODEL):] set_user_model(msg.source, user_model) reply_content = 'set user model success' elif msg.content == CMD_GET_MODEL: reply_content = get_user_model(msg.source) elif msg.content.startswith(CMD_SET_SYS): user_sys = msg.content[len(CMD_SET_SYS):] set_user_sys(msg.source, user_sys) reply_content = 'set user sys success' elif msg.content == CMD_GET_SYS: reply_content = get_user_sys(msg.source) elif msg.content.startswith(CMD_CALL_API): reply_content = ask(msg.content[len(CMD_CALL_API):], allow_cache=False) else: reply_content = ask(msg) print('ask {} response {}'.format(msg.content, reply_content)) reply = create_reply(reply_content, msg) else: reply = create_reply('Sorry, can not handle this for now', msg) return reply.render() else: # encryption mode from wechatpy.crypto import WeChatCrypto crypto = WeChatCrypto(TOKEN, AES_KEY, APPID) try: msg = crypto.decrypt_message(request.data, msg_signature, timestamp, nonce) except (InvalidSignatureException, InvalidAppIdException): abort(403) else: msg = parse_message(msg) if msg.type == 'text': reply = create_reply(msg.content, msg) else: reply = create_reply('Sorry, can not handle this for now', msg) return crypto.encrypt_message(reply.render(), nonce, timestamp) if __name__ == '__main__': app.run('127.0.0.1', 8081, debug=False)
[ "application/json" ]
2024-01-10
Valentina-Frenkel/llama_index
llama_index~llms~azure_openai.py
from typing import Any, Dict, Optional from llama_index.bridge.pydantic import Field, root_validator from llama_index.callbacks import CallbackManager from llama_index.llms.openai import OpenAI AZURE_OPENAI_API_TYPE = "azure" class AzureOpenAI(OpenAI): """ Azure OpenAI To use this, you must first deploy a model on Azure OpenAI. Unlike OpenAI, you need to specify a `engine` parameter to identify your deployment (called "model deployment name" in Azure portal). - model: Name of the model (e.g. `text-davinci-003`) This in only used to decide completion vs. chat endpoint. - engine: This will correspond to the custom name you chose for your deployment when you deployed a model. You must have the following environment variables set: - `OPENAI_API_TYPE`: set this to `azure`, `azure_ad`, or `azuread` - `OPENAI_API_VERSION`: set this to `2023-05-15` This may change in the future. - `OPENAI_API_BASE`: your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/ - `OPENAI_API_KEY`: your API key More information can be found here: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?tabs=command-line&pivots=programming-language-python """ engine: str = Field(description="The name of the deployed azure engine.") def __init__( self, model: str = "gpt-35-turbo", engine: Optional[str] = None, temperature: float = 0.1, max_tokens: Optional[int] = None, additional_kwargs: Optional[Dict[str, Any]] = None, max_retries: int = 10, api_key: Optional[str] = None, api_type: Optional[str] = AZURE_OPENAI_API_TYPE, api_base: Optional[str] = None, api_version: Optional[str] = None, callback_manager: Optional[CallbackManager] = None, **kwargs: Any, ) -> None: if engine is None: raise ValueError("You must specify an `engine` parameter.") super().__init__( engine=engine, model=model, temperature=temperature, max_tokens=max_tokens, additional_kwargs=additional_kwargs, max_retries=max_retries, api_key=api_key, api_base=api_base, api_type=api_type, api_version=api_version, callback_manager=callback_manager, **kwargs, ) @root_validator def validate_env(cls, values: Dict[str, Any]) -> Dict[str, Any]: """Validate necessary credentials are set.""" if values["api_base"] == "https://api.openai.com/v1": raise ValueError( "You must set OPENAI_API_BASE to your Azure endpoint. " "It should look like https://YOUR_RESOURCE_NAME.openai.azure.com/" ) if values["api_type"] not in ("azure", "azure_ad", "azuread"): raise ValueError( "You must set OPENAI_API_TYPE to one of " "(`azure`, `azuread`, `azure_ad`) for Azure OpenAI." ) if values["api_version"] is None: raise ValueError("You must set OPENAI_API_VERSION for Azure OpenAI.") return values @property def _model_kwargs(self) -> Dict[str, Any]: model_kwargs = super()._model_kwargs model_kwargs.pop("model") model_kwargs["engine"] = self.engine return model_kwargs @classmethod def class_name(cls) -> str: """Get class name.""" return "azure_openai_llm"
[]
2024-01-10
abhinandarun-02/Intelli-Store
backend~server.py
import os from dotenv import load_dotenv from flask import Flask, request, jsonify from pymongo import MongoClient from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory load_dotenv() client = OpenAI() app = Flask(__name__) client = MongoClient("mongodb+srv://prabath:[email protected]/") db = client["mydb"] users_collection = db["users"] products_collection = db["products"] fashion_template = PromptTemplate(input_variables=['input'],template='You are an assistant suggesting helpful eco-friendly fashion tips. Suggest some fashion tips for "{input}"') fashion_llm = OpenAI(temperature=0.9) fashion_memory = ConversationBufferMemory(input_key='input',memory_key='chat_history') fashion_suggestion = LLMChain(llm=fashion_llm, prompt=fashion_template, memory=fashion_memory, verbose=True) keywords_template = PromptTemplate(input_variables=["content"],template='Suggest a curated set of keywords in python programming language array format based on "{content}". The system should analyze the input and recommend relevant terms from the following set only: [mens shirt, casual shirt, black, full-sleeve shirt, medium size, regular fit, blue shirt, pants, trousers, blue fit, slim fit, beige trousers, shirt, jacket, sports jacket, gym shoes, shoes, black and white, Skirt, black skirt, womens wear, black watch, metal strap, analog watch, watches, red, vest, beach wear, red vest, mens wear, digital watch, rubber strap]. If not found anything relatable, return "Keyword not matching in store"') keyword_llm = OpenAI() keyword_memory = ConversationBufferMemory(input_key="content",memory_key='keyword_history') keywords_suggestion = LLMChain(llm=keyword_llm, prompt=keywords_template, memory=keyword_memory, verbose=True) def ask(prompt): return fashion_suggestion.run(prompt) def generate_keywords(prompt): return keywords_suggestion.run(prompt['msg']) @app.route('/ask', methods=['GET']) # {msg:'input'} def get_bot_response(): data = request.get_json() response = ask(data['msg']) print(fashion_memory.buffer) return response @app.route('/generate', methods=['GET']) def get_keywords(): data = request.get_json() response = generate_keywords(data) print(keyword_memory.buffer) return response @app.route('/register', methods=['POST']) def register_user(): data = request.get_json() if 'username' not in data or 'password' not in data: return jsonify({'error': 'Username and password are required'}), 400 existing_user = users_collection.find_one({'username': data['username']}) if existing_user: return jsonify({'error': 'Username already exists'}), 400 new_user = { 'username': data['username'], 'password': data['password'] } users_collection.insert_one(new_user) return jsonify({'message': 'User registered successfully'}), 201 @app.route('/products', methods=['GET']) # /products GET def get_products(): products = list(db.products.find({}, {'_id': 0})) if products: return jsonify({'products': products}) else: return jsonify({'message': 'No products found'}), 404 @app.route('/createProduct', methods=['POST']) def create_product(): data = request.get_json() if 'name' not in data or 'description' not in data or 'price' not in data: return jsonify({'error': 'Name, description and price are required'}), 400 existing_product = products_collection.find_one({'name': data['name']}) if existing_product: return jsonify({'error': 'Product already exists'}), 400 new_product = { 'name': data['name'], 'description': data['description'], 'price': data['price'] } products_collection.insert_one(new_product) return jsonify({'message': 'Product added successfully'}), 201 @app.route('/deleteProduct', methods=['POST']) def delete_product(): data = request.get_json() if 'name' not in data: return jsonify({'error': 'Name, description and price are required'}), 400 criteria={'name': data['name']} result = db.products.delete_one(criteria) if result.deleted_count ==1: return jsonify({'msg': 'Product deleted successfully'}), 200 else: return jsonify({'error': 'Failed to delete product'}), 500 @app.route('/product', methods=['GET']) # /product {name} def search_product(): product_name = request.args.get('name') if not product_name: return jsonify({'error': 'Product name is required in the query parameters'}), 400 query = {'name': {'$regex': f'.*{product_name}.*', '$options': 'i'}} matching_products = list(db.products.find(query, {'_id': 0})) if matching_products: return jsonify({'matching_products': matching_products}), else: return jsonify({'message': 'No matching products found'}), 404 @app.route('/updateProduct', methods=['PUT']) def update_product(): data = request.get_json() if 'name' not in data: return jsonify({'error': 'Product name is required for updating'}), 400 existing_product = products_collection.find_one({'name': data['name']}) if not existing_product: return jsonify({'error': 'Product not found'}), 404 update_fields = {} if 'new_name' in data: update_fields['name'] = data['new_name'] if 'price' in data: update_fields['price'] = data['price'] if 'description' in data: update_fields['description'] = data['description'] products_collection.update_one( {'name': data['name']}, {'$set': update_fields} ) return jsonify({'message': 'Product updated successfully'}), 200 if __name__ == "__main__": app.run(debug=True)
[ "Suggest a curated set of keywords in python programming language array format based on \"{content}\". The system should analyze the input and recommend relevant terms from the following set only: [mens shirt, casual shirt, black, full-sleeve shirt, medium size, regular fit, blue shirt, pants, trousers, blue fit, slim fit, beige trousers, shirt, jacket, sports jacket, gym shoes, shoes, black and white, Skirt, black skirt, womens wear, black watch, metal strap, analog watch, watches, red, vest, beach wear, red vest, mens wear, digital watch, rubber strap]. If not found anything relatable, return \"Keyword not matching in store\"", "content", "input", "You are an assistant suggesting helpful eco-friendly fashion tips. Suggest some fashion tips for \"{input}\"" ]
2024-01-10
EthanV1920/ENGR-220-MatLab
FinalProject~matLabGPT.py
import openai import configparser config = configparser.ConfigParser() config.read('/Users/ethanvosburg/Documents/git/ENGR-220-MatLab/FinalProject/env.ini') def getResponse(prompt, name, probNum, assign, toggle): openai.api_key = config['KEYS']['OPENAI_API_KEY'] if toggle == "PatGPT": decorationText = "Write a matlab script to solve the following problem:" promptIn = decorationText + prompt else: promptIn = prompt promptIn = promptIn.replace("\n", "") completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": promptIn}]) reply = completion.choices[0].message.content # decorCode = open(r"DecoratedCode.txt", "w+") if toggle == "PatGPT": appendStr = "%% Assignment " + str(assign) + " ENGR 220\n% " + name + "\n%% Problem #" + str(probNum) + "\n% " genCode = open(r"GeneratedCode.m", "w+") genCode.write(appendStr) genCode = open(r"GeneratedCode.m", "a") genCode.write(prompt) genCode.write(reply) genCode.close() toMatLab = appendStr + prompt + reply else: toMatLab = reply return toMatLab reply = getResponse(promptIn, nameIn, probNumIn, assignIn, toggleIn)
[ "Write a matlab script to solve the following problem:PLACEHOLDER" ]
2024-01-10
EthanV1920/ENGR-220-MatLab
FinalProject~Reference~chatgpt-api-youtube-main~01-chatgpt-simple.py
import openai import configparser config = configparser.ConfigParser() config.read('/Users/ethanvosburg/Documents/git/ENGR-220-MatLab/FinalProject/env.ini') def getResponse(prompt, name, probNum, assign, toggle): openai.api_key = config['KEYS']['OPENAI_API_KEY'] if toggle == "PatGPT": decorationText = "Write a matlab script to solve the following problem:" promptIn = decorationText + prompt else: promptIn = prompt promptIn = promptIn.replace("\n", "") completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": promptIn}]) reply = completion.choices[0].message.content # decorCode = open(r"DecoratedCode.txt", "w+") if toggle == "PatGPT": appendStr = "%% Assignment " + str(assign) + " ENGR 220\n% " + name + "\n%% Problem #" + str(probNum) + "\n% " genCode = open(r"GeneratedCode.m", "w+") genCode.write(appendStr) genCode = open(r"GeneratedCode.m", "a") genCode.write(prompt) genCode.write(reply) genCode.close() toMatLab = appendStr + prompt + reply else: toMatLab = reply return toMatLab reply = getResponse(promptIn, nameIn, probNumIn, assignIn, toggleIn)
[ "Write a matlab script to solve the following problem:PLACEHOLDER" ]
2024-01-10
stormaker/AIcoding101
W0~new_v.py
import os from dotenv import load_dotenv from openai import OpenAI from langchain.text_splitter import RecursiveCharacterTextSplitter from tqdm import tqdm load_dotenv() openai_api = os.getenv("OPENAI_API_KEY") # Step 1: Read the text file with open("input.txt", "r", encoding='utf-8') as f: text = f.read() text_splitter = RecursiveCharacterTextSplitter( chunk_size=1100, chunk_overlap=20, length_function=len, is_separator_regex=False, ) chunks = text_splitter.split_text(text) client = OpenAI() def summarize_text(text): response = client.chat.completions.create( model="gpt-4-1106-preview", messages=[ { "role": "assistant", "content": f"่ฏทๅฐ†ๆˆ‘ๅ‘้€็š„่ฏญ้Ÿณ็จฟๆ•ด็†ไธบๆญฃๅผ็š„ไนฆ้ข่ฏญ๏ผŒ่ฆๆฑ‚" f"-ไฟ็•™ๅฏน่ฏไธญ็š„ๆฏไธ€ไธช็ป†่Š‚๏ผŒไธๆ”นๅ˜ๅŽŸๆ–‡ๅซไน‰๏ผ›" f"-ๅฐฝๅฏ่ƒฝๅœฐไฟ็•™ๅŽŸ่ฏ็š„็”จ่ฏใ€่ฏ่ฏญ้ฃŽๆ ผ๏ผ›" f"-่ฏทไฟฎๆ”น้”™ๅˆซๅญ—๏ผŒ็ฌฆๅˆไธญๆ–‡่ฏญๆณ•่ง„่Œƒใ€‚" f"-ๅŽปๆމ่ฏด่ฏไบบๅ’Œๆ—ถ้—ดๆˆณใ€‚:{text}" } ], temperature=0, max_tokens=4000, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].message.content print("Summarizing the text...\n") summaries = [] with tqdm(total=len(chunks)) as pbar: for i, chunk in enumerate(chunks): summary = summarize_text(chunk) summaries.append(summary) # Save each summary chunk in a separate text file with open(f'summary_chunk_{i+1}.txt', 'w', encoding='utf-8') as file: file.write(summary) pbar.update(1) # Combine the summary chunk files into one text file with open('summary_output.txt', 'w', encoding='utf-8') as file: for i, summary in enumerate(summaries): # file.write(f'Summary Chunk {i+1}:\n\n') file.write(summary + '\n\n') print("Summarization complete. Check the summary_output.txt file.")
[ "่ฏทๅฐ†ๆˆ‘ๅ‘้€็š„่ฏญ้Ÿณ็จฟๆ•ด็†ไธบๆญฃๅผ็š„ไนฆ้ข่ฏญ๏ผŒ่ฆๆฑ‚-ไฟ็•™ๅฏน่ฏไธญ็š„ๆฏไธ€ไธช็ป†่Š‚๏ผŒไธๆ”นๅ˜ๅŽŸๆ–‡ๅซไน‰๏ผ›-ๅฐฝๅฏ่ƒฝๅœฐไฟ็•™ๅŽŸ่ฏ็š„็”จ่ฏใ€่ฏ่ฏญ้ฃŽๆ ผ๏ผ›-่ฏทไฟฎๆ”น้”™ๅˆซๅญ—๏ผŒ็ฌฆๅˆไธญๆ–‡่ฏญๆณ•่ง„่Œƒใ€‚-ๅŽปๆމ่ฏด่ฏไบบๅ’Œๆ—ถ้—ดๆˆณใ€‚:PLACEHOLDER" ]
2024-01-10
stormaker/AIcoding101
W0~new.py
import os from dotenv import load_dotenv from openai import OpenAI from langchain.text_splitter import RecursiveCharacterTextSplitter load_dotenv() openai_api = os.getenv("OPENAI_API_KEY") # Step 1: Read the text file with open("input.txt", "r", encoding='utf-8') as f: text = f.read() text_splitter = RecursiveCharacterTextSplitter( chunk_size = 1100, chunk_overlap = 20, length_function = len, is_separator_regex = False, ) chunks = text_splitter.split_text(text) #print(chunks[0]) client = OpenAI() def summarize_text(text): response = client.chat.completions.create( model="gpt-4-1106-preview", messages=[ { "role": "assistant", "content": f"ไฝ ๆ˜ฏไผš่ฎฎ่ฎฐๅฝ•ๆ•ด็†ไบบๅ‘˜๏ผŒไปฅไธ‹ๆ˜ฏไธ€ๆฎตๅฝ•้Ÿณ็š„้€ๅญ—็จฟ๏ผŒ่ฏท้€ๅญ—ๅฐ†ๅ…ถๆ•ด็†ๆˆๅ‰ๅŽ่ฟž่ดฏ็š„ๆ–‡ๅญ—๏ผŒ้œ€่ฆๆณจๆ„๏ผš1.ไฟ็•™ๅฎŒๆ•ดไฟ็•™ๅŽŸๅง‹ๅฝ•้Ÿณ็š„ๆ‰€ๆœ‰็ป†่Š‚ใ€‚2.ๅฐฝ้‡ไฟ็•™ๅŽŸๆ–‡่ฏญไน‰ใ€่ฏญๆ„Ÿใ€‚3.่ฏทไฟฎๆ”น้”™ๅˆซๅญ—๏ผŒ็ฌฆๅˆไธญๆ–‡่ฏญๆณ•่ง„่Œƒใ€‚4.ๅŽปๆމ่ฏด่ฏไบบๅ’Œๆ—ถ้—ดๆˆณใ€‚5.็ฌฌไธ€ไบบ็งฐ๏ผšๆˆ‘ใ€‚6.่ฏท่ถณๅคŸ่ฏฆ็ป†๏ผŒๅญ—ๆ•ฐ่ถŠๅคš่ถŠๅฅฝใ€‚7.ไฟๆŒๅŽŸๅง‹ๅฝ•้Ÿณ้€ๅญ—็จฟ็š„่ฏญ่จ€้ฃŽๆ ผ:{text}" } ], temperature=0, max_tokens=4000, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].message.content print("Summarizing the text...") summary = [] summaries = [summarize_text(chunk) for chunk in chunks] # Step 4: Combine the result from the summarize chain and save them as a new text file with open('summary_output.txt', 'w', encoding='utf-8') as file: for summary in summaries: file.write(summary + '\n\n') print("Summarization complete. Check the summary_output.txt file.")
[ "ไฝ ๆ˜ฏไผš่ฎฎ่ฎฐๅฝ•ๆ•ด็†ไบบๅ‘˜๏ผŒไปฅไธ‹ๆ˜ฏไธ€ๆฎตๅฝ•้Ÿณ็š„้€ๅญ—็จฟ๏ผŒ่ฏท้€ๅญ—ๅฐ†ๅ…ถๆ•ด็†ๆˆๅ‰ๅŽ่ฟž่ดฏ็š„ๆ–‡ๅญ—๏ผŒ้œ€่ฆๆณจๆ„๏ผš1.ไฟ็•™ๅฎŒๆ•ดไฟ็•™ๅŽŸๅง‹ๅฝ•้Ÿณ็š„ๆ‰€ๆœ‰็ป†่Š‚ใ€‚2.ๅฐฝ้‡ไฟ็•™ๅŽŸๆ–‡่ฏญไน‰ใ€่ฏญๆ„Ÿใ€‚3.่ฏทไฟฎๆ”น้”™ๅˆซๅญ—๏ผŒ็ฌฆๅˆไธญๆ–‡่ฏญๆณ•่ง„่Œƒใ€‚4.ๅŽปๆމ่ฏด่ฏไบบๅ’Œๆ—ถ้—ดๆˆณใ€‚5.็ฌฌไธ€ไบบ็งฐ๏ผšๆˆ‘ใ€‚6.่ฏท่ถณๅคŸ่ฏฆ็ป†๏ผŒๅญ—ๆ•ฐ่ถŠๅคš่ถŠๅฅฝใ€‚7.ไฟๆŒๅŽŸๅง‹ๅฝ•้Ÿณ้€ๅญ—็จฟ็š„่ฏญ่จ€้ฃŽๆ ผ:PLACEHOLDER" ]
2024-01-10
YAIxPOZAlabs/MuseDiffusion
MuseDiffusion~utils~logger.py
# Logger copied from OpenAI baselines to avoid extra RL-based dependencies: # https://github.com/openai/baselines/blob/ea25b9e8b234e6ee1bca43083f8f3cf974143998/baselines/logger.py from abc import abstractmethod, ABC import os import sys import json import time import datetime import tempfile import warnings from collections import defaultdict from contextlib import contextmanager try: import wandb except ModuleNotFoundError: class wandb: def log(self, *args, **kwargs): pass DEBUG = 10 INFO = 20 WARN = 30 ERROR = 40 DISABLED = 50 class KVWriter(ABC): @abstractmethod def writekvs(self, kvs): raise NotImplementedError class SeqWriter(ABC): @abstractmethod def writeseq(self, seq): raise NotImplementedError class HumanOutputFormat(KVWriter, SeqWriter): def __init__(self, filename_or_file): if isinstance(filename_or_file, str): self.file = open(filename_or_file, "wt") self.own_file = True else: assert hasattr(filename_or_file, "read"), ( "expected file or str, got %s" % filename_or_file ) self.file = filename_or_file self.own_file = False def writekvs(self, kvs): # Create strings for printing key2str = {} for (key, val) in sorted(kvs.items()): if hasattr(val, "__float__"): valstr = "%-8.3g" % val else: valstr = str(val) key2str[self._truncate(key)] = self._truncate(valstr) # Find max widths if len(key2str) == 0: print("WARNING: tried to write empty key-value dict") return else: keywidth = max(map(len, key2str.keys())) valwidth = max(map(len, key2str.values())) # Write out the data dashes = "-" * (keywidth + valwidth + 7) lines = [dashes] for (key, val) in sorted(key2str.items(), key=lambda kv: kv[0].lower()): lines.append( "| %s%s | %s%s |" % (key, " " * (keywidth - len(key)), val, " " * (valwidth - len(val))) ) lines.append(dashes) self.file.write("\n".join(lines) + "\n") # Flush the output to the file self.file.flush() @staticmethod def _truncate(s): maxlen = 30 return s[: maxlen - 3] + "..." if len(s) > maxlen else s def writeseq(self, seq): seq = list(seq) for (i, elem) in enumerate(seq): self.file.write(elem) if i < len(seq) - 1: # add space unless this is the last one self.file.write(" ") self.file.write("\n") self.file.flush() def close(self): if self.own_file: self.file.close() class JSONOutputFormat(KVWriter): def __init__(self, filename): self.file = open(filename, "wt") def writekvs(self, kvs): for k, v in sorted(kvs.items()): if hasattr(v, "dtype"): kvs[k] = float(v) self.file.write(json.dumps(kvs) + "\n") self.file.flush() def close(self): self.file.close() class CSVOutputFormat(KVWriter): def __init__(self, filename): self.file = open(filename, "w+t") self.keys = [] self.sep = "," def writekvs(self, kvs): # Add our current row to the history extra_keys = list(kvs.keys() - self.keys) extra_keys.sort() if extra_keys: self.keys.extend(extra_keys) self.file.seek(0) lines = self.file.readlines() self.file.seek(0) for (i, k) in enumerate(self.keys): if i > 0: self.file.write(",") self.file.write(k) self.file.write("\n") for line in lines[1:]: self.file.write(line[:-1]) self.file.write(self.sep * len(extra_keys)) self.file.write("\n") for (i, k) in enumerate(self.keys): if i > 0: self.file.write(",") v = kvs.get(k) if v is not None: self.file.write(str(v)) self.file.write("\n") self.file.flush() def close(self): self.file.close() class TensorBoardOutputFormat(KVWriter): """ Dumps key/value pairs into TensorBoard's numeric format. """ def __init__(self, dir): os.makedirs(dir, exist_ok=True) self.dir = dir self.step = 1 prefix = "events" path = os.path.join(os.path.abspath(dir), prefix) import tensorflow as tf from tensorflow.python import pywrap_tensorflow from tensorflow.core.util import event_pb2 from tensorflow.python.util import compat self.tf = tf self.event_pb2 = event_pb2 self.pywrap_tensorflow = pywrap_tensorflow self.writer = pywrap_tensorflow.EventsWriter(compat.as_bytes(path)) def writekvs(self, kvs): def summary_val(k, v): kwargs = {"tag": k, "simple_value": float(v)} return self.tf.Summary.Value(**kwargs) summary = self.tf.Summary(value=[summary_val(k, v) for k, v in kvs.items()]) event = self.event_pb2.Event(wall_time=time.time(), summary=summary) event.step = ( self.step ) # is there any reason why you'd want to specify the step? self.writer.WriteEvent(event) self.writer.Flush() self.step += 1 def close(self): if self.writer: self.writer.Close() self.writer = None def make_output_format(format, ev_dir, log_suffix=""): os.makedirs(ev_dir, exist_ok=True) if format == "stdout": return HumanOutputFormat(sys.stdout) elif format == "log": return HumanOutputFormat(os.path.join(ev_dir, "log%s.txt" % log_suffix)) elif format == "json": return JSONOutputFormat(os.path.join(ev_dir, "progress%s.json" % log_suffix)) elif format == "csv": return CSVOutputFormat(os.path.join(ev_dir, "progress%s.csv" % log_suffix)) elif format == "tensorboard": return TensorBoardOutputFormat(os.path.join(ev_dir, "tb%s" % log_suffix)) else: raise ValueError("Unknown format specified: %s" % (format,)) # ================================================================ # API # ================================================================ def logkv(key, val): """ Log a value of some diagnostic Call this once for each diagnostic quantity, each iteration If called many times, last value will be used. """ get_current().logkv(key, val) def logkv_mean(key, val): """ The same as logkv(), but if called many times, values averaged. """ get_current().logkv_mean(key, val) def logkvs(d): """ Log a dictionary of key-value pairs """ for (k, v) in d.items(): logkv(k, v) def dumpkvs(): """ Write all of the diagnostics from the current iteration """ return get_current().dumpkvs() def getkvs(): return get_current().name2val def log(*args, level=INFO): """ Write the sequence of args, with no separators, to the console and output files (if you've configured an output file). """ get_current().log(*args, level=level) def debug(*args): log(*args, level=DEBUG) def info(*args): log(*args, level=INFO) def warn(*args): log(*args, level=WARN) def error(*args): log(*args, level=ERROR) def set_level(level): """ Set logging threshold on current logger. """ get_current().set_level(level) def set_comm(comm): get_current().set_comm(comm) def get_dir(): """ Get directory that log files are being written to. will be None if there is no output directory (i.e., if you didn't call start) """ return get_current().get_dir() record_tabular = logkv dump_tabular = dumpkvs @contextmanager def profile_kv(scopename): logkey = "wait_" + scopename tstart = time.time() try: yield finally: get_current().name2val[logkey] += time.time() - tstart def profile(n): """ Usage: @profile("my_func") def my_func(): code """ def decorator_with_name(func): def func_wrapper(*args, **kwargs): with profile_kv(n): return func(*args, **kwargs) return func_wrapper return decorator_with_name # ================================================================ # Backend # ================================================================ def get_current(): if Logger.CURRENT is None: _configure_default_logger() return Logger.CURRENT class Logger(object): DEFAULT = None # A logger with no output files. (See right below class definition) # So that you can still log to the terminal without setting up any output files CURRENT = None # Current logger being used by the free functions above def __init__(self, dir, output_formats, comm=None): self.name2val = defaultdict(float) # values this iteration self.name2cnt = defaultdict(int) self.level = INFO self.dir = dir self.output_formats = output_formats self.comm = comm # Logging API, forwarded # ---------------------------------------- def logkv(self, key, val): self.name2val[key] = val def logkv_mean(self, key, val): oldval, cnt = self.name2val[key], self.name2cnt[key] self.name2val[key] = oldval * cnt / (cnt + 1) + val / (cnt + 1) self.name2cnt[key] = cnt + 1 def dumpkvs(self): if self.comm is None: d = self.name2val else: d = mpi_weighted_mean( self.comm, { name: (val, self.name2cnt.get(name, 1)) for (name, val) in self.name2val.items() }, ) if self.comm.rank != 0: d["dummy"] = 1 # so we don't get a warning about empty dict # LISA out = d.copy() # Return the dict for unit testing purposes wandb.log({**d}) if int(os.environ['LOCAL_RANK']) == 0: for fmt in self.output_formats: if isinstance(fmt, KVWriter): fmt.writekvs(d) self.name2val.clear() self.name2cnt.clear() return out def log(self, *args, level=INFO): if self.level <= level: self._do_log(args) # Configuration # ---------------------------------------- def set_level(self, level): self.level = level def set_comm(self, comm): self.comm = comm def get_dir(self): return self.dir def close(self): for fmt in self.output_formats: fmt.close() # Misc # ---------------------------------------- def _do_log(self, args): for fmt in self.output_formats: if isinstance(fmt, SeqWriter): fmt.writeseq(map(str, args)) def get_rank_without_mpi_import(): # check environment variables here instead of importing mpi4py # to avoid calling MPI_Init() when this module is imported for varname in ["PMI_RANK", "OMPI_COMM_WORLD_RANK"]: if varname in os.environ: return int(os.environ[varname]) return 0 def mpi_weighted_mean(comm, local_name2valcount): """ Copied from: https://github.com/openai/baselines/blob/ea25b9e8b234e6ee1bca43083f8f3cf974143998/baselines/common/mpi_util.py#L110 Perform a weighted average over dicts that are each on a different node Input: local_name2valcount: dict mapping key -> (value, count) Returns: key -> mean """ all_name2valcount = comm.gather(local_name2valcount) if comm.rank == 0: name2sum = defaultdict(float) name2count = defaultdict(float) for n2vc in all_name2valcount: for (name, (val, count)) in n2vc.items(): try: val = float(val) except ValueError: if comm.rank == 0: warnings.warn( "WARNING: tried to compute mean on non-float {}={}".format( name, val ) ) else: name2sum[name] += val * count name2count[name] += count return {name: name2sum[name] / name2count[name] for name in name2sum} else: return {} def configure(dir=None, format_strs=None, comm=None, log_suffix=""): """ If comm is provided, average all numerical stats across that comm """ if dir is None: dir = os.getenv("OPENAI_LOGDIR") if dir is None: dir = os.path.join( tempfile.gettempdir(), datetime.datetime.now().strftime("openai-%Y-%m-%d-%H-%M-%S-%f"), ) assert isinstance(dir, str) dir = os.path.expanduser(dir) os.makedirs(os.path.expanduser(dir), exist_ok=True) rank = get_rank_without_mpi_import() if rank > 0: log_suffix = log_suffix + "-rank%03i" % rank if format_strs is None: if rank == 0: format_strs = os.getenv("OPENAI_LOG_FORMAT", "stdout,log,csv").split(",") else: format_strs = os.getenv("OPENAI_LOG_FORMAT_MPI", "log").split(",") format_strs = filter(None, format_strs) output_formats = [make_output_format(f, dir, log_suffix) for f in format_strs] Logger.CURRENT = Logger(dir=dir, output_formats=output_formats, comm=comm) if output_formats: log("Logging to %s" % dir) def _configure_default_logger(): configure() Logger.DEFAULT = Logger.CURRENT def reset(): if Logger.CURRENT is not Logger.DEFAULT: Logger.CURRENT.close() Logger.CURRENT = Logger.DEFAULT log("Reset logger") @contextmanager def scoped_configure(dir=None, format_strs=None, comm=None): prevlogger = Logger.CURRENT configure(dir=dir, format_strs=format_strs, comm=comm) try: yield finally: Logger.CURRENT.close() Logger.CURRENT = prevlogger
[]
2024-01-10
ChaoticBlack/SexEdBot-gpt4
src~core~chat_parser.py
from typing import Any from langchain.agents.conversational_chat.output_parser import ConvoOutputParser class ChatCustomParser(ConvoOutputParser): @property def _type(self) -> str: return "ChatCustomParser" def parse(self, text: str) -> Any: cleaned_output = text.strip() if cleaned_output.startswith("AI: "): cleaned_output = cleaned_output[len("AI: ") :] return super().parse(cleaned_output)
[]
2024-01-10
ChaoticBlack/SexEdBot-gpt4
src~core~parser.py
import re from typing import Union from langchain.agents.mrkl.output_parser import MRKLOutputParser from langchain.schema import AgentAction, AgentFinish from utils import UUID_PATTERN FINAL_ANSWER_ACTION = "Final Answer:" FORMAT_INSTRUCTIONS_W_TOOLS = """ Use the following format: Question: the input question you must answer Thought: you should always think about what to do unless it's a casual conversation, then skip to final answer. Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question. Consider all observations to come up with a final answer. """ FORMAT_INSTRUCTIONS_WO_TOOLS = """ Use the following format: Question: the input question you must answer Thought: I now know the final answer Final Answer: the final answer to the original input question """ def get_format_instructions(has_tools=True) -> str: return FORMAT_INSTRUCTIONS_W_TOOLS if has_tools else FORMAT_INSTRUCTIONS_WO_TOOLS class CustomParser(MRKLOutputParser): def get_format_instructions(self) -> str: return get_format_instructions(True) def parse(self, text: str) -> Union[AgentAction, AgentFinish]: if FINAL_ANSWER_ACTION in text: output = text.split(FINAL_ANSWER_ACTION)[-1].strip() output = UUID_PATTERN.split(output) output = [re.sub(r"^\W+", "", el) for el in output] return AgentFinish({"output": output}, text) cleaned_output = super().parse(text) return cleaned_output
[]
2024-01-10
ChaoticBlack/SexEdBot-gpt4
src~core~agent~chat_agent.py
from langchain.agents import AgentExecutor, ConversationalChatAgent from langchain.agents.conversational_chat.prompt import PREFIX from langchain.memory import ConversationBufferWindowMemory from steamship_langchain.llms import OpenAIChat from core.agent.base import BaseAgent from core.chat_parser import ChatCustomParser TEMPERATURE = 0.7 MODEL_NAME = "gpt-3.5-turbo" # or "gpt-4" class ChatAgent(BaseAgent): name: str = "MyAgent" def get_agent(self): llm = OpenAIChat( client=self.client, temperature=TEMPERATURE, model_name=MODEL_NAME ) sys_message = ( f"{PREFIX}\n Please take on the personality of {self.get_personality()}" ) tools = self.get_tools() memory = ConversationBufferWindowMemory( return_messages=True, memory_key="chat_history", ) agent = ConversationalChatAgent.from_llm_and_tools( llm, tools, system_message=sys_message, output_parser=ChatCustomParser() ) return AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, memory=memory, verbose=self.is_verbose_logging_enabled(), )
[]
2024-01-10
anbraten/incosy
src~incosy.py
import yaml from langchain import LLMChain from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.agents import initialize_agent, Tool, AgentType from langchain.chains import ConversationChain from langchain.memory.chat_message_histories.in_memory import ChatMessageHistory class SafeDict(dict): def __missing__(self, key): return '{' + key + '}' def get_best_product_prompt(): file = open("database.yml", "r") database = yaml.load(file, Loader=yaml.FullLoader) product_list = [] product_names = [] for product in database['products']: if not 'use_cases' in product: print("Product " + product.get('name') + " has no name or use_cases") continue product_names.append(product.get('name')) product_list.append(product.get('name') + ": " + product.get('use_cases').replace('\n', ' ').replace('- ', '')) data = SafeDict(product_list='- ' + '\n- '.join(product_list), product_names=', '.join(product_names)) prompt = """ Suggest products for the following problem of a caregiver of a nursing home as best you can. You have access to the following products and their use cases: {product_list} Current conversation: {chat_history} Use the following format: Question: the input problem to suggest products for Thought: you should always think what would be the best products to suggest Products: the products to suggest, should of [{product_names}] Observation: the result of the suggestion ... (this Thought/Product/Observation can repeat N times) Thought: I now know the best products to suggest Final Answer: Maybe one of the following products could help you ... Begin! Question: {input} Thought: """.format_map(data).strip() return prompt def get_product_description_prompt(): file = open("database.yml", "r") database = yaml.load(file, Loader=yaml.FullLoader) product_list = [] product_names = [] for product in database['products']: if not 'use_cases' in product: print("Product " + product.get('name') + " has no name or use_cases") continue product_names.append(product.get('name')) product_list.append(product.get('name') + ": " + product.get('use_cases').replace('\n', ' ').replace('- ', '')) data = SafeDict(product_list='- ' + '\n- '.join(product_list), product_names=', '.join(product_names)) prompt = """ Explain how the caregiver can use the suggested products to solve their problem. Current conversation: {chat_history} Caregiver: {input} AI: """.format_map(data).strip() return prompt def get_product_buy_prompt(): file = open("database.yml", "r") database = yaml.load(file, Loader=yaml.FullLoader) product_list = [] product_names = [] for product in database['products']: if not 'use_cases' in product: print("Product " + product.get('name') + " has no name or use_cases") continue product_names.append(product.get('name')) product_list.append(product.get('name') + ": " + product.get('use_cases').replace('\n', ' ').replace('- ', '')) data = SafeDict(product_list='- ' + '\n- '.join(product_list), product_names=', '.join(product_names)) prompt = """ Tell the caregiver that you will forward their suggestion to the home management. Tell the caregiver that you will also notify them again when the home management has decided to buy the product. Tell the caregiver that they can always ask you for help again and tell them that you care about them. Current conversation: {chat_history} Caregiver: {input} AI: """.format_map(data).strip() return prompt def open_chat(): llm = ChatOpenAI( temperature=0, model_name='gpt-3.5-turbo', verbose=True) chat_memory = ChatMessageHistory() chat_memory.add_ai_message( "How was your last shift?") memory = ConversationBufferMemory( chat_memory=chat_memory, memory_key="chat_history", human_prefix="Caregiver:", output_key="output") readonly_memory = ReadOnlySharedMemory(memory=memory) product_chain = ConversationChain( prompt=PromptTemplate( template=get_best_product_prompt(), input_variables=["chat_history", "input"], ), llm=llm, verbose=True, memory=readonly_memory, ) describe_chain = ConversationChain( prompt=PromptTemplate( template=get_product_description_prompt(), input_variables=["chat_history", "input"], ), llm=llm, verbose=True, memory=readonly_memory, ) buy_chain = LLMChain( prompt=PromptTemplate( template=get_product_buy_prompt(), input_variables=["chat_history", "input"], ), llm=llm, verbose=True, memory=readonly_memory, ) tools = [ Tool( name="problem", func=product_chain.run, description="""useful for when the caregiver is telling us about a problem they have in caregiving""", return_direct=True, ), Tool( name="describe", func=describe_chain.run, description="""useful for when the caregiver is interested in a product and wants to know more about it""", return_direct=True, ), Tool( name="buy", func=buy_chain.run, description="""useful for when the caregiver wants to suggest a product to the home lead""", return_direct=True, ), ] agent_chain = initialize_agent( tools, llm=llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory, return_intermediate_steps=True,) return agent_chain
[ "Tell the caregiver that you will forward their suggestion to the home management.\nTell the caregiver that you will also notify them again when the home management has decided to buy the product.\nTell the caregiver that they can always ask you for help again and tell them that you care about them.\n\nCurrent conversation:\nPLACEHOLDER\n\nCaregiver: PLACEHOLDER\nAI:", "Explain how the caregiver can use the suggested products to solve their problem.\n\nCurrent conversation:\nPLACEHOLDER\n\nCaregiver: PLACEHOLDER\nAI:", "Suggest products for the following problem of a caregiver of a nursing home as best you can. You have access to the following products and their use cases:\n\nPLACEHOLDER\n\nCurrent conversation:\nPLACEHOLDER\n\nUse the following format:\n\nQuestion: the input problem to suggest products for\nThought: you should always think what would be the best products to suggest\nProducts: the products to suggest, should of [PLACEHOLDER]\nObservation: the result of the suggestion\n... (this Thought/Product/Observation can repeat N times)\nThought: I now know the best products to suggest\nFinal Answer: Maybe one of the following products could help you ...\n\nBegin!\n\nQuestion: PLACEHOLDER\nThought:" ]
2024-01-10
anbraten/incosy
src~generate_database.py
import yaml import os from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import CharacterTextSplitter from langchain.llms import OpenAI from langchain.chains import RetrievalQA from langchain.document_loaders import TextLoader from dotenv import load_dotenv import requests load_dotenv() def sanitize_path(file): # replace space with _ and lower case return file.replace(' ', '_').lower() def generate_description(product, qa): name = product['name'] return qa.run("Give me a description of " + name + " in english.") def generate_use_cases(product, qa): name = product['name'] problem = product['problem'] return qa.run("What are the use cases of " + name + " in english.") def generate_funding(product, qa): name = product['name'] description = product['description'] return qa.run("Generate a text how can I fund " + name + " with following description: " + description + " in english.") def generate_reviews(product, qa): name = product['name'] return qa.run("Make up a list of reviews from caregivers of a nursing home for " + name + " formatted as markdown list in english.") def generate_db(): # load yaml file file = open("database.yml") database = yaml.load(file, Loader=yaml.FullLoader) for product in database['products']: print('\n### ' + product['name'] + ' ###') filename = sanitize_path(product['name'] + '.txt') if not os.path.exists('data/' + filename): print(filename+' is missing!') continue loader = TextLoader("data/" + filename) documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() docsearch = Chroma.from_documents(texts, embeddings) # qa = VectorstoreIndexCreator().from_loaders([loader]) qa = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever()) if not 'description' in product or product['description'] == '': product["description"] = generate_description(product, qa) print("Generated description") if not 'use_cases' in product or product['use_cases'] == '': product["use_cases"] = generate_use_cases(product, qa) print("Generated use_cases") if not 'funding' in product or product['funding'] == '': product["funding"] = generate_funding(product, qa) print("Generated funding") if not 'reviews' in product or product['reviews'] == '': product["reviews"] = generate_reviews(product, qa) print("Generated reviews") product["description"] = product["description"].strip() product["use_cases"] = product["use_cases"].strip() product["funding"] = product["funding"].strip() product["reviews"] = product["reviews"].strip() # write yaml file with open('database.yml', 'w') as file: yaml.dump(database, file, allow_unicode=True) def upload_database(): baseUrl = "https://budibase.app/api/public/v1/" url = baseUrl + "tables/" + \ os.getenv("BUDIBASE_PRODUCTS_TABLE_ID") + "/rows" # load yaml file file = open("database.yml") database = yaml.load(file, Loader=yaml.FullLoader) for product in database['products']: print('\n### ' + product['name'] + ' ###') data = { "name": product.get('name'), "description": product.get('description'), "use_cases": product.get('use_cases'), "funding": product.get('funding'), "reviews": product.get('reviews'), "website": product.get('websites', [""])[0], } headers = { 'x-budibase-app-id': os.getenv("BUDIBASE_APP_ID"), 'x-budibase-api-key': os.getenv("BUDIBASE_API_KEY"), } response = requests.post(url, json=data, headers=headers) print(response.status_code) if __name__ == "__main__": # generate_db() upload_database()
[]
2024-01-10
Magma4/ai_assistant
chatgpt~exercise2.py
# Text summarization # One really common use case for using OpenAI's models is summarizing text. This has a ton of applications in business settings, including summarizing reports into concise one-pagers or a handful of bullet points, or extracting the next steps and timelines for different stakeholders. # In this exercise, you'll summarize a passage of text on financial investment into two concise bullet points using a text completion model. # The openai package has been pre-loaded for you. # Instructions # 100 XP # Assign your API key to openai.api_key. # Create a request to Completion endpoint, sending the prompt provided; use a maximum of 400 tokens and make the response more deterministic. # Set your API key import openai openai.api_key = "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" prompt="""Summarize the following text into two concise bullet points: Investment refers to the act of committing money or capital to an enterprise with the expectation of obtaining an added income or profit in return. There are a variety of investment options available, including stocks, bonds, mutual funds, real estate, precious metals, and currencies. Making an investment decision requires careful analysis, assessment of risk, and evaluation of potential rewards. Good investments have the ability to produce high returns over the long term while minimizing risk. Diversification of investment portfolios reduces risk exposure. Investment can be a valuable tool for building wealth, generating income, and achieving financial security. It is important to be diligent and informed when investing to avoid losses.""" # Create a request to the Completion endpoint response = openai.Completion.create( model = "text-davinci-003", prompt = prompt, temperature = 0, max_tokens = 400 ) print(response["choices"][0]["text"])
[ "Summarize the following text into two concise bullet points:\nInvestment refers to the act of committing money or capital to an enterprise with the expectation of obtaining an added income or profit in return. There are a variety of investment options available, including stocks, bonds, mutual funds, real estate, precious metals, and currencies. Making an investment decision requires careful analysis, assessment of risk, and evaluation of potential rewards. Good investments have the ability to produce high returns over the long term while minimizing risk. Diversification of investment portfolios reduces risk exposure. Investment can be a valuable tool for building wealth, generating income, and achieving financial security. It is important to be diligent and informed when investing to avoid losses." ]
2024-01-10
Magma4/ai_assistant
chatgpt~ex3.py
# AI is playing a much greater role in content generation, from creating marketing content such as blog post titles to creating outreach email templates for sales teams. # In this exercise, you'll harness AI through the Completion endpoint to generate a catchy slogan for a new restaurant. Feel free to test out different prompts, such as varying the type of cuisine (e.g., Italian, Chinese) or the type of restaurant (e.g., fine-dining, fast-food), to see how the response changes. # The openai package has been pre-loaded for you. # Instructions # 100 XP # Assign your API key to openai.api_key. # Create a request to the Completion endpoint to create a slogan for a new restaurant; set the maximum number of tokens to 100. # Set your API key import openai openai.api_key = "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" # Create a request to the Completion endpoint response = openai.Completion.create( model="text-davinci-003", prompt = """create a catchy slogan for a new restaurant: The restaurant serves chinese fast-food cuisines """, max_tokens = 100 ) print(response["choices"][0]["text"])
[ "create a catchy slogan for a new restaurant:\n The restaurant serves chinese fast-food cuisines " ]
2024-01-10
cbr4l0k/snoo.py
src~LLMManager.py
import json import os from dotenv import load_dotenv from langchain.llms.openai import OpenAI from langchain import PromptTemplate, LLMChain from langchain.chains import ConversationChain from langchain.memory import ConversationKGMemory from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from FileHandler import FileHandler from prompt_handler import PromptHandler # some important enviroment variables load_dotenv() PROJECTS_PATH = os.getenv("PROJECTS_PATH") OPEN_AI_API_KEY = os.getenv("OPEN_AI_API_KEY") OUTPUTS_PATH = os.getenv("OUTPUTS_PATH") DEFAULT_LLM = os.getenv("DEFAULT_LLM") import warnings warnings.filterwarnings("ignore") class LLM: """ This class is a wrapper for the langchain library. It is used to generate explainations for code. It uses the langchain library to generate the explainations. It is also used to generate cohesion and coupling analysis for a project. It also produces explainations for the folders in the project. It is also used to generate explainations for a directory. It is also used to generate explainations for a file. Attributes: model: The langchain model used to generate the explainations. llm_chain: The langchain chain used to generate the explainations. context_window_size: The size of the context window used to generate the explainations. options: The options used to initialize the langchain model. prompt_handler: The prompt handler used to generate the prompts for the langchain model. file_handler: The file handler used to handle the files in the project. Methods: load_model: Loads the langchain model. load_chain: Loads the langchain chain. generate_response: Generates an explaination for a file. generate_explaination_for_directory: Generates an explaination for a directory. generate_cohesion_coupling_analysis: Generates a cohesion and coupling analysis for a project. set_context_window_size: Sets the context window size for the langchain model. _check_response: Checks the response of the langchain model. """ projects_path = '' def __init__(self, projects_path: str, options: dict) -> None: self.model : OpenAI = None self.llm_chain : LLMChain = None self.context_window_size : int = None self.options : dict = options self.prompt_handler : PromptHandler = PromptHandler(model_name=self.options["model_name"]) self.prompt_handler.set_projects_path(projects_path) self.set_projects_path(projects_path) self.load_model() self.file_handler : FileHandler = FileHandler() @staticmethod def set_projects_path(projects_path: str) -> None: """ Sets the outputs path for the LLMManager. """ LLM.projects_path = projects_path def set_context_window_size(self, context_window_size: int) -> None: """ Sets the context window size for the langchain model. Args: ---------- context_window_size: int The size of the context window to use. Returns: ---------- None """ self.context_window_size = context_window_size def load_model(self) -> None: """ Loads the OpenAI langchain model. Args: ---------- None Returns: ---------- None """ model = OpenAI(**self.options) self.prompt_handler.set_model(model_name=self.options["model_name"]) self.model = model def load_chain(self, template: dict[str, any], requires_memory: bool = False) -> None: """ Loads the langchain chain. Args: ---------- template: dict[str, any] The template to use for the langchain chain. requires_memory: bool Whether the langchain chain requires memory or not. Returns: ---------- None """ self.load_model() prompt: PromptTemplate = PromptTemplate( input_variables=template["input_variables"], template=template["template"] ) llm_chain: LLMChain = LLMChain( llm=self.model, prompt=prompt, verbose=self.options["verbose"] ) self.llm_chain = llm_chain def generate_response(self, file_full_path: str, code: str) -> str: """ Generates an explaination for a file. Args: ---------- file_full_path: str The full path to the file. code: str The code to generate the explaination for. Returns: ---------- str The explaination for the file. """ # estimate the number of tokens for the code code_token_size = (self.context_window_size - self.prompt_handler.longest_prompt_lenght) # chunk the document based on the estimated number of tokens available for the code docs = self.file_handler.chunk_document(file_full_path, code, code_token_size) # define the response response = None # if the document is too small, just run it if len(docs) == 1: template = self.prompt_handler.get_raw_template(template=0) self.load_chain(template=template) response = self.llm_chain.run(docs[0].page_content) # if the document is too big, chunk it and run it elif len(docs) > 1: template = self.prompt_handler.get_raw_template(template=1) self.load_chain(template=template, requires_memory=True) responses = [] for doc in docs: response = self.llm_chain.run(doc.page_content) responses.append(response) # combine the responses and save them template = self.prompt_handler.get_raw_template(template=2) self.load_chain(template=template, requires_memory=True) response = self.llm_chain.run(responses) # print(response) return self._check_response(response) def _check_response(self, response: str) -> str: """ Checks the response of the langchain model. Args: ---------- response: str The response to check. Returns: ---------- str The checked response. """ template = self.prompt_handler.get_raw_template(template=4) self.load_chain(template=template) response = self.llm_chain.run(response) response_json = json.loads(response) return response_json def generate_explaination_for_directory(self, directory_contents: str) -> str: """ Generates an explaination for a directory. It is used to generate explainations for the folders in the project. Args: ---------- directory_contents: str The contents of the directory. Returns: ---------- str The explaination for the directory. """ template = self.prompt_handler.get_raw_template(template=5) self.load_chain(template=template) response = self.llm_chain.run(directory_contents) # print(response) return response def generate_cohesion_coupling_analysis(self, json_report: str) -> str: """ Generates a cohesion and coupling analysis for a project. It is used to generate cohesion and coupling analysis for a project. Args: ---------- json_report: str The json report of the project. Returns: ---------- str The cohesion and coupling analysis for the project. """ template = self.prompt_handler.get_raw_template(template=3) prompt = self.prompt_handler.get_prompt(template=3, json_reports=json_report) prompt_len = self.prompt_handler.get_prompt_token_lenght(prompt) # if the prompt is really big (bigger than the context window size) then load # the gpt which has a bigger context window size if prompt_len > self.context_window_size: # print("prompt is too big, loading gpt-3.5-turbo-16k") self.options["model_name"] = "gpt-3.5-turbo-16k" self.load_model() self.set_context_window_size(16e3) self.load_chain(template=template) response = self.llm_chain.run(prompt) else: # print("prompt is not too big, loading gpt-3.5-turbo-16k") self.options["model_name"] = "gpt-3.5-turbo-16k" self.load_model() self.set_context_window_size(16e3) self.load_chain(template=template) response = self.llm_chain.run(prompt) #before returning the response, go back to the original cheaper model self.options["model_name"] = "gpt-3.5-turbo-16k" self.set_context_window_size(16e3) self.load_model() response_json = json.loads(response) return response_json def default_llm(projects_path : str): """ Returns the default langchain model. It is used to generate explainations for the files in the project. Args: ---------- projects_path: str The path to the projects. Returns: ---------- LLM The default langchain model. """ average_number_of_tokens_per_sentence = 27 desired_number_of_sentences_per_file = 30 max_tokens = desired_number_of_sentences_per_file * average_number_of_tokens_per_sentence context_window_size = 16e3 model_name = DEFAULT_LLM llm = LLM( projects_path=projects_path, options={ "openai_api_key": OPEN_AI_API_KEY, "model_name": model_name, "temperature": 0, "max_tokens": max_tokens, "presence_penalty": 2, "callback_manager": CallbackManager([StreamingStdOutCallbackHandler()]), "verbose": False }, ) llm.set_context_window_size(context_window_size) return llm
[ "input_variables" ]
2024-01-10
cbr4l0k/snoo.py
src~FileHandler.py
import json import os import logging from typing import List from dotenv import load_dotenv from langchain.text_splitter import (RecursiveCharacterTextSplitter, Language) from langchain.schema.document import Document # some important enviroment variables load_dotenv() MODEL_PATH = os.getenv("MODEL_PATH") PROJECTS_PATH = os.getenv("PROJECTS_PATH") OUTPUTS_PATH = os.getenv("OUTPUTS_PATH") class FileHandler: """ This class is supposed to handle the files, individually. It will be in charge of reading the files, chunking them, and retrieving the chunks. Attributes: ---------- - None Methods: ---------- - chunk_document: List[Document] This function chunks a given block of code taking into account the semantic categories given in a language by considering it's syntax, from it recursively tries to divide each chunk into one or many of the desired chunk size, this does not guarantee that they all have the same size, but they should be close. Also considers the chunk overlap, which allows to have a bit of the previous information available. - from_filename_to_lang: Language This function is supposed to take the filename and infer the language by taking the last part of the name and then return the Language it corresponds to if any (in the dict of supported LangChain languages) If it does not find it, it will return None. """ def __init__(self) -> None: """ The constructor for the FileHandler class. """ pass @staticmethod def from_filename_to_lang(filename: str): """ This function is supposed to take the filename and infer the language by taking the last part of the name and then return the Language it corresponds to if any (in the dict of supported LangChain languages) If it does not find it, it will return None Args: ---------- filename: str The filename to infer the language from Returns: ---------- Language The language that corresponds to the filename, if any. """ return Language.PYTHON @staticmethod def chunk_document(filename_full_path: str, code: str, chunk_size: int, chunk_overlap: int = 0) -> List[Document]: """ This function chunks a given block of code taking into account the semantic categories given in a language by considering it's syntax, from it recursively tries to divide each chunk into one or many of the desired chunk size, this does not guarantee that they all have the same size, but they should be close. Also considers the chunk overlap, which allows to have a bit of the previous information available. Args: ---------- filename_full_path: str The full path to the file, including the filename and extension. code: str The code to chunk. chunk_size: int The size of the chunks to create. chunk_overlap: int The overlap between chunks. Returns: ---------- List[Document] The list of documents created from the chunking. """ filename = filename_full_path.split("/")[-1] lang = FileHandler.from_filename_to_lang(filename) python_splitter = RecursiveCharacterTextSplitter.from_language(chunk_size=chunk_size, chunk_overlap=chunk_overlap, language=lang, ) docs = python_splitter.create_documents([code]) return docs
[]
2024-01-10
Ssslakter/AniSearch
src~anisearch~core~recsys.py
from langchain.docstore.document import Document from libreco.algorithms import LightGCN from libreco.data import DataInfo class RecSys: def __init__(self, model_path: str): data_info = DataInfo.load(model_path, "recsys_model") self.model = LightGCN.load(model_path, "recsys_model", data_info) def rec_from_relevant(self, nickname: str, docs: list[Document]) -> list[Document]: item_ids = list(map(lambda x: x.metadata["uid"], docs)) scores = self.model.predict( user=nickname, item=item_ids, cold_start="popular" ).astype(object) filtered = [] for uid, score in sorted( zip(item_ids, scores), key=lambda x: x[1], reverse=True ): if not score > 0: break for doc in docs: if doc.metadata["uid"] == uid: filtered.append(doc) return filtered def recommend(self, nickname: str, n_rec: int): response = self.model.recommend_user(user=nickname, n_rec=n_rec)[nickname] return list(response.astype(object))
[]
2024-01-10
Ssslakter/AniSearch
src~anisearch~core~storage.py
from venv import logger import pandas as pd from qdrant_client import QdrantClient from qdrant_client.http.exceptions import UnexpectedResponse import qdrant_client.http.models as rest from langchain.vectorstores import Qdrant from langchain.embeddings.base import Embeddings from src.features import document_builder from src.anisearch.configs import QdrantConfig BATCH_SIZE = 64 class Storage: """Client wrapper to qdrant vector storage""" def __init__(self, embeddings: Embeddings, config: QdrantConfig) -> None: logger.info("Initializing storage") self.config = config self.embeddings = embeddings self.client = QdrantClient(url=config.url, api_key=config.api_key, timeout=100) self.document_builder = document_builder.DocumentBuilder() self.qdrant = Qdrant( client=self.client, collection_name=config.collection_name, embeddings=embeddings, ) self.init_collection(config.collection_name) def init_collection(self, collection_name: str): """Tries to create collection, even if it already exists Raises: err_response: If qdrant returned error response not related with existance of collection """ logger.info("Creating collection %s...", collection_name) vector_length = len(self.embeddings.embed_documents([""])[0]) try: self.client.create_collection( collection_name, rest.VectorParams( size=vector_length, distance=rest.Distance[self.config.distance_func.upper()], ), ) logger.info("Collection was created!") except UnexpectedResponse as err_response: if b"already exists!" in err_response.content: logger.info("Collection %s already exists", collection_name) return raise err_response def insert_df(self, df: pd.DataFrame): """Inserts documents to db from pandas dataframe""" logger.info("Starting to build documents...") df = self.document_builder.prepare_df(df) docs = self.document_builder.build_documents(df) logger.info("Documents were succesfully builded") for i in range(0, len(docs), BATCH_SIZE): batch = docs[i : min(i + BATCH_SIZE, len(docs) - 1)] try: self.qdrant.add_documents(batch) logger.info( "Uploaded %s/%s documents", min(i + BATCH_SIZE, len(docs) - 1), len(docs), ) except UnexpectedResponse as e: logger.error( "Failed to upload documents", exc_info=e, ) break
[]
2024-01-10
Ssslakter/AniSearch
src~anisearch~core~services.py
from operator import itemgetter from qdrant_client.models import Filter, FieldCondition, Record, MatchValue, MatchText, Range from langchain.docstore.document import Document from langchain.embeddings.huggingface import HuggingFaceEmbeddings from src.features.document_builder import DocumentBuilder from src.anisearch.configs import Config from src.anisearch.core.storage import Storage from src.anisearch.data_models import AnimeData from src.anisearch.core.recsys import RecSys class Services: """Class that holds all services""" OUTPUT_AMOUNT = 15 filter = Filter( must=[ FieldCondition( key="metadata.score", range=Range(gt=5.3), ), FieldCondition( key="metadata.popularity", range=Range(lt=7000), ), ] ) def __init__(self, config: Config) -> None: self.embeddings = get_embeddings(config.models_dir) self.config = config self.document_builder = DocumentBuilder() self.storage = Storage(self.embeddings, self.config.qdrant) self.recsys = RecSys(config.recsys_dir) def initialize(self): """Perform some init configurations like creating collections, etc.""" self.storage.init_collection(self.config.qdrant.collection_name) def insert_anime(self, anime: AnimeData) -> list[str]: """Inserts anime into storage""" doc = self.document_builder.render_document(anime) return self.storage.qdrant.add_documents([doc]) async def search_anime(self, query: str) -> list[Document]: docs = self.qdrant_search(query, k=200) return list(docs) async def full_search(self, query: str) -> list[Document]: docs = self.fulltext_search(query, k=200) return list(docs) def delete_anime(self, uid: int): self.storage.qdrant.client.delete( self.config.qdrant.collection_name, points_selector=Filter( should=[FieldCondition(key="metadata.uid", match=MatchValue(value=uid))] ), ) def get_anime(self, uid: int): response = self.storage.qdrant.client.scroll( self.config.qdrant.collection_name, scroll_filter=Filter( must=[FieldCondition(key="metadata.uid", match=MatchValue(value=uid))] ), limit=1, )[0] if not response: return None return self._to_document(response[0]) def recommend_anime(self, nickname: str): uids = self.recsys.recommend(nickname, self.OUTPUT_AMOUNT) return [self.get_anime(uid) for uid in uids] async def recommend_from_relevant(self, nickname: str, query: str): animes = await self.search_anime(query) return self.recsys.rec_from_relevant(nickname, animes) def get_collections(self): return self.storage.client.get_collections() def _to_document(self, point: Record): return Document( page_content=point.payload["page_content"], # type:ignore metadata=point.payload["metadata"], # type:ignore ) def qdrant_search(self, query: str, k: int = 4) -> list[Document]: results = self.storage.client.search( collection_name=self.config.qdrant.collection_name, query_vector=self.storage.qdrant._embed_query(query), query_filter=self.filter, with_payload=True, limit=k, ) results = [ ( self.storage.qdrant._document_from_scored_point( result, self.storage.qdrant.content_payload_key, self.storage.qdrant.metadata_payload_key, ), result.score, ) for result in results ] return list(map(itemgetter(0), results)) def fulltext_search(self, query: str, k: int = 4) -> list[Document]: data = self.storage.qdrant.client.scroll( self.config.qdrant.collection_name, scroll_filter=Filter( should=[ FieldCondition(key="page_content", match=MatchText(text=query.lower())), ] ), limit=k )[0] return [ self.storage.qdrant._document_from_scored_point( result, self.storage.qdrant.content_payload_key, self.storage.qdrant.metadata_payload_key, ) for result in data ] def get_embeddings(models_dir: str) -> HuggingFaceEmbeddings: """Get default huggingface embeddings from models directory""" model_name = "sentence-transformers/all-mpnet-base-v2" model_kwargs = {"device": "cuda"} encode_kwargs = {"normalize_embeddings": False} return HuggingFaceEmbeddings( model_name=model_name, cache_folder=models_dir, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs, )
[]
2024-01-10
cnacha-mfu/AutoGPT
autogpt~json_utils~utilities.py
"""Utilities for the json_fixes package.""" import ast import json import os.path from typing import Any, Literal from jsonschema import Draft7Validator from autogpt.config import Config from autogpt.logs import logger LLM_DEFAULT_RESPONSE_FORMAT = "llm_response_format_1" def extract_dict_from_response(response_content: str) -> dict[str, Any]: # Sometimes the response includes the JSON in a code block with ``` if response_content.startswith("```") and response_content.endswith("```"): # Discard the first and last ```, then re-join in case the response naturally included ``` response_content = "```".join(response_content.split("```")[1:-1]) # response content comes from OpenAI as a Python `str(content_dict)`, literal_eval reverses this print("response_content: ",response_content) try: return ast.literal_eval(response_content) except BaseException as e: logger.info(f"Error parsing JSON response with literal_eval {e}") logger.debug(f"Invalid JSON received in response: {response_content}") # TODO: How to raise an error here without causing the program to exit? return {} def llm_response_schema( config: Config, schema_name: str = LLM_DEFAULT_RESPONSE_FORMAT ) -> dict[str, Any]: filename = os.path.join(os.path.dirname(__file__), f"{schema_name}.json") with open(filename, "r") as f: try: json_schema = json.load(f) except Exception as e: raise RuntimeError(f"Failed to load JSON schema: {e}") if config.openai_functions: del json_schema["properties"]["command"] json_schema["required"].remove("command") return json_schema def validate_dict( object: object, config: Config, schema_name: str = LLM_DEFAULT_RESPONSE_FORMAT ) -> tuple[Literal[True], None] | tuple[Literal[False], list]: """ :type schema_name: object :param schema_name: str :type json_object: object Returns: bool: Whether the json_object is valid or not list: Errors found in the json_object, or None if the object is valid """ schema = llm_response_schema(config, schema_name) validator = Draft7Validator(schema) if errors := sorted(validator.iter_errors(object), key=lambda e: e.path): for error in errors: logger.debug(f"JSON Validation Error: {error}") if config.debug_mode: logger.error(json.dumps(object, indent=4)) logger.error("The following issues were found:") for error in errors: logger.error(f"Error: {error.message}") return False, errors logger.debug("The JSON object is valid.") return True, None
[]
2024-01-10
icanbwell/SparkAutoMapper.FHIR
spark_auto_mapper_fhir~resources~guidance_response.py
from __future__ import annotations from typing import List, Optional, TYPE_CHECKING, Union # noinspection PyPackageRequirements from pyspark.sql.types import StructType, DataType from spark_auto_mapper_fhir.fhir_types.date_time import FhirDateTime from spark_auto_mapper_fhir.fhir_types.list import FhirList from spark_auto_mapper_fhir.complex_types.meta import Meta from spark_auto_mapper_fhir.extensions.extension_base import ExtensionBase from spark_auto_mapper_fhir.fhir_types.id import FhirId from spark_auto_mapper_fhir.fhir_types.uri import FhirUri from spark_auto_mapper_fhir.base_types.fhir_resource_base import FhirResourceBase from spark_fhir_schemas.r4.resources.guidanceresponse import GuidanceResponseSchema if TYPE_CHECKING: pass # id_ (id) # meta (Meta) # implicitRules (uri) # language (CommonLanguages) from spark_auto_mapper_fhir.value_sets.common_languages import CommonLanguagesCode # text (Narrative) from spark_auto_mapper_fhir.complex_types.narrative import Narrative # contained (ResourceContainer) from spark_auto_mapper_fhir.complex_types.resource_container import ( ResourceContainer, ) # extension (Extension) # modifierExtension (Extension) # requestIdentifier (Identifier) from spark_auto_mapper_fhir.complex_types.identifier import Identifier # identifier (Identifier) # moduleUri (uri) # moduleCanonical (canonical) from spark_auto_mapper_fhir.fhir_types.canonical import FhirCanonical # moduleCodeableConcept (CodeableConcept) from spark_auto_mapper_fhir.complex_types.codeable_concept import CodeableConcept # Import for CodeableConcept for moduleCodeableConcept from spark_auto_mapper_fhir.value_sets.generic_type import GenericTypeCode # End Import for CodeableConcept for moduleCodeableConcept # status (GuidanceResponseStatus) from spark_auto_mapper_fhir.value_sets.guidance_response_status import ( GuidanceResponseStatusCode, ) # subject (Reference) from spark_auto_mapper_fhir.complex_types.reference import Reference # Imports for References for subject from spark_auto_mapper_fhir.resources.patient import Patient from spark_auto_mapper_fhir.resources.group import Group # encounter (Reference) # Imports for References for encounter from spark_auto_mapper_fhir.resources.encounter import Encounter # occurrenceDateTime (dateTime) # performer (Reference) # Imports for References for performer from spark_auto_mapper_fhir.resources.device import Device # reasonCode (CodeableConcept) # Import for CodeableConcept for reasonCode # End Import for CodeableConcept for reasonCode # reasonReference (Reference) # Imports for References for reasonReference from spark_auto_mapper_fhir.resources.condition import Condition from spark_auto_mapper_fhir.resources.observation import Observation from spark_auto_mapper_fhir.resources.diagnostic_report import DiagnosticReport from spark_auto_mapper_fhir.resources.document_reference import DocumentReference # note (Annotation) from spark_auto_mapper_fhir.complex_types.annotation import Annotation # evaluationMessage (Reference) # Imports for References for evaluationMessage from spark_auto_mapper_fhir.resources.operation_outcome import OperationOutcome # outputParameters (Reference) # Imports for References for outputParameters from spark_auto_mapper_fhir.resources.parameters import Parameters # result (Reference) # Imports for References for result from spark_auto_mapper_fhir.resources.care_plan import CarePlan from spark_auto_mapper_fhir.resources.request_group import RequestGroup # dataRequirement (DataRequirement) from spark_auto_mapper_fhir.complex_types.data_requirement import DataRequirement # This file is auto-generated by generate_classes so do not edit manually # noinspection PyPep8Naming class GuidanceResponse(FhirResourceBase): """ GuidanceResponse guidanceresponse.xsd A guidance response is the formal response to a guidance request, including any output parameters returned by the evaluation, as well as the description of any proposed actions to be taken. If the element is present, it must have either a @value, an @id, or extensions """ # noinspection PyPep8Naming def __init__( self, *, use_date_for: Optional[List[str]] = None, id_: Optional[FhirId] = None, meta: Optional[Meta] = None, implicitRules: Optional[FhirUri] = None, language: Optional[CommonLanguagesCode] = None, text: Optional[Narrative] = None, contained: Optional[FhirList[ResourceContainer]] = None, extension: Optional[FhirList[ExtensionBase]] = None, modifierExtension: Optional[FhirList[ExtensionBase]] = None, requestIdentifier: Optional[Identifier] = None, identifier: Optional[FhirList[Identifier]] = None, moduleUri: Optional[FhirUri] = None, moduleCanonical: Optional[FhirCanonical] = None, moduleCodeableConcept: Optional[CodeableConcept[GenericTypeCode]] = None, status: GuidanceResponseStatusCode, subject: Optional[Reference[Union[Patient, Group]]] = None, encounter: Optional[Reference[Encounter]] = None, occurrenceDateTime: Optional[FhirDateTime] = None, performer: Optional[Reference[Device]] = None, reasonCode: Optional[FhirList[CodeableConcept[GenericTypeCode]]] = None, reasonReference: Optional[ FhirList[ Reference[ Union[Condition, Observation, DiagnosticReport, DocumentReference] ] ] ] = None, note: Optional[FhirList[Annotation]] = None, evaluationMessage: Optional[FhirList[Reference[OperationOutcome]]] = None, outputParameters: Optional[Reference[Parameters]] = None, result: Optional[Reference[Union[CarePlan, RequestGroup]]] = None, dataRequirement: Optional[FhirList[DataRequirement]] = None, ) -> None: """ A guidance response is the formal response to a guidance request, including any output parameters returned by the evaluation, as well as the description of any proposed actions to be taken. If the element is present, it must have either a @value, an @id, or extensions :param id_: The logical id of the resource, as used in the URL for the resource. Once assigned, this value never changes. :param meta: The metadata about the resource. This is content that is maintained by the infrastructure. Changes to the content might not always be associated with version changes to the resource. :param implicitRules: A reference to a set of rules that were followed when the resource was constructed, and which must be understood when processing the content. Often, this is a reference to an implementation guide that defines the special rules along with other profiles etc. :param language: The base language in which the resource is written. :param text: A human-readable narrative that contains a summary of the resource and can be used to represent the content of the resource to a human. The narrative need not encode all the structured data, but is required to contain sufficient detail to make it "clinically safe" for a human to just read the narrative. Resource definitions may define what content should be represented in the narrative to ensure clinical safety. :param contained: These resources do not have an independent existence apart from the resource that contains them - they cannot be identified independently, and nor can they have their own independent transaction scope. :param extension: May be used to represent additional information that is not part of the basic definition of the resource. To make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. :param modifierExtension: May be used to represent additional information that is not part of the basic definition of the resource and that modifies the understanding of the element that contains it and/or the understanding of the containing element's descendants. Usually modifier elements provide negation or qualification. To make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions. Modifier extensions SHALL NOT change the meaning of any elements on Resource or DomainResource (including cannot change the meaning of modifierExtension itself). :param requestIdentifier: The identifier of the request associated with this response. If an identifier was given as part of the request, it will be reproduced here to enable the requester to more easily identify the response in a multi-request scenario. :param identifier: Allows a service to provide unique, business identifiers for the response. :param moduleUri: None :param moduleCanonical: None :param moduleCodeableConcept: None :param status: The status of the response. If the evaluation is completed successfully, the status will indicate success. However, in order to complete the evaluation, the engine may require more information. In this case, the status will be data-required, and the response will contain a description of the additional required information. If the evaluation completed successfully, but the engine determines that a potentially more accurate response could be provided if more data was available, the status will be data-requested, and the response will contain a description of the additional requested information. :param subject: The patient for which the request was processed. :param encounter: The encounter during which this response was created or to which the creation of this record is tightly associated. :param occurrenceDateTime: Indicates when the guidance response was processed. :param performer: Provides a reference to the device that performed the guidance. :param reasonCode: Describes the reason for the guidance response in coded or textual form. :param reasonReference: Indicates the reason the request was initiated. This is typically provided as a parameter to the evaluation and echoed by the service, although for some use cases, such as subscription- or event-based scenarios, it may provide an indication of the cause for the response. :param note: Provides a mechanism to communicate additional information about the response. :param evaluationMessage: Messages resulting from the evaluation of the artifact or artifacts. As part of evaluating the request, the engine may produce informational or warning messages. These messages will be provided by this element. :param outputParameters: The output parameters of the evaluation, if any. Many modules will result in the return of specific resources such as procedure or communication requests that are returned as part of the operation result. However, modules may define specific outputs that would be returned as the result of the evaluation, and these would be returned in this element. :param result: The actions, if any, produced by the evaluation of the artifact. :param dataRequirement: If the evaluation could not be completed due to lack of information, or additional information would potentially result in a more accurate response, this element will a description of the data required in order to proceed with the evaluation. A subsequent request to the service should include this data. """ super().__init__( resourceType="GuidanceResponse", id_=id_, meta=meta, implicitRules=implicitRules, language=language, text=text, contained=contained, extension=extension, modifierExtension=modifierExtension, requestIdentifier=requestIdentifier, identifier=identifier, moduleUri=moduleUri, moduleCanonical=moduleCanonical, moduleCodeableConcept=moduleCodeableConcept, status=status, subject=subject, encounter=encounter, occurrenceDateTime=occurrenceDateTime, performer=performer, reasonCode=reasonCode, reasonReference=reasonReference, note=note, evaluationMessage=evaluationMessage, outputParameters=outputParameters, result=result, dataRequirement=dataRequirement, ) self.use_date_for = use_date_for def get_schema( self, include_extension: bool, extension_fields: Optional[List[str]] = None ) -> Optional[Union[StructType, DataType]]: return GuidanceResponseSchema.get_schema( include_extension=include_extension, extension_fields=extension_fields, use_date_for=self.use_date_for, )
[]
2024-01-10
FloTeu/llm-prompt-engineering-generator
src~llm_prompting_gen~models~prompt_engineering.py
import json import logging from typing import Optional, List, Union, Dict from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, PromptTemplate, AIMessagePromptTemplate from langchain.prompts.chat import BaseMessagePromptTemplate from pydantic import BaseModel, Field, ConfigDict from llm_prompting_gen.constants import INSTRUCTOR_USER_NAME class FewShotHumanAIExample(BaseModel): human: Optional[str] = Field(None, description="Example human message.", examples=["What is 2 + 3?"]) ai: str = Field(description="Example ai message as answer to the human message. Or standalone example without human message.", examples=["5"]) class PromptElements(BaseModel): """ Defines all elements of a prompt """ role: Optional[str] = Field(None, description="The role in which the LLM should respond") instruction: Optional[str] = Field(None, description="The task of the LLM") context: Optional[str] = Field(None, description="Context with relevant information to solve the task") output_format: Optional[str] = Field(None, description="Description how the LLM output format should look like") examples: Optional[Union[List[FewShotHumanAIExample], List[str]]] = Field( None, description="Few shot examples. Can be either human ai interactions or a list of examples") input: Optional[str] = Field( None, description="Target which the LLM should execute the task on. Could be for example a user question, or a text block to summarize.") model_config = ConfigDict( extra='allow', ) def is_any_set(self) -> bool: """ Whether at least one prompt element is set. Otherwise, an LLM cannot handle prompt input and therefore False is returned """ return any([self.role, self.instruction, self.context, self.examples, self.input, self.output_format]) def get_example_system_prompt_template(self) -> SystemMessagePromptTemplate: """Transforms list of examples into SystemMessagePromptTemplate "Example <n>:" is appended as prefix of each example Example: self.examples = ["positive", "negative", "neutral"] t = self.get_example_system_prompt_template() # t equals (pseudo representation of SystemMessagePromptTemplate) SystemMessagePromptTemplate(''' Example 1: positive Example 2: negative Example 3: neutral ''') """ assert self.examples, "examples are not set yet" assert type(self.examples) == list and type(self.examples[0]) == str, "examples is not a list of strings" prompt_template = "" for i, example in enumerate(self.examples): prompt_template += f"\nExample {i+1}: {example}" return SystemMessagePromptTemplate.from_template(prompt_template, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) def get_example_msg_prompt_templates(self) -> List[Union[SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate]]: """ Returns a list of message prompt template depending on the example format If human_ai_interaction is set: Returns list of langchain human and ai prompt templates If examples are set and no human_ai_interaction: Returns list of single langchain system message prompt template """ assert self.examples, "examples are not set yet" # Return SystemMessagePromptTemplate in case only simple string examples are set if self.examples and not type(self.examples[0]) == FewShotHumanAIExample: return [self.get_example_system_prompt_template()] assert type(self.examples[0]) == FewShotHumanAIExample, "human ai interaction examples are not set yet" messages = [] for example in self.examples: messages.append(HumanMessagePromptTemplate.from_template(example.human)) messages.append(AIMessagePromptTemplate.from_template(example.ai)) return messages class PromptEngineeringMessages: """ Dataclass which contains all messages to create a LLM Output based on prompt engineering techniques. Transforms strings into langchain prompt template messages. """ def __init__(self, messages: Dict[str, Union[BaseMessagePromptTemplate, List[BaseMessagePromptTemplate]]]): """ Note: messages can contain custom keys and do not require but can match the PromptElements fields. Examples are a special case, where we have multiple BaseMessagePromptTemplate for one key. :param messages: Look up dict matching prompt element key with corresponding PromptTemplate """ self.messages = messages @classmethod def from_pydantic(cls, pe_elements: PromptElements): """Init class by PromptElements pydantic""" messages = {} if pe_elements.role: messages["role"] = SystemMessagePromptTemplate.from_template(pe_elements.role, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) if pe_elements.instruction: messages["instruction"] = SystemMessagePromptTemplate.from_template( pe_elements.instruction, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) if pe_elements.context: messages["context"] = SystemMessagePromptTemplate.from_template(pe_elements.context, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) if pe_elements.output_format: # Create output format system message output_format_prompt = PromptTemplate( template="{format_instructions}", input_variables=[], partial_variables={"format_instructions": pe_elements.output_format} ) messages["output_format"] = SystemMessagePromptTemplate(prompt=output_format_prompt, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) if pe_elements.examples: messages["examples"] = pe_elements.get_example_msg_prompt_templates() if pe_elements.input: messages["input"] = HumanMessagePromptTemplate.from_template(pe_elements.input) # Add extra fields as system messages for extra_field, extra_value in pe_elements.model_extra.items(): messages[extra_field] = SystemMessagePromptTemplate.from_template(extra_value, additional_kwargs={"name": INSTRUCTOR_USER_NAME}) return cls(messages=messages) @classmethod def from_json(cls, file_path): """Init class by json file""" with open(file_path, "r") as fp: data_dict = json.load(fp) return cls.from_pydantic(PromptElements(**data_dict)) @classmethod def from_yaml(cls, file_path): # TODO raise NotImplementedError def get_flattened_messages(self, message_order: Optional[List[str]] = None) -> List[BaseMessagePromptTemplate]: """Returns list of valid messages. Examples are flattened in order to get one dimension output. Optionally a message order can be provided to define the output order """ flattened_messages = [] message_keys = message_order or list(self.messages.keys()) for message_key in message_keys: if message_key not in self.messages: logging.warning( f"messages do not contain key '{message_key}'") continue message_or_list = self.messages[message_key] # Case message is valid if isinstance(message_or_list, BaseMessagePromptTemplate): flattened_messages.append(message_or_list) # Case list of messages elif isinstance(message_or_list, list): for message in message_or_list: # check if message is valid if isinstance(message, BaseMessagePromptTemplate): flattened_messages.append(message) return flattened_messages def get_chat_prompt_template(self, message_order: Optional[List[str]] = None) -> ChatPromptTemplate: """Combines all prompt element messages into one chat prompt template :param message_order: Optional list of prompt element keys, to define message order of final chat prompt template :return: """ if message_order: # Print warning in case message_order does not include all fields available excluded_fields = set(self.messages.keys()) - set(message_order) if excluded_fields: logging.warning(f"'message_order' does not include fields {excluded_fields}. They will be ignored for chat prompt template creation") return ChatPromptTemplate.from_messages(self.get_flattened_messages(message_order))
[ "{format_instructions}", "format_instructions" ]
2024-01-10
QuantumBear/supersonic
chat~core~src~main~python~services~sql~prompt_maker.py
# -*- coding:utf-8 -*- import os import sys from typing import List, Mapping sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(os.path.dirname(os.path.abspath(__file__))) from util.logging_utils import logger from langchain.prompts import PromptTemplate from langchain.prompts.few_shot import FewShotPromptTemplate from langchain.prompts.example_selector import SemanticSimilarityExampleSelector def schema_linking_exampler( user_query: str, domain_name: str, fields_list: List[str], prior_schema_links: Mapping[str, str], example_selector: SemanticSimilarityExampleSelector, ) -> str: prior_schema_links_str = ( "[" + ",".join(["""'{}'->{}""".format(k, v) for k, v in prior_schema_links.items()]) + "]" ) example_prompt_template = PromptTemplate( input_variables=[ "table_name", "fields_list", "prior_schema_links", "question", "analysis", "schema_links", ], template="Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž:{analysis} ๆ‰€ไปฅSchema_linksๆ˜ฏ:\nSchema_links:{schema_links}", ) instruction = "# ๆ นๆฎๆ•ฐๆฎๅบ“็š„่กจ็ป“ๆž„,ๅ‚่€ƒๅ…ˆ้ชŒไฟกๆฏ,ๆ‰พๅ‡บไธบๆฏไธช้—ฎ้ข˜็”ŸๆˆSQLๆŸฅ่ฏข่ฏญๅฅ็š„schema_links" schema_linking_prompt = "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž: ่ฎฉๆˆ‘ไปฌไธ€ๆญฅไธ€ๆญฅๅœฐๆ€่€ƒใ€‚" schema_linking_example_prompt_template = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt_template, example_separator="\n\n", prefix=instruction, input_variables=["table_name", "fields_list", "prior_schema_links", "question"], suffix=schema_linking_prompt, ) schema_linking_example_prompt = schema_linking_example_prompt_template.format( table_name=domain_name, fields_list=fields_list, prior_schema_links=prior_schema_links_str, question=user_query, ) return schema_linking_example_prompt def sql_exampler( user_query: str, domain_name: str, schema_link_str: str, data_date: str, example_selector: SemanticSimilarityExampleSelector, ) -> str: instruction = "# ๆ นๆฎschema_linksไธบๆฏไธช้—ฎ้ข˜็”ŸๆˆSQLๆŸฅ่ฏข่ฏญๅฅ" sql_example_prompt_template = PromptTemplate( input_variables=[ "question", "current_date", "table_name", "schema_links", "sql", ], template="้—ฎ้ข˜:{question}\nCurrent_date:{current_date}\nTable {table_name}\nSchema_links:{schema_links}\nSQL:{sql}", ) sql_prompt = "้—ฎ้ข˜:{question}\nCurrent_date:{current_date}\nTable {table_name}\nSchema_links:{schema_links}\nSQL:" sql_example_prompt_template = FewShotPromptTemplate( example_selector=example_selector, example_prompt=sql_example_prompt_template, example_separator="\n\n", prefix=instruction, input_variables=["question", "current_date", "table_name", "schema_links"], suffix=sql_prompt, ) sql_example_prompt = sql_example_prompt_template.format( question=user_query, current_date=data_date, table_name=domain_name, schema_links=schema_link_str, ) return sql_example_prompt def schema_linking_sql_combo_examplar( user_query: str, domain_name: str, data_date: str, fields_list: List[str], prior_schema_links: Mapping[str, str], example_selector: SemanticSimilarityExampleSelector, ) -> str: prior_schema_links_str = ( "[" + ",".join(["""'{}'->{}""".format(k, v) for k, v in prior_schema_links.items()]) + "]" ) example_prompt_template = PromptTemplate( input_variables=[ "table_name", "fields_list", "prior_schema_links", "current_date", "question", "analysis", "schema_links", "sql", ], template="Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\nCurrent_date:{current_date}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž:{analysis} ๆ‰€ไปฅSchema_linksๆ˜ฏ:\nSchema_links:{schema_links}\nSQL:{sql}", ) instruction = ( "# ๆ นๆฎๆ•ฐๆฎๅบ“็š„่กจ็ป“ๆž„,ๅ‚่€ƒๅ…ˆ้ชŒไฟกๆฏ,ๆ‰พๅ‡บไธบๆฏไธช้—ฎ้ข˜็”ŸๆˆSQLๆŸฅ่ฏข่ฏญๅฅ็š„schema_links,ๅ†ๆ นๆฎschema_linksไธบๆฏไธช้—ฎ้ข˜็”ŸๆˆSQLๆŸฅ่ฏข่ฏญๅฅ" ) schema_linking_sql_combo_prompt = "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\nCurrent_date:{current_date}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž: ่ฎฉๆˆ‘ไปฌไธ€ๆญฅไธ€ๆญฅๅœฐๆ€่€ƒใ€‚" schema_linking_sql_combo_example_prompt_template = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt_template, example_separator="\n\n", prefix=instruction, input_variables=[ "table_name", "fields_list", "prior_schema_links", "current_date", "question", ], suffix=schema_linking_sql_combo_prompt, ) schema_linking_sql_combo_example_prompt = ( schema_linking_sql_combo_example_prompt_template.format( table_name=domain_name, fields_list=fields_list, prior_schema_links=prior_schema_links_str, current_date=data_date, question=user_query, ) ) return schema_linking_sql_combo_example_prompt
[ "\n\n", "table_name", "prior_schema_links", "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž: ่ฎฉๆˆ‘ไปฌไธ€ๆญฅไธ€ๆญฅๅœฐๆ€่€ƒใ€‚", "้—ฎ้ข˜:{question}\nCurrent_date:{current_date}\nTable {table_name}\nSchema_links:{schema_links}\nSQL:{sql}", "question", "current_date", "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\nCurrent_date:{current_date}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž: ่ฎฉๆˆ‘ไปฌไธ€ๆญฅไธ€ๆญฅๅœฐๆ€่€ƒใ€‚", "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\nCurrent_date:{current_date}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž:{analysis} ๆ‰€ไปฅSchema_linksๆ˜ฏ:\nSchema_links:{schema_links}\nSQL:{sql}", "้—ฎ้ข˜:{question}\nCurrent_date:{current_date}\nTable {table_name}\nSchema_links:{schema_links}\nSQL:", "fields_list", "Table {table_name}, columns = {fields_list}, prior_schema_links = {prior_schema_links}\n้—ฎ้ข˜:{question}\nๅˆ†ๆž:{analysis} ๆ‰€ไปฅSchema_linksๆ˜ฏ:\nSchema_links:{schema_links}", "analysis", "schema_links" ]
2024-01-10
QuantumBear/supersonic
chat~core~src~main~python~services~sql~constructor.py
# -*- coding:utf-8 -*- import os import sys from typing import List, Mapping sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(os.path.dirname(os.path.abspath(__file__))) from util.logging_utils import logger from langchain.vectorstores import Chroma from langchain.prompts.example_selector import SemanticSimilarityExampleSelector from few_shot_example.sql_exampler import examplars as sql_examplars from util.text2vec import hg_embedding from util.chromadb_instance import client as chromadb_client, empty_chroma_collection_2 from config.config_parse import TEXT2DSL_COLLECTION_NAME, TEXT2DSL_FEW_SHOTS_EXAMPLE_NUM def reload_sql_example_collection( vectorstore: Chroma, sql_examplars: List[Mapping[str, str]], sql_example_selector: SemanticSimilarityExampleSelector, example_nums: int, ): logger.info("original sql_examples_collection size: {}", vectorstore._collection.count()) new_collection = empty_chroma_collection_2(collection=vectorstore._collection) vectorstore._collection = new_collection logger.info("emptied sql_examples_collection size: {}", vectorstore._collection.count()) sql_example_selector = SemanticSimilarityExampleSelector( vectorstore=sql_examples_vectorstore, k=example_nums, input_keys=["question"], example_keys=[ "table_name", "fields_list", "prior_schema_links", "question", "analysis", "schema_links", "current_date", "sql", ], ) for example in sql_examplars: sql_example_selector.add_example(example) logger.info("reloaded sql_examples_collection size: {}", vectorstore._collection.count()) return vectorstore, sql_example_selector sql_examples_vectorstore = Chroma( collection_name=TEXT2DSL_COLLECTION_NAME, embedding_function=hg_embedding, client=chromadb_client, ) example_nums = TEXT2DSL_FEW_SHOTS_EXAMPLE_NUM sql_example_selector = SemanticSimilarityExampleSelector( vectorstore=sql_examples_vectorstore, k=example_nums, input_keys=["question"], example_keys=[ "table_name", "fields_list", "prior_schema_links", "question", "analysis", "schema_links", "current_date", "sql", ], ) if sql_examples_vectorstore._collection.count() > 0: logger.info("examples already in sql_vectorstore") logger.info("init sql_vectorstore size: {}", sql_examples_vectorstore._collection.count()) logger.info("sql_examplars size: {}", len(sql_examplars)) sql_examples_vectorstore, sql_example_selector = reload_sql_example_collection( sql_examples_vectorstore, sql_examplars, sql_example_selector, example_nums ) logger.info("added sql_vectorstore size: {}", sql_examples_vectorstore._collection.count())
[]
2024-01-10
ArshiaPessaran/PDFQueries
Chatbot~llmChainResponse.py
import os from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, ConversationalRetrievalChain from langchain.agents import load_tools, initialize_agent, AgentType from pdfDocLoader import vectordb # from llamaapi import LlamaAPI # llama = LlamaAPI("Your_API_Token") # from langchain_experimental.llms import ChatLlamaAPI # OpenAI API Key from APIKey import OpenAIKey if OpenAIKey is None: raise ValueError("OPEN_API_KEY is not set. Create APIKey.py file that defines variable OpenAIKey") os.environ["OPENAI_API_KEY"] = OpenAIKey # Chatbot Chain chatBotResponse = ConversationalRetrievalChain.from_llm( ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo"), vectordb.as_retriever(), return_source_documents=False, verbose=False ) yellow = "\033[0;33m" green = "\033[0;32m" white = "\033[0;39m" chat_history = [] print(f"{yellow}------------------------------------") print('Make queries about your documents') print('------------------------------------') while True: query = input(f"{green}Prompt (type q to quit): ") if query == "q": sys.exit() if query == '': continue result = chatBotResponse( {"question": query, "chat_history": chat_history}) print(f"{white}Answer: " + result["answer"]) chat_history.append((query, result["answer"]))
[]
2024-01-10
ArshiaPessaran/PDFQueries
Chatbot~pdfDocLoader.py
import os from langchain.llms import OpenAI from langchain.chains import LLMChain, ConversationalRetrievalChain from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings # Import OpenAI API Key from APIKey import OpenAIKey if OpenAIKey is None: raise ValueError("OPEN_API_KEY is not set. Create APIKey.py file that defines variable OpenAIKey") os.environ["OPENAI_API_KEY"] = OpenAIKey # Create langchain document from multiple PDF multiDoc = [] for file in os.listdir('./sampleFilesAndWebsites/'): if file.endswith('.pdf'): pdfPath = './sampleFilesAndWebsites/' + file loader = PyPDFLoader(pdfPath) multiDoc.extend(loader.load()) elif file.endswith('.docx') or file.endswith('.doc'): docPath = './sampleFilesAndWebsites/' + file loader = Docx2txtLoader(docPath) multiDoc.extend(loader.load()) elif file.endswith('.txt'): txtPath = './sampleFilesAndWebsites' + file loader = TextLoader(txtPath) multiDoc.extend(loader.load()) # Split into chunks text_splitter = CharacterTextSplitter(chunk_size = 1000, chunk_overlap=200) chunks = text_splitter.split_documents(multiDoc) #Convert to embeddings. Store embeddings from text chunks inside ./embeddings directory vectordb = Chroma.from_documents( chunks, embedding = OpenAIEmbeddings(), persist_directory = './sampleFilesAndWebsites/embeddings' ) vectordb.persist()
[]
2024-01-10
trumanng10/langchain003
chat_with_documents_01.py
import streamlit as st from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma # loading PDF, DOCX and TXT files as LangChain Documents def load_document(file): import os name, extension = os.path.splitext(file) if extension == '.pdf': from langchain.document_loaders import PyPDFLoader print(f'Loading {file}') loader = PyPDFLoader(file) elif extension == '.docx': from langchain.document_loaders import Docx2txtLoader print(f'Loading {file}') loader = Docx2txtLoader(file) elif extension == '.txt': from langchain.document_loaders import TextLoader loader = TextLoader(file) else: print('Document format is not supported!') return None data = loader.load() return data # splitting data in chunks def chunk_data(data, chunk_size=256, chunk_overlap=20): from langchain.text_splitter import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) chunks = text_splitter.split_documents(data) return chunks # create embeddings using OpenAIEmbeddings() and save them in a Chroma vector store def create_embeddings(chunks): embeddings = OpenAIEmbeddings() vector_store = Chroma.from_documents(chunks, embeddings) return vector_store def ask_and_get_answer(vector_store, q, k=3): from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=1) retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': k}) chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) answer = chain.run(q) return answer # calculate embedding cost using tiktoken def calculate_embedding_cost(texts): import tiktoken enc = tiktoken.encoding_for_model('text-embedding-ada-002') total_tokens = sum([len(enc.encode(page.page_content)) for page in texts]) # print(f'Total Tokens: {total_tokens}') # print(f'Embedding Cost in USD: {total_tokens / 1000 * 0.0004:.6f}') return total_tokens, total_tokens / 1000 * 0.0004 # clear the chat history from streamlit session state def clear_history(): if 'history' in st.session_state: del st.session_state['history'] if __name__ == "__main__": import os # loading the OpenAI api key from .env from dotenv import load_dotenv, find_dotenv load_dotenv(find_dotenv(), override=True) st.image('img.png') st.subheader('LLM Question-Answering Application ๐Ÿค–') with st.sidebar: # text_input for the OpenAI API key (alternative to python-dotenv and .env) api_key = st.text_input('OpenAI API Key:', type='password') if api_key: os.environ['OPENAI_API_KEY'] = api_key # file uploader widget uploaded_file = st.file_uploader('Upload a file:', type=['pdf', 'docx', 'txt']) # chunk size number widget chunk_size = st.number_input('Chunk size:', min_value=100, max_value=2048, value=512, on_change=clear_history) # k number input widget k = st.number_input('k', min_value=1, max_value=20, value=3, on_change=clear_history) # add data button widget add_data = st.button('Add Data', on_click=clear_history) if uploaded_file and add_data: # if the user browsed a file with st.spinner('Reading, chunking and embedding file ...'): # writing the file from RAM to the current directory on disk bytes_data = uploaded_file.read() file_name = os.path.join('./', uploaded_file.name) with open(file_name, 'wb') as f: f.write(bytes_data) data = load_document(file_name) chunks = chunk_data(data, chunk_size=chunk_size) st.write(f'Chunk size: {chunk_size}, Chunks: {len(chunks)}') tokens, embedding_cost = calculate_embedding_cost(chunks) st.write(f'Embedding cost: ${embedding_cost:.4f}') # creating the embeddings and returning the Chroma vector store vector_store = create_embeddings(chunks) # saving the vector store in the streamlit session state (to be persistent between reruns) st.session_state.vs = vector_store st.success('File uploaded, chunked and embedded successfully.') # user's question text input widget q = st.text_input('Ask a question about the content of your file:') if q: # if the user entered a question and hit enter standard_answer = "Answer only based on the text you received as input. Don't search external sources. " \ "If you can't answer then return `I DONT KNOW`." q = f"{q} {standard_answer}" if 'vs' in st.session_state: # if there's the vector store (user uploaded, split and embedded a file) vector_store = st.session_state.vs st.write(f'k: {k}') answer = ask_and_get_answer(vector_store, q, k) # text area widget for the LLM answer st.text_area('LLM Answer: ', value=answer) st.divider() # if there's no chat history in the session state, create it if 'history' not in st.session_state: st.session_state.history = '' # the current question and answer value = f'Q: {q} \nA: {answer}' st.session_state.history = f'{value} \n {"-" * 100} \n {st.session_state.history}' h = st.session_state.history # text area widget for the chat history st.text_area(label='Chat History', value=h, key='history', height=400) # run the app: streamlit run ./chat_with_documents.py
[]
2024-01-10
mohit-raghavendra/gt-policy-bot
pinecone_index.py
import os import pinecone import time import yaml import pandas as pd from langchain.document_loaders import DataFrameLoader from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores.pinecone import Pinecone from typing import List from dotenv import load_dotenv from pathlib import Path class PinceconeIndex: def __init__(self, index_name: str, model_name: str): self.index_name = index_name self._embeddingModel = HuggingFaceEmbeddings(model_name=model_name) def connect_index(self, embedding_dimension: int, delete_existing: bool = False): index_name = self.index_name # load pinecone env variables within Google Colab if (not os.getenv("PINECONE_KEY")) or (not os.getenv("PINECONE_ENV")): dotenv_path = Path("/content/gt-policy-bot/config.env") load_dotenv(dotenv_path=dotenv_path) pinecone.init( api_key=os.getenv("PINECONE_KEY"), environment=os.getenv("PINECONE_ENV"), ) if index_name in pinecone.list_indexes() and delete_existing: pinecone.delete_index(index_name) if index_name not in pinecone.list_indexes(): pinecone.create_index(index_name, dimension=embedding_dimension) index = pinecone.Index(index_name) pinecone.describe_index(index_name) self._index = index def upsert_docs(self, df: pd.DataFrame, text_col: str): loader = DataFrameLoader(df, page_content_column=text_col) docs = loader.load() Pinecone.from_documents(docs, self._embeddingModel, index_name=self.index_name) def get_embedding_model(self): return self._embeddingModel def get_index_name(self): return self.index_name def query(self, query: str, top_k: int = 5) -> List[str]: docsearch = Pinecone.from_existing_index(self.index_name, self._embeddingModel) res = docsearch.similarity_search(query, k=top_k) return [doc.page_content for doc in res] if __name__ == "__main__": config_path = "config.yml" with open("config.yml", "r") as file: config = yaml.safe_load(file) print(config) data_path = config["paths"]["data_path"] project = config["paths"]["project"] format = ".csv" index_name = config["pinecone"]["index-name"] embedding_model = config["sentence-transformers"]["model-name"] embedding_dimension = config["sentence-transformers"]["embedding-dimension"] delete_existing = True if config["paths"]["chunking"] == "manual": print("Using manual chunking") file_path_embedding = config["paths"]["manual_chunk_file"] df = pd.read_csv(file_path_embedding, header=None, names=["chunks"]) else: print("Using automatic chunking") file_path_embedding = config["paths"]["auto_chunk_file"] df = pd.read_csv(file_path_embedding, index_col=0) print(df) start_time = time.time() index = PinceconeIndex(index_name, embedding_model) index.connect_index(embedding_dimension, delete_existing) index.upsert_docs(df, "chunks") end_time = time.time() print(f"Indexing took {end_time - start_time} seconds") index = PinceconeIndex(index_name, embedding_model) index.connect_index(embedding_dimension, delete_existing=False) query = "When was the student code of conduct last revised?" res = index.query(query, top_k=5) # assert len(res) == 5 print(res)
[]
2024-01-10
mohit-raghavendra/gt-policy-bot
utils~faiss_index.py
import time import yaml import pandas as pd from langchain.document_loaders import DataFrameLoader from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores.faiss import FAISS from typing import List class FaissIndex: def __init__(self, index_name: str, model_name: str): self.index_name = index_name self._embeddingModel = HuggingFaceEmbeddings(model_name=model_name) def connect_index(self, embedding_dimension: int, delete_existing: bool = False): ... def upsert_docs(self, df: pd.DataFrame, text_col: str): loader = DataFrameLoader(df, page_content_column=text_col) docs = loader.load() self._db = FAISS.from_documents(docs, self._embeddingModel) def query(self, query: str, top_k: int = 5) -> List[str]: res = self._db.similarity_search(query, k=top_k) return [doc.page_content for doc in res] if __name__ == "__main__": config_path = "config.yml" with open("config.yml", "r") as file: config = yaml.safe_load(file) print(config) data_path = config["paths"]["data_path"] project = config["paths"]["project"] format = ".csv" index_name = config["pinecone"]["index-name"] embedding_model = config["sentence-transformers"]["model-name"] embedding_dimension = config["sentence-transformers"]["embedding-dimension"] delete_existing = True file_path_embedding = data_path + project + format df = pd.read_csv(file_path_embedding, index_col=0) print(df.head()) start_time = time.time() index = FaissIndex(index_name, embedding_model) index.connect_index(embedding_dimension, delete_existing) index.upsert_docs(df, "chunks") end_time = time.time() print(f"Indexing took {end_time - start_time} seconds") query = "When was the student code of conduct last revised?" res = index.query(query, top_k=5) assert len(res) == 5 print(res)
[]
2024-01-10
monum/llm-exploration
streamlit_app~index_documents.py
import os os.environ['OPENAI_API_KEY']= "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" from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, LLMPredictor, PromptHelper, StorageContext from langchain.chat_models import ChatOpenAI max_input_size = 4096 num_outputs = 100 max_chunk_overlap = 20 chunk_size_limit = 600 prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap = max_chunk_overlap, chunk_size_limit = chunk_size_limit) llm_predictor = LLMPredictor( llm = ChatOpenAI( temperature = 0.7, model_name = "gpt-3.5-turbo", max_tokens = num_outputs ) ) documents = SimpleDirectoryReader("data").load_data() index = GPTVectorStoreIndex.from_documents( documents, llm_predictor = llm_predictor, prompt_helper = prompt_helper) index.storage_context.persist(persist_dir=".")
[]
2024-01-10
map-A/own-chatgpt-web
get-accessToken.py
from OpenAIAuth import Auth0 import sys import yaml # ็ฌฌไธ€ไธชๅ‚ๆ•ฐๆ˜ฏ่„šๆœฌๅ็งฐ๏ผŒๅ› ๆญค้œ€่ฆไปŽ็ฌฌไบŒไธชๅ‚ๆ•ฐๅผ€ๅง‹่ฟ›่กŒๅค„็† email = sys.argv[1] password = sys.argv[2] your_web_password = sys.argv[3] auth = Auth0(email, password) access_token = auth.get_access_token() ## ๅฐ†access_token ๅ†™ๅ…ฅๅˆฐymlๆ–‡ไปถไธญ with open('docker-compose.yml', 'r') as file: data = yaml.safe_load(file) data['services']['app']['environment']['OPENAI_ACCESS_TOKEN']=access_token data['services']['app']['environment']['AUTH_SECRET_KEY']=your_web_password # ๅฐ†ๆ›ดๆ”นไฟๅญ˜ๅ›žๆ–‡ไปถ with open('docker-compose.yml', 'w') as file: yaml.safe_dump(data, file,)
[]
2024-01-10
davidpinhas/freedom55
fd55~cli~chatgpt_client~chatgpt_cli.py
import time import re import openai import shutil import logging import itertools from fd55.utils.fd55_config import Config config = Config() class ChatGPT: def __init__(self): openai.api_key = f"{str(config.get('AI', 'api_key'))}" self.engine = "text-davinci-003" def backup_modified_file(file): """ Create a backup for the modified file """ shutil.copy2(file, file + '.bak') logging.info(f"Created backup of '{file}' as '{file}.bak'") def remove_first_comment(file): """ Remove all comments at the head of the file """ with open(file, 'r') as f: lines = f.readlines() with open(file, 'w') as f: found_valid_line = False for line in lines: if not found_valid_line: if re.search( r"[a-zA-Z0-9ร€-รฟ]+", line) and not ( line.startswith(" ") or line.startswith("#") and not line.startswith("#!")): found_valid_line = True f.write(line) else: f.write(line) def iterate_code_rewrite(self, file=None, iterations=0, prompt=None): """ Iterate over a file with the same prompt """ if iterations > 0 and file is not None: retry_count = 0 logging.info(f"Backing up file '{file}'") ChatGPT.backup_modified_file(file) ChatGPT.remove_first_comment(file=file) logging.info(f"Running {iterations} iterations on file '{file}'") for i in range(iterations): with open(file, 'r') as f: file_content = f.read() new_prompt = "" new_prompt += prompt + '. File to modify: \n' + \ file_content + "\n\n Return the improved version in full." logging.debug(f"Manifested new prompt:\n{prompt}") while True: output = openai.Completion.create( engine=self.engine, prompt=new_prompt, max_tokens=2048, temperature=0, stream=False ) if not output.choices[0].text: retry_count += 1 logging.warning( f"Received empty output from OpenAI, retrying...") if retry_count == 10: logging.error( "Failed to get a response from OpenAI after 10 retries") time.sleep(5) pass break with open(file, 'w') as f: f.write(output.choices[0].text) logging.info(f"Iteration {i+1} - Finished updating file") logging.info(f"All iterations on file '{file}' finished") def send_openai_request( self, prompt, full_output=False, file=None, iterations=None): """ Send OpenAI request """ try: logging.info("Creating request to OpenAI") if file is not None: logging.info(f"Manifesting prompt with file '{file}'") ChatGPT.remove_first_comment(file) with open(file, 'r') as f: file_content = f.read() original_prompt = prompt prompt += prompt + '. File to improve: \n' + file_content + \ "\n\n Return the improved version in full." logging.debug(f"Manifested new prompt:\n{prompt}") if iterations: iterations = int(iterations) if int(iterations) and iterations > 0: ChatGPT().iterate_code_rewrite( file=file, iterations=iterations, prompt=original_prompt) exit() if file is None and iterations: logging.error(f"Iterations argument requires a '--file'") exit() stream = False if full_output else True output = openai.Completion.create( engine=self.engine, prompt=prompt, max_tokens=2048, temperature=0, stream=stream ) if full_output is not False: logging.info("Waiting for full output generation") full_output = output.choices[0].text print(full_output) logging.info("Finished generation") else: logging.info("Recieved answer:") print("#################################") print("######## OpenAI Response ########") print("#################################\n") for event in itertools.islice(output, 1, None): event_text = event['choices'][0]['text'] print(f"{event_text}", end="", flush=True) print("\n\n#################################") except Exception as e: logging.error(f"Failed to get a response from OpenAI, Reason: {e}") exit()
[ "PLACEHOLDER. File to modify: \nPLACEHOLDER\n\n Return the improved version in full.", "PLACEHOLDER. File to improve: \nPLACEHOLDER\n\n Return the improved version in full." ]
2024-01-10
Sofus-Wen/prototype-personalized-learning-with-AI
embedding_doc.py
import os from config import OPENAI_API_KEY, PINECONE_API_KEY, PINECONE_API_ENVIRONMENT from constants import PINECONE_INDEX_NAME from langchain.document_loaders import UnstructuredPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import Pinecone from langchain.embeddings.openai import OpenAIEmbeddings import pinecone import chromadb # Added for ChromaDB chroma_client = chromadb.Client() # Added for ChromaDB # Set API key for OpenAI and Pinecone os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_API_ENVIRONMENT) # Check if index exists, if not create it print('Checking if index exists...') if PINECONE_INDEX_NAME not in pinecone.list_indexes(): print('Index does not exist, creating index...') pinecone.create_index(name=PINECONE_INDEX_NAME, metric='cosine', dimension=1536) # List of PDFs to process (Changed Variable) pdf_list = [ 'informatik.pdf', 'samfundsfag-grundforlob.pdf', 'รธkonomisk-grundforlob.pdf', 'marketing-en-grundbog-i-afsรฆtning.pdf', 'larebog-i-matematik-hhx-1.pdf', 'international-รธkonomi.pdf', 'ind-i-sproget-hhx-hรฅndbog-til-almen-sprogforstรฅelse.pdf', 'virksomhedsokonomi.pdf', 'fortallingens-spejl-grundforlob-i-dansk.pdf', 'ccc-company-culture-and-communication-grundforlob-i-engelsk.pdf', 'aus-aktuellem-anlass-grundforlob-i-tysk.pdf', # Add more PDFs here ] # Loop to handle multiple PDFs (New Addition) for pdf in pdf_list: print(f'Loading document: {pdf}') loader = UnstructuredPDFLoader(pdf) data = loader.load() print(f'Loaded a PDF with {len(data)} pages') print(f'There are {len(data[0].page_content)} characters in your document') # Chunk data into smaller documents text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200) # Changed chunk_size and chunk_overlap texts = text_splitter.split_documents(data) print(f'Split the document into {len(texts)} smaller documents') # Create embeddings and index from your documents print('Creating embeddings and index...') embeddings = OpenAIEmbeddings(client='') docsearch = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=PINECONE_INDEX_NAME) print('Done!')
[]
2024-01-10
GitSumito/CodeArsenal
openai~transcription.py
import os import openai import librosa from pydub import AudioSegment from tempfile import NamedTemporaryFile # OpenAIใฎAPIใ‚ญใƒผใ‚’็’ฐๅขƒๅค‰ๆ•ฐใ‹ใ‚‰ๅ–ๅพ—ใ—ใพใ™ใ€‚ openai.api_key = os.getenv('OPENAI_API_KEY') # mp3ใƒ•ใ‚กใ‚คใƒซๅใ‚’็’ฐๅขƒๅค‰ๆ•ฐใ‹ใ‚‰ๅ–ๅพ—ใ—ใพใ™ใ€‚ mp3_filename = os.getenv('MP3_FILENAME') audio_file= open(mp3_filename, "rb") transcript = openai.Audio.transcribe("whisper-1", audio_file) print(transcript.text) # ็ตๆžœใ‚’ใƒ†ใ‚ญใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ—ใพใ™ใ€‚ with open('transcription.txt', 'w') as file: file.write(transcript.text) print("Transcription saved to 'transcription.txt'")
[]
2024-01-10
WilliamSteenbergen/gym-SmartPrimer
gym_SmartPrimer~examples~exampleComparison.py
import gym import numpy as np import gym_SmartPrimer.agents.baselineV2 as Baseline import matplotlib.pyplot as plt from rlgraph.agents import Agent from rlgraph.environments import OpenAIGymEnv from rlgraph.execution import SingleThreadedWorker import json import os np.random.seed(2) env = gym.make('gym_SmartPrimer:SmartPrimer-realistic-v2') agent = Baseline.BaselineAgent(env.action_space) episode_count = 1000 reward = 0 done = False for i in range(episode_count): if i == 100: stop = 1 ob = env.reset() while True: action = agent.act(ob, reward, done) ob, reward, done, Baseinfo = env.step(action) if done: agent.reset() break np.random.seed(2) agent_config_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + '/agents/ppoSmartPrimer_config.json' with open(agent_config_path, 'rt') as fp: agent_config = json.load(fp) env = OpenAIGymEnv.from_spec({ "type": "openai", "gym_env": 'gym_SmartPrimer:SmartPrimer-realistic-v2' }) agent = Agent.from_spec( agent_config, state_space=env.state_space, action_space=env.action_space ) episode_returns = [] def episode_finished_callback(episode_return, duration, timesteps, *args, **kwargs): episode_returns.append(episode_return) if len(episode_returns) % 100 == 0: print("Episode {} finished: reward={:.2f}, average reward={:.2f}.".format( len(episode_returns), episode_return, np.mean(episode_returns[-100:]) )) worker = SingleThreadedWorker(env_spec=lambda: env, agent=agent, render=False, worker_executes_preprocessing=False, episode_finish_callback=episode_finished_callback) # Use exploration is true for training, false for evaluation. worker.execute_episodes(1000, use_exploration=True) def plotting(Baseline, PPO, quit, finished, quitBaseline, finishedBaseline, actionInfo): ax1 = plt.subplot(311) #ax1.set_title('Scenario 4: average reward of last 100 children without quitting penalty') ax1.margins(0.05) #ax1.set_xlabel('Number of children') ax1.set_title('Average reward of last 100 children') ax1.plot(PPO, 'r', label='PPO') ax1.plot(Baseline, 'b', label='Baseline') ax1.legend() ax4 = plt.subplot(312) ax4.set_title('Actions taken') ax4.plot(actionInfo['question'], 'r', label='Question') ax4.plot(actionInfo['encourage'], 'g', label='Encourage') ax4.plot(actionInfo['hint'], 'b', label='Hint') ax4.plot(actionInfo['nothing'], 'y', label='Nothing') ax4.set_ylabel('% of last 500 actions') ax4.legend() ax2 = plt.subplot(325) ax2.set_title('PPO') ax2.plot(quit, 'r', label='Quit') ax2.plot(finished, 'g', label='Finished') ax2.set_ylabel('% of last 100 children') ax2.legend() ax3 = plt.subplot(326) ax3.set_title('Baseline') ax3.plot(quitBaseline, 'r', label='Quit') ax3.plot(finishedBaseline, 'g', label='Finished') ax3.legend() plt.show() ax1 = plt.subplot(141) ax1.set_title('Smart Child') ax1.plot(actionInfo['0'][0], 'y', label='Nothing') ax1.plot(actionInfo['0'][1], 'g', label='Encourage') ax1.plot(actionInfo['0'][2], 'r', label='Question') ax1.plot(actionInfo['0'][3], 'b', label='Hint') ax1.set_ylabel('% of last 500 actions') ax1.legend() ax2 = plt.subplot(142) ax2.set_title('Outgoing Child') ax2.plot(actionInfo['1'][0], 'y', label='Nothing') ax2.plot(actionInfo['1'][1], 'g', label='Encourage') ax2.plot(actionInfo['1'][2], 'r', label='Question') ax2.plot(actionInfo['1'][3], 'b', label='Hint') ax2.set_xlabel('Number of actions taken') ax2.legend() ax3 = plt.subplot(143) ax3.set_title('Quiet Child') ax3.plot(actionInfo['2'][0], 'y', label='Nothing') ax3.plot(actionInfo['2'][1], 'g', label='Encourage') ax3.plot(actionInfo['2'][2], 'r', label='Question') ax3.plot(actionInfo['2'][3], 'b', label='Hint') ax3.legend() ax4 = plt.subplot(144) ax4.set_title('Anxious Child') ax4.plot(actionInfo['3'][0], 'y', label='Nothing') ax4.plot(actionInfo['3'][1], 'g', label='Encourage') ax4.plot(actionInfo['3'][2], 'r', label='Question') ax4.plot(actionInfo['3'][3], 'b', label='Hint') ax4.legend() plt.show() return 0 plotting(Baseinfo['Performance'], env.gym_env.info['Performance'], env.gym_env.info['nQuit'], env.gym_env.info['nFinish'], Baseinfo['nQuit'], Baseinfo['nFinish'], env.gym_env.info['actionInfo'])
[]
2024-01-10
viniciusarruda/AskPDF
knowledge_base.py
import os from langchain.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from langchain.embeddings.base import Embeddings from langchain.schema import Document class KnowledgeBase: def __init__(self, embeddings: Embeddings, persistence_folder: os.PathLike | None = None, index_name: str = 'index') -> None: self.index_name = index_name self.persistence_folder = persistence_folder self.save = persistence_folder is not None self.embeddings = embeddings self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) if self.save: if os.path.exists(self.persistence_folder): if os.path.exists(os.path.join(self.persistence_folder, f'{self.index_name}.faiss')): self.db = FAISS.load_local(self.persistence_folder, self.embeddings, self.index_name) else: self.db = None else: os.makedirs(self.persistence_folder) # for future saving self.db = None else: self.db = None def remove_documents(self) -> None: self.db = None def add_document(self, file_path_or_url: os.PathLike | str) -> None: loader = PyPDFLoader(file_path_or_url) documents = loader.load() splitted_documents = self.text_splitter.split_documents(documents) if self.db is None: self.db = FAISS.from_documents(splitted_documents, self.embeddings) else: self.db.add_documents(splitted_documents) if self.save: self.db.save_local(self.persistence_folder, self.index_name) def query(self, query: str) -> list[Document]: return [] if self.db is None else self.db.similarity_search(query, k=4)
[]
2024-01-10
ShivangiReja/ChatWithAzureSDK
ChatWithAzureSDK~src~prepare_data.py
import tiktoken import os import json from bs4 import BeautifulSoup from typing import List from langchain.document_loaders import ReadTheDocsLoader from langchain.text_splitter import RecursiveCharacterTextSplitter import hashlib from tqdm.auto import tqdm def load_documents(html_files_path): docs = parse_html_content(html_files_path) token_counts = [tiktoken_len(doc.page_content) for doc in docs] doc1_chunks = text_splitter.split_text(docs[0].page_content) doc2_chunks = text_splitter.split_text(docs[1].page_content) formatted_docs = process_documents(docs, html_files_path) output_directory = r'C:\Users\shreja\Demo\ChatWithAzureSDK\ChatWithAzureSDK\src\JsonDocument' write_documents_to_jsonl(formatted_docs, output_directory) return f"\n \n Token count Doc0 - {token_counts[0]} \n Token count Doc1 - {token_counts[1]} \n Doc0 chunks length {len(doc1_chunks)} chunk1 token count {tiktoken_len(doc1_chunks[0])} \n Doc1 chunks length {len(doc2_chunks)} chunk1 token count {tiktoken_len(doc2_chunks[0])} \n\n Formatted docs length: {len(formatted_docs)} \n\n Formatted doc 1: {formatted_docs[0]}" # Custom class to hold document data class Document: def __init__(self, file_path, page_content): self.file_path = file_path self.page_content = page_content # Create the function to parse the html content def parse_html_content(html_files_path): documents: List[Document] = [] for root, _, files in os.walk(html_files_path): for file_name in files: if file_name.endswith(".html"): file_path = os.path.join(root, file_name) with open(file_path, "r", encoding="utf-8") as file: html_content = file.read() soup = BeautifulSoup(html_content, "html.parser") # Get the whole text content without modifications extracted_text = soup.get_text() # Remove 3 or more empty lines extracted_text = "\n".join([t for t in extracted_text.split("\n\n\n") if t]) document = Document(file_path, extracted_text) documents.append(document) return documents # Create the length function to calculate token length for the given text tokenizer = tiktoken.get_encoding('cl100k_base') def tiktoken_len(text): tokens = tokenizer.encode( text, disallowed_special=() ) return len(tokens) # To create these chunks we use the RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter( chunk_size=800, chunk_overlap=50, # number of tokens overlap between chunks length_function=tiktoken_len, separators=['\n\n', '\n', ' ', ''] ) # Process a list of documents, split them into chunks, and format them as follows: """ Each processed document will be restructured into a format that looks like: [ { "id": "abc-0", "content": "some important document text", "source": "https://langchain.readthedocs.io/en/latest/glossary.html" }, { "id": "abc-1", "content": "the next chunk of important document text", "source": "https://langchain.readthedocs.io/en/latest/glossary.html" } ... ] """ def process_documents(docs, html_files_path): documents = [] m = hashlib.sha256() for doc in tqdm(docs): url = doc.file_path.replace(html_files_path, 'https:') m.update(url.encode('utf-8')) uid = m.hexdigest()[:12] chunks = text_splitter.split_text(doc.page_content) for i, chunk in enumerate(chunks): documents.append({ 'id': f'{uid}-{i}', 'content': chunk, 'source': url }) return documents # Save them to a JSON lines (.json) file like so: def write_documents_to_jsonl(documents, output_directory): output_file = os.path.join(output_directory, 'documents.jsonl') with open(output_file, 'w') as f: for doc in documents: f.write(json.dumps(doc) + '\n')
[]
2024-01-10
bleschunov/msu-test
src~datastep~components~datastep_sql_database.py
from langchain.utilities import SQLDatabase from util.logger import log databases_connection_pool = dict() class DatastepSqlDatabase: @log("ะŸะพะดะบะปัŽั‡ะตะฝะธะต ะบ ะฑะฐะทะต ะดะฐะฝะฝั‹ั…") def __init__( self, database_connection_string: str, include_tables: list[str], tenant_id: int ): self.database = SQLDatabase.from_uri( database_connection_string, include_tables=include_tables, view_support=True ) # TODO: ะ˜ัะฟะพะปัŒะทะพะฒะฐั‚ัŒ ะฟัƒะปะธะฝะณ ะธะท ะะปั…ะธะผะธะธ # if not databases_connection_pool.get(tenant_id, None): # databases_connection_pool[tenant_id] = SQLDatabase.from_uri( # database_connection_string, # include_tables=include_tables, # view_support=True # ) # # self.database = databases_connection_pool[tenant_id] @log("ะ˜ัะฟะพะปะฝะตะฝะธะต SQL ะฒ ะฑะฐะทะต ะดะฐะฝะฝั‹ั…") def run(self, sql_query): response = self.database._execute(sql_query) return response
[]
2024-01-10
bleschunov/msu-test
src~datastep~components~sql_database_chain_executor.py
import dataclasses import langchain import pandas as pd from dotenv import load_dotenv from langchain.callbacks import StdOutCallbackHandler from langchain.chat_models import ChatOpenAI from openai import OpenAIError from sqlalchemy.exc import SQLAlchemyError from config.config import config from datastep.components.chain import get_sql_database_chain_patched from datastep.components.datastep_prediction import DatastepPrediction from datastep.components.datastep_prompt import DatastepPrompt from datastep.components.patched_database_class import SQLDatabasePatched from datastep.components.patched_sql_chain import SQLDatabaseChainPatched from datastep.models.intermediate_steps import IntermediateSteps from repository.prompt_repository import prompt_repository load_dotenv() TABLE_PROMPT_ID_MAPPING = { "ะฟะปะฐั‚ะตะถะธ": 1, "ัะพั‚ั€ัƒะดะฝะธะบะธ": 2 } @dataclasses.dataclass class SQLDatabaseChainExecutor: db_chain: SQLDatabaseChainPatched debug: bool = False verbose_answer: bool = False langchain_debug: bool = False def __post_init__(self): langchain.debug = self.langchain_debug def run(self, query: str, tables: list[str]) -> DatastepPrediction: callbacks = self.get_callbacks() if tables: self.db_chain.database._include_tables = set(tables) prompt_dto = prompt_repository.fetch_by_id(TABLE_PROMPT_ID_MAPPING[tables[0]]) else: prompt_dto = prompt_repository.fetch_by_id(config["prompt_id"]) self.db_chain.llm_chain.prompt = DatastepPrompt.get_prompt(table_description=prompt_dto.prompt) try: db_chain_response = self.db_chain(query, callbacks=callbacks) except OpenAIError as e: return DatastepPrediction( answer=str(e), sql=None, table=None, table_source=None, is_exception=True ) except SQLAlchemyError as e: intermediate_steps = e.intermediate_steps intermediate_steps = IntermediateSteps.from_chain_steps(intermediate_steps) return DatastepPrediction( answer=str(e), sql=self.get_sql_markdown(intermediate_steps.sql_query), table=None, table_source=None, is_exception=True ) if self.verbose_answer: chain_answer = db_chain_response["result"] else: chain_answer = "" intermediate_steps = db_chain_response["intermediate_steps"] intermediate_steps = IntermediateSteps.from_chain_steps(intermediate_steps) sql_query = intermediate_steps.sql_query sql_result = intermediate_steps.sql_result return DatastepPrediction( answer=chain_answer, sql=self.get_sql_markdown(sql_query), table=self.get_table_markdown(sql_result), table_source=self.get_table_source(sql_result), is_exception=False ) @classmethod def get_sql_markdown(cls, sql_result) -> str: if sql_result: return f"~~~sql\n{sql_result}\n~~~" return "" @classmethod def get_table_markdown(cls, sql_result) -> str: data_frame = pd.DataFrame(sql_result) if data_frame is not None and any(data_frame): return data_frame.to_markdown(index=False, floatfmt=".3f") return "" @classmethod def get_table_source(cls, sql_result) -> str: return pd.DataFrame(sql_result).to_json(orient="table", force_ascii=False, index=False) def get_callbacks(self): callbacks = [] if self.debug: callbacks.append(StdOutCallbackHandler()) return callbacks def get_sql_database_chain_executor( db: SQLDatabasePatched, llm: ChatOpenAI, debug: bool = False, verbose_answer: bool = False ) -> SQLDatabaseChainExecutor: return SQLDatabaseChainExecutor( db_chain=get_sql_database_chain_patched(db, llm, DatastepPrompt.get_prompt(), verbose_answer), debug=debug, verbose_answer=verbose_answer )
[ "{'ะฟะปะฐั‚ะตะถะธ': 1, 'ัะพั‚ั€ัƒะดะฝะธะบะธ': 2}" ]
2024-01-10
bleschunov/msu-test
src~datastep~components~patched_sql_chain.py
from typing import Any, Dict, List from langchain import PromptTemplate, LLMChain from langchain_experimental.sql import SQLDatabaseChain from langchain.callbacks.manager import CallbackManagerForChainRun from langchain.tools.sql_database.prompt import QUERY_CHECKER INTERMEDIATE_STEPS_KEY = "intermediate_steps" class SQLDatabaseChainPatched(SQLDatabaseChain): def _call( self, inputs: Dict[str, Any], run_manager: CallbackManagerForChainRun | None = None, ) -> Dict[str, Any]: _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() input_text = f"{inputs[self.input_key]}\nSQLQuery:" _run_manager.on_text(input_text, verbose=self.verbose) # If not present, then defaults to None which is all tables. table_names_to_use = inputs.get("table_names_to_use") table_info = self.database.get_table_info(table_names=table_names_to_use) llm_inputs = { "input": input_text, "top_k": str(self.top_k), "dialect": self.database.dialect, "table_info": table_info, "stop": ["\nSQLResult:"], } intermediate_steps: List = [] try: intermediate_steps.append(llm_inputs) # input: sql generation sql_cmd = self.llm_chain.predict( callbacks=_run_manager.get_child(), **llm_inputs, ).strip() if not self.use_query_checker: _run_manager.on_text(sql_cmd, color="green", verbose=self.verbose) intermediate_steps.append( sql_cmd ) # output: sql generation (no checker) intermediate_steps.append({"sql_cmd": sql_cmd}) # input: sql exec result = self.database.run(sql_cmd) intermediate_steps.append(result) # output: sql exec PATCHED else: query_checker_prompt = self.query_checker_prompt or PromptTemplate( template=QUERY_CHECKER, input_variables=["query", "dialect"] ) query_checker_chain = LLMChain( llm=self.llm_chain.llm, prompt=query_checker_prompt ) query_checker_inputs = { "query": sql_cmd, "dialect": self.database.dialect, } checked_sql_command: str = query_checker_chain.predict( callbacks=_run_manager.get_child(), **query_checker_inputs ).strip() intermediate_steps.append( checked_sql_command ) # output: sql generation (checker) _run_manager.on_text( checked_sql_command, color="green", verbose=self.verbose ) intermediate_steps.append( {"sql_cmd": checked_sql_command} ) # input: sql exec result = self.database.run(checked_sql_command) intermediate_steps.append(result) # output: sql exec PATCHED sql_cmd = checked_sql_command _run_manager.on_text("\nSQLResult: ", verbose=self.verbose) _run_manager.on_text(result, color="yellow", verbose=self.verbose) # If return direct, we just set the final result equal to # the result of the sql query result, otherwise try to get a human readable # final answer if self.return_direct: final_result = result else: _run_manager.on_text("\nAnswer:", verbose=self.verbose) input_text += f"{sql_cmd}\nSQLResult: {result}\nAnswer:" llm_inputs["input"] = input_text intermediate_steps.append(llm_inputs) # input: final answer final_result = self.llm_chain.predict( callbacks=_run_manager.get_child(), **llm_inputs, ).strip() intermediate_steps.append(final_result) # output: final answer _run_manager.on_text(final_result, color="green", verbose=self.verbose) chain_result: Dict[str, Any] = {self.output_key: final_result} if self.return_intermediate_steps: chain_result[INTERMEDIATE_STEPS_KEY] = intermediate_steps return chain_result except Exception as exc: # Append intermediate steps to exception, to aid in logging and later # improvement of few shot prompt seeds exc.intermediate_steps = intermediate_steps # type: ignore raise exc
[]
2024-01-10
bleschunov/msu-test
src~datastep~components~patched_database_class.py
from langchain import SQLDatabase from langchain.utilities.sql_database import text class SQLDatabasePatched(SQLDatabase): def run(self, command: str, fetch: str = "all") -> list: """Execute a SQL command and return a string representing the results. If the statement returns rows, a string of the results is returned. If the statement returns no rows, an empty string is returned. """ with self._engine.begin() as connection: if self._schema is not None: if self.dialect == "snowflake": connection.exec_driver_sql( f"ALTER SESSION SET search_path='{self._schema}'" ) elif self.dialect == "bigquery": connection.exec_driver_sql(f"SET @@dataset_id='{self._schema}'") else: connection.exec_driver_sql(f"SET search_path TO {self._schema}") cursor = connection.execute(text(command)) if cursor.returns_rows: # if fetch == "all": # result = cursor.fetchall() # elif fetch == "one": # result = cursor.fetchone() # type: ignore # else: # raise ValueError("Fetch parameter must be either 'one' or 'all'") result = cursor.mappings().all() # PATCHED # Convert columns values to string to avoid issues with sqlalchmey # trunacating text # if isinstance(result, list): # return [ # tuple( # truncate_word(c, length=self._max_string_length) for c in r # ) # for r in result # ] # # return tuple( # truncate_word(c, length=self._max_string_length) for c in result # ) return result return []
[]
2024-01-10
bleschunov/msu-test
src~datastep~datastep_chains~datastep_rephrase.py
from langchain.chains import LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts.prompt import PromptTemplate from dotenv import load_dotenv load_dotenv() rephrase_template = """ You must follow the steps described below: Step 1: ะะฐะฟะธัˆะธ, ะบะฐะบ ะผะพะถะฝะพ ะธะทะผะตะฝะธั‚ัŒ ะฝะตะบะพั‚ะพั€ั‹ะต ัะปะพะฒะฐ ะฒ ะฒะพะฟั€ะพัะต, ั‡ั‚ะพะฑั‹ ะพะฝ ั‚ั€ะฐะบั‚ะพะฒะฐะปัั ะพะดะฝะพะทะฝะฐั‡ะฝะพ. ะžะฑั€ะฐั‚ะธ ะฒะฝะธะผะฐะฝะธะต, ั‡ั‚ะพ ะฝะตะบะพั‚ะพั€ั‹ะต ัะปะพะฒะฐ ะผะพะถะฝะพ ะฒะพัะฟั€ะธะฝะธะผะฐั‚ัŒ ะฟะพโ€“ั€ะฐะทะฝะพะผัƒ. ะ˜ะทะฑะฐะฒัŒัั ะพั‚ ั‚ะฐะบะพะน ะฝะตะพะดะฝะพะทะฝะฐั‡ะฝะพัั‚ะธ. ะŸะพะบะฐะถะธ ั‚ะพะปัŒะบะพ ะธัะฟั€ะฐะฒะปะตะฝะฝั‹ะน ะฒะพะฟั€ะพั. ะ’ะพะฟั€ะพั: {input} ะŸะพะบะฐะถะธ ั‚ะพะปัŒะบะพ ะฝะพะฒั‹ะน ะฒะพะฟั€ะพั """ rephrase_prompt = PromptTemplate( template=rephrase_template, input_variables=["input"] ) llm = ChatOpenAI(temperature=0.5, verbose=True, model_name="gpt-3.5-turbo") rephrase_chain = LLMChain(llm=llm, prompt=rephrase_prompt, verbose=True) def rephrase(input: str) -> str: return rephrase_chain.run( input=input, )
[ "input", "\nYou must follow the steps described below:\n\nStep 1:\nะะฐะฟะธัˆะธ, ะบะฐะบ ะผะพะถะฝะพ ะธะทะผะตะฝะธั‚ัŒ ะฝะตะบะพั‚ะพั€ั‹ะต ัะปะพะฒะฐ ะฒ ะฒะพะฟั€ะพัะต, ั‡ั‚ะพะฑั‹ ะพะฝ ั‚ั€ะฐะบั‚ะพะฒะฐะปัั ะพะดะฝะพะทะฝะฐั‡ะฝะพ. ะžะฑั€ะฐั‚ะธ ะฒะฝะธะผะฐะฝะธะต, ั‡ั‚ะพ ะฝะตะบะพั‚ะพั€ั‹ะต ัะปะพะฒะฐ ะผะพะถะฝะพ ะฒะพัะฟั€ะธะฝะธะผะฐั‚ัŒ ะฟะพโ€“ั€ะฐะทะฝะพะผัƒ. ะ˜ะทะฑะฐะฒัŒัั ะพั‚ ั‚ะฐะบะพะน ะฝะตะพะดะฝะพะทะฝะฐั‡ะฝะพัั‚ะธ.\nะŸะพะบะฐะถะธ ั‚ะพะปัŒะบะพ ะธัะฟั€ะฐะฒะปะตะฝะฝั‹ะน ะฒะพะฟั€ะพั.\n\nะ’ะพะฟั€ะพั: {input}\n\nะŸะพะบะฐะถะธ ั‚ะพะปัŒะบะพ ะฝะพะฒั‹ะน ะฒะพะฟั€ะพั\n" ]
2024-01-10
bleschunov/msu-test
src~datastep~components~datastep_nomenclature.py
import pathlib from infra.supabase import supabase from langchain.prompts import PromptTemplate from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain from langchain.text_splitter import RecursiveCharacterTextSplitter from rq import get_current_job from langchain.callbacks import get_openai_callback from langchain.chains.openai_functions import create_structured_output_runnable def get_nomenclatures(group: str) -> str: response = supabase \ .table("nomenclature") \ .select("ะะพะผะตะฝะบะปะฐั‚ัƒั€ะฐ") \ .eq("ะ“ั€ัƒะฟะฟะฐ", group) \ .execute() return "\n".join([d["ะะพะผะตะฝะบะปะฐั‚ัƒั€ะฐ"] for d in response.data]) def get_groups(filepath: str, group_number: str = None) -> str: with open(filepath, "r") as f: lines = f.readlines() if group_number: lines = [line for line in lines if line.startswith(group_number)] return "".join(lines) description_template = "ะงั‚ะพ ั‚ะฐะบะพะต {input}" group_template = """"ะ”ะปั ะพะฑัŠะตะบั‚ะฐ ะฒั‹ะฑะตั€ะธ ัั‚ั€ะพะณะพ ะพะดะฝัƒ ะฟะพะดั…ะพะดัั‰ัƒัŽ ะบะฐั‚ะตะณะพั€ะธัŽ ะธะท ัะฟะธัะบะฐ. ะฃั‡ั‚ะธ ะพะฟะธัะฐะฝะธะต ะพะฑัŠะตะบั‚ะฐ. ะžะฑัŠะตะบั‚: {input} ะžะฟะธัะฐะฝะธะต ะพะฑัŠะตะบั‚ะฐ: {description} ะšะฐั‚ะตะณะพั€ะธะธ: {groups} """ nomenclature_template = """ะะฐะนะดะธ ะฒ ัะฟะธัะบะต ั‚ะฐะบะพะน ะถะต ะพะฑัŠะตะบั‚. ะ•ัะปะธ ะฒ ัะฟะธัะบะต ะฝะตั‚ ั‚ะฐะบะพะณะพ ะถะต ะพะฑัŠะตะบั‚ะฐ, ะฝะฐะฟะธัˆะธ 0 ะžะฑัŠะตะบั‚: {input} ะกะฟะธัะพะบ: {groups} """ description_prompt = PromptTemplate( template=description_template, input_variables=["input"] ) group_prompt = PromptTemplate( template=group_template, input_variables=["input", "groups", "description"] ) nomenclature_prompt = PromptTemplate( template=nomenclature_template, input_variables=["input", "groups"] ) gpt_3 = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-1106", max_retries=3, request_timeout=10) gpt_4 = ChatOpenAI(temperature=0, model_name="gpt-4-1106-preview", max_retries=3, request_timeout=10) group_json_schema = { "title": "category", "description": "category from list", "type": "object", "properties": { "category": {"title": "category", "description": "category number and name", "type": "string"}, }, "required": ["category"], } nomenclature_json_schema = { "title": "object", "description": "object from list", "type": "object", "properties": { "nomenclature": {"title": "object_name", "description": "object name or 0", "type": "string"}, }, "required": ["nomenclature"], } group_runnable_gpt_3 = create_structured_output_runnable(group_json_schema, gpt_3, group_prompt) group_runnable_gpt_4 = create_structured_output_runnable(group_json_schema, gpt_4, group_prompt) nomenclature_runnable = create_structured_output_runnable(nomenclature_json_schema, gpt_4, nomenclature_prompt) description_chain = LLMChain(llm=gpt_3, prompt=description_prompt) text_splitter = RecursiveCharacterTextSplitter( chunk_size=4000, chunk_overlap=0, separators=["\n"] ) def map_with_groups(query: str, description: str, filepath: str, group_runnable, prev_response: str = None, index: int = None) -> str: groups = get_groups(filepath, prev_response[:index] if prev_response else None) if len(groups) == 0: return prev_response response = group_runnable.invoke({"input": query, "groups": groups, "description": description}) return response["category"] def map_with_nomenclature(query: str, final_group: str): nomenclatures = get_nomenclatures(final_group) nomenclatures_chunks = text_splitter.split_text(nomenclatures) short_list = [] for chunk in nomenclatures_chunks: response = nomenclature_runnable.invoke({"input": query, "groups": chunk}) nomenclature_position = response["nomenclature"] short_list.append(nomenclature_position) groups = "\n".join(str(short_list)) response = nomenclature_runnable.invoke({"input": query, "groups": groups}) return response["nomenclature"] def get_data_folder_path(): return f"{pathlib.Path(__file__).parent.resolve()}" def save_to_database(values: dict): return supabase.table("nomenclature_mapping").insert(values).execute() def do_mapping(query: str) -> str: with get_openai_callback() as cb: description = description_chain.run(query) job = get_current_job() middle_group = map_with_groups(query, description, f"{get_data_folder_path()}/../data/parent-parent.txt", group_runnable_gpt_3) job.meta["middle_group"] = middle_group job.save_meta() narrow_group = map_with_groups(query, description, f"{get_data_folder_path()}/../data/parent.txt", group_runnable_gpt_3, middle_group, 6) job.meta["narrow_group"] = narrow_group job.save_meta() response = map_with_nomenclature(query, narrow_group) database_response = save_to_database({ "input": query, "output": response, "wide_group": "", "middle_group": middle_group, "narrow_group": narrow_group, "source": job.get_meta().get("source", None), "status": job.get_status() }) job.meta["mapping_id"] = database_response.data[0]["id"] job.save() return response if __name__ == "__main__": print("FINAL_RESPONSE", do_mapping("ะŸะพะปะบะฐ ะฟะพะด ะฟั€ะธะฝั‚ะตั€ ะบ ั‚ะตะฟะปะพัั‡ั‘ั‚ั‡ะธะบัƒ ะ’ะ˜ะก.ะข"))
[ "ะงั‚ะพ ั‚ะฐะบะพะต {input}", "ะะฐะนะดะธ ะฒ ัะฟะธัะบะต ั‚ะฐะบะพะน ะถะต ะพะฑัŠะตะบั‚.\nะ•ัะปะธ ะฒ ัะฟะธัะบะต ะฝะตั‚ ั‚ะฐะบะพะณะพ ะถะต ะพะฑัŠะตะบั‚ะฐ, ะฝะฐะฟะธัˆะธ 0\n\nะžะฑัŠะตะบั‚: \n{input}\n\nะกะฟะธัะพะบ:\n{groups}\n", "input", "description", "\"ะ”ะปั ะพะฑัŠะตะบั‚ะฐ ะฒั‹ะฑะตั€ะธ ัั‚ั€ะพะณะพ ะพะดะฝัƒ ะฟะพะดั…ะพะดัั‰ัƒัŽ ะบะฐั‚ะตะณะพั€ะธัŽ ะธะท ัะฟะธัะบะฐ.\nะฃั‡ั‚ะธ ะพะฟะธัะฐะฝะธะต ะพะฑัŠะตะบั‚ะฐ.\n\nะžะฑัŠะตะบั‚: {input}\nะžะฟะธัะฐะฝะธะต ะพะฑัŠะตะบั‚ะฐ: {description}\n\nะšะฐั‚ะตะณะพั€ะธะธ:\n{groups}\n" ]
2024-01-10
bleschunov/msu-test
src~datastep~components~datastep_prompt.py
import datetime from langchain import PromptTemplate from config.config import config basic_prompt = """You are an %s expert. Given an input question, first create a syntactically correct %s query to run, then look at the results of the query and return the answer to the input question. Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the TOP clause as per %s. You can order the results to return the most informative data in the database. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Use Russian language. Use the following format: Question: Question here SQLQuery: SQL Query to run SQLResult: Result of the SQLQuery Answer: Final answer here """ prompt_suffix = """Only use the following tables: {table_info} Question: {input}""" class DatastepPrompt: @classmethod def get_prompt(cls, table_description: str = None) -> PromptTemplate: return PromptTemplate( input_variables=["input", "table_info", "top_k"], template=cls.build_prompt(table_description) ) @classmethod def build_prompt(cls, table_description) -> str: table_description = table_description or config["prompt"]["table_description"] table_description = table_description.replace("[[currentDate]]", str(datetime.date.today())) db_driver = config["db_driver"] return basic_prompt % (db_driver, db_driver, db_driver) + table_description + prompt_suffix
[ "Only use the following tables:\n{table_info}\n\nQuestion: {input}", "You are an %s expert. Given an input question, first create a syntactically correct %s query to run, then look at the results of the query and return the answer to the input question.\nUnless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the TOP clause as per %s. You can order the results to return the most informative data in the database.\nNever query for all columns from a table. You must query only the columns that are needed to answer the question.\nPay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Use Russian language.\n\nUse the following format:\n\nQuestion: Question here\nSQLQuery: SQL Query to run\nSQLResult: Result of the SQLQuery\nAnswer: Final answer here\n" ]
2024-01-10
wzaielamri/unmix
setup.py
import os import pkg_resources from setuptools import setup, find_packages setup( name="unmix", py_modules=["unmix"], version="1.0", description="original code from OpenAI Jukebox", author="", packages=find_packages(), install_requires=[ str(r) for r in pkg_resources.parse_requirements( open(os.path.join(os.path.dirname(__file__), "requirements.txt")) ) ], include_package_data=True )
[]
2024-01-10
chubbyyb/huawei_hackathon
validate.py
import sqlvalidator import os import openai import sqlite3 SQL_path = '''sql_DB/example-covid-vaccinations.sqlite3''' openai.api_key = "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" invalidSQL = '''DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';''' validSQL = '''SELECT id, name, surname, standard, drop, city, FROM studentdetails WHERE name = "John" ORDER BY standard ASC''' def validateSQL(response): if not sqlvalidator.parse(response).is_valid(): print("SQL Invalid") return badWords = ['INSERT', 'UPDATE', 'DELETE', 'ALTER', 'DROP', 'TRUNCATE', 'MERGE', 'REPLACE', 'SET', 'COMMIT', 'ROLLBACK', 'SAVEPOINT', 'GRANT', 'REVOKE', 'DENY', 'EXEC', 'EXECUTE', 'CALL', 'USE'] return 'Valid' def getResponse(message,SQLschema): completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": f"You assess the users message and respond with only valid SQL according to this schema: {SQLschema}."}, {"role": "user", "content": f"{message}"} ] ) print(completion.choices[0].message['content']) print(validateSQL(completion.choices[0].message['content'])) def connect_db(path): conn = sqlite3.connect(path) # Create a cursor object cursor = conn.cursor() # Query to retrieve the schema information for all tables query = "SELECT sql FROM sqlite_master WHERE type='table';" # Execute the query cursor.execute(query) # Fetch all the results and concatenate them into a single string schema_string = "" for row in cursor.fetchall(): schema_string += row[0] + '\n' # Close the cursor and the database connection cursor.close() conn.close() print(schema_string) getResponse(message='''What was the biggest vaccination rate achieved?''', SQLschema=str(schema_string)) return conn connect_db(SQL_path)
[ "PLACEHOLDER", "You assess the users message and respond with only valid SQL according to this schema: PLACEHOLDER." ]
2024-01-10
ZohaibRamzan/Youtube_Content_Creation
llam-7B-GGML-PDF-Chat-langchain~Chat_PDF_llama2-7B-ggml-langchain.py
import os from langchain.vectorstores import FAISS from langchain.embeddings import HuggingFaceEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.chains import ConversationalRetrievalChain from langchain.document_loaders import PyPDFLoader, DirectoryLoader from langchain.llms import CTransformers import chainlit as cl from langchain.memory import ConversationBufferMemory from prompts_chat_pdf import chat_prompt, CONDENSE_QUESTION_PROMPT class PDFChatBot: def __init__(self): self.data_path = os.path.join('data') self.db_faiss_path = os.path.join('vectordb', 'db_faiss') #self.chat_prompt = PromptTemplate(template=chat_prompt, input_variables=['context', 'question']) #self.CONDENSE_QUESTION_PROMPT=CONDENSE_QUESTION_PROMPT def create_vector_db(self): '''function to create vector db provided the pdf files''' loader = DirectoryLoader(self.data_path, glob='*.pdf', loader_cls=PyPDFLoader) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=50) texts = text_splitter.split_documents(documents) embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'}) db = FAISS.from_documents(texts, embeddings) db.save_local(self.db_faiss_path) def load_llm(self): # Load the locally downloaded model here llm = CTransformers( model="llama-2-7b-chat.ggmlv3.q8_0.bin", model_type="llama", max_new_tokens=2000, temperature=0.5 ) return llm def conversational_chain(self): embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'}) db = FAISS.load_local(self.db_faiss_path, embeddings) # initializing the conversational chain memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversational_chain = ConversationalRetrievalChain.from_llm( llm=self.load_llm(), retriever=db.as_retriever(search_kwargs={"k": 3}), verbose=True, memory=memory ) return conversational_chain def intialize_chain(): bot = PDFChatBot() bot.create_vector_db() conversational_chain = bot.conversational_chain() return conversational_chain chat_history = [] chain = intialize_chain() @cl.on_chat_start async def start(): msg = cl.Message(content="Starting the bot...") await msg.send() msg.content = "Welcome to Data Science Interview Prep Bot. Ask me your question?" await msg.update() cl.user_session.set("chain", chain) @cl.on_message async def main(message): chain = cl.user_session.get("chain") cb = cl.AsyncLangchainCallbackHandler( stream_final_answer=False, answer_prefix_tokens=["FINAL", "ANSWER"] ) cb.answer_reached = True res = await chain.acall({"question": message, "chat_history": chat_history}, callbacks=[cb]) answer = res["answer"] chat_history.append(answer) await cl.Message(content=answer).send() # while(True): # query = input('User: ') # response = conversational_chain({"question": query, "chat_history": chat_history}) # chat_history.append(response["answer"]) # Append the answer to chat history # print(response["answer"])
[]
2024-01-10
ZohaibRamzan/Youtube_Content_Creation
SQL_Multi_agent_example_using_Autogen~SQL_multi_agent_autogen.py
import os from dotenv import load_dotenv import openai import autogen from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain from prompts import query_maker_gpt_system_prompt, admin_prompt, data_engineer_prompt from langchain.prompts import PromptTemplate import mysql.connector #load env variables load_dotenv() # openai api key api_key=os.getenv('OPENAI_API_KEY') # Set your LLms Endpoint config_list_gpt_turbo = autogen.config_list_from_models(model_list=[ "gpt-4"]) def query_maker(user_input): # make sql queries using LLM chain openaiLLM = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7, openai_api_key=api_key, cache=False) prompt_template = PromptTemplate.from_template( "{system_prompt} + '\n' + {user_input}.") chain = LLMChain(llm=openaiLLM, prompt=prompt_template) query=chain.run({"system_prompt": query_maker_gpt_system_prompt, "user_input": user_input}) return query def run_sql_query(sql_query): config = { 'host': os.getenv('azure_sql_host'), 'user': os.getenv('user'), 'password': os.getenv('password'), 'database': os.getenv('database'), 'ssl_ca': os.getenv('ssl_ca') # Path to the SSL CA certificate (optional) } conn = mysql.connector.connect(**config) cursor = conn.cursor() try: # Execute SQL queries cursor.execute(sql_query) result = cursor.fetchall() except Exception as e: return e return result gpt_turbo_simple_config = { "use_cache": False, "temperature": 0.5, "config_list": config_list_gpt_turbo, "request_timeout": 90} gpt_turbo_config = { "use_cache": False, "temperature": 0.7, "config_list": config_list_gpt_turbo, "request_timeout": 90, "functions" : [ { "name": "query_maker", "description": "generates sql query as per user input", "parameters": { "type": "object", "properties": { "user_input": { "type": "string", "description": "This is the input from the user side.", } , }, "required": ["user_input"], }, }, { "name": "run_sql_query", "description": "This function is used to run sql query against user input to get the results.", "parameters": { "type": "object", "properties": { "sql_query": { "type": "string", "description": "This is the mysql query.", } , }, "required": ["sql_query"], }, } ] } function_map={"query_maker": query_maker ,"run_sql_query": run_sql_query} termination_msg="If everything looks good, respond with Approved." def is_termination_msg(content): have_content=content.get("content", None) is not None if have_content and "Approved" in content["content"]: return True else: return False user_proxy = autogen.UserProxyAgent( name="Admin", system_message= admin_prompt + termination_msg, human_input_mode="Never", is_termination_msg=is_termination_msg ) engineer = autogen.AssistantAgent( name="Data_Engineer", llm_config=gpt_turbo_config, system_message=data_engineer_prompt + termination_msg, function_map=function_map ) # register the functions user_proxy.register_function(function_map={"query_maker": query_maker ,"run_sql_query": run_sql_query},) user_proxy.initiate_chat( engineer, message="""How many Apple pro vision we have in our stock?""", clear_history=True ) user_proxy_chat=user_proxy.chat_messages engineer_chat=engineer.chat_messages
[ "{system_prompt} + '\n' + {user_input}." ]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~vector_search_chain.py
import json import os from typing import Dict, List, Any from langchain.chains.base import Chain from langchain.embeddings.openai import OpenAIEmbeddings from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.vectorstores.base import VectorStore class VectorSearchChain(Chain): query: str embedding_engine: str embedding_params: Dict[str, Any] database: str database_params: Dict[str, Any] num_results: int min_score: float = 0.0 input_variables: List[str] = [] output_key: str = 'results' @property def input_keys(self) -> List[str]: return self.input_variables @property def output_keys(self) -> List[str]: return [self.output_key] def vector_store(self, query) -> VectorStore: if self.database == 'pinecone': if 'index' not in self.database_params: raise ValueError('Missing index parameter for Pinecone database') # import just-in-time so auth will happen after env vars are loaded import pinecone from langchain.vectorstores.pinecone import Pinecone index = pinecone.Index(self.database_params['index']) return Pinecone( index, query, self.database_params.get('text_key') or 'text', namespace=self.database_params.get('namespace') or '' ) else: raise ValueError(f'Unknown database: {self.database}') def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: if self.embedding_engine == 'openai': self.embedding_params['openai_api_key'] = os.environ.get("OPENAI_API_KEY") embeddings = OpenAIEmbeddings(**self.embedding_params) elif self.embedding_engine == 'huggingface': model = self.embedding_params.get('model_name') or 'all-mpnet-base-v2' embeddings = HuggingFaceEmbeddings(**self.embedding_params, model_name=model) else: raise ValueError(f'Unknown embedding engine: {self.embedding_engine}') vector_store = self.vector_store(embeddings.embed_query) formatted_query = self.query.format(**inputs) items = vector_store.similarity_search_with_score(formatted_query, self.num_results, self.database_params.get('filter'), self.database_params.get('namespace')) return {self.output_key: json.dumps([{'text': item[0].page_content, 'meta': item[0].metadata, 'score': item[1]} for item in items if item[1] >= self.min_score])}
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~recording_chain.py
from typing import Dict, List from langchain.chains.base import Chain class RecordingChain(Chain): recorded_calls: List[tuple[Dict[str, str], Dict[str, str]]] = [] chain_spec_id: int chain: Chain def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: output = self.chain._call(inputs) self.recorded_calls.append((inputs, output)) return output @property def input_keys(self) -> List[str]: return self.chain.input_keys @property def output_keys(self) -> List[str]: return self.chain.output_keys @property def calls(self) -> List[tuple[Dict[str, str], Dict[str, str]]]: return self.recorded_calls def reset(self): self.recorded_calls.clear()
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~tests~test_vector_search_chain.py
import json import os from typing import Any, Iterable, Optional, List, Tuple from unittest.mock import patch from langchain.vectorstores.base import VectorStore from langchain.docstore.document import Document from chains.vector_search_chain import VectorSearchChain class MockVectorStore(VectorStore): def similarity_search_with_score(self, query: str, k: int = 5, filter: Optional[dict] = None, namespace: Optional[str] = None) -> List[Tuple[Document, float]]: assert query == "How do I open a can of soup?" return [ (Document(page_content="Opening cans of soup.", metadata={}), 0.5), (Document(page_content="Opening cans of paint.", metadata={}), 0.4), ] def add_texts(self, texts: Iterable[str], metadatas: List[dict] | None = None, **kwargs: Any) -> List[str]: return super().add_texts(texts, metadatas, **kwargs) def similarity_search(self, query: str, k: int = 4, **kwargs: Any) -> List[Document]: return super().similarity_search(query, k, **kwargs) def from_texts(self, texts: Iterable[str], metadatas: List[dict] | None = None, **kwargs: Any) -> List[str]: return super().from_texts(texts, metadatas, **kwargs) def test_openai_pinecone_search(): os.environ.setdefault("OPENAI_API_KEY", "test") chain = VectorSearchChain( query="How do I open a can of {can_type}?", embedding_engine="openai", embedding_params={"openai_api_key": "test"}, database="pinecone", database_params={"index": "test", "text_key": "text"}, input_variables=[], num_results=10, ) with patch.object(VectorSearchChain, 'vector_store', return_value=MockVectorStore()): response = chain._call({"can_type": "soup"}) results = json.loads(response['results']) assert len(results) == 2 assert results[0]['text'] == "Opening cans of soup."
[]
2024-01-10
wm-pxel/langchain-testbench
lib~model~llm_spec.py
from typing import Annotated, Callable, Optional, Dict, Literal, Union, TypedDict from pydantic import BaseModel, Field from langchain.llms.base import LLM from langchain.llms.openai import OpenAI from langchain.llms.huggingface_hub import HuggingFaceHub from langchain.chat_models.openai import ChatOpenAI LLMSpec = Annotated[Union[ "OpenAILLMSpec", "HuggingFaceHubLLMSpec", "ChatOpenAILLMSpec", ], Field(discriminator='llm_type')] class BaseLLMSpec(BaseModel): def to_llm(self) -> LLM: raise NotImplementedError def copy_replace(self, replace: Callable[[LLMSpec], LLMSpec]): return replace(self).copy(deep=True) class OpenAILLMSpec(BaseLLMSpec): llm_type: Literal["openai"] = "openai" model_name: str temperature: float max_tokens: int top_p: float frequency_penalty: float presence_penalty: float n: int request_timeout: Optional[int] logit_bias: Optional[Dict[int, int]] def to_llm(self) -> LLM: return OpenAI(model_name=self.model_name, temperature=self.temperature, max_tokens=self.max_tokens, top_p=self.top_p, frequency_penalty=self.frequency_penalty, presence_penalty=self.presence_penalty, n=self.n, request_timeout=self.request_timeout, logit_bias=self.logit_bias) class HuggingFaceHubLLMSpec(BaseLLMSpec): class ModelKwargs(TypedDict): temperature: float max_length: int llm_type: Literal["huggingface_hub"] = "huggingface_hub" repo_id: str task: Optional[str] model_kwargs: Optional[ModelKwargs] def to_llm(self) -> LLM: return HuggingFaceHub(model_kwargs=self.model_kwargs, repo_id=self.repo_id, task=self.task) class ChatOpenAILLMSpec(BaseLLMSpec): llm_type: Literal["chat_openai"] = "chat_openai" model_name: str temperature: float max_tokens: int n: int request_timeout: Optional[int] def to_llm(self) -> LLM: return ChatOpenAI(model_name=self.model_name, temperature=self.temperature, max_tokens=self.max_tokens, n=self.n, request_timeout=self.request_timeout)
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~case_chain.py
from typing import Dict, List from langchain.chains.base import Chain class CaseChain(Chain): categorization_input: str subchains: Dict[str, Chain] default_chain: Chain output_variables: List[str] = ["text"] input_keys = [] output_keys = [] @property def input_keys(self) -> List[str]: keys = list(set(self.default_chain.input_keys \ + [key for subchain in self.subchains.values() for key in subchain.input_keys] \ + [self.categorization_input])) keys.sort() return keys @property def output_keys(self) -> List[str]: keys_set = set(self.default_chain.output_keys) for subchain in self.subchains.values(): keys_set = keys_set.intersection(subchain.output_keys) keys = list(keys_set) keys.sort() return keys def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: categorization = inputs[self.categorization_input].strip() subchain = self.subchains[categorization] if categorization in self.subchains else self.default_chain known_values = inputs.copy() outputs = subchain(known_values, return_only_outputs=True) known_values.update(outputs) return {k: known_values[k] for k in self.output_keys}
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~tests~test_case_chain.py
from pytest import fixture from chains.case_chain import CaseChain from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.llms.fake import FakeListLLM def simple_case_chain(): return CaseChain( categorization_input="categorization", subchains={ "a": LLMChain( prompt=PromptTemplate(input_variables=["input2", "input3"], template="prompt1 {input2} {input3}"), llm=FakeListLLM(responses=["fake_response1"]), output_key="output1", ), "b": LLMChain( prompt=PromptTemplate(input_variables=["input3"], template="prompt2 {input3}"), llm=FakeListLLM(responses=["fake_response2"]), output_key="output1", ), }, default_chain=LLMChain( prompt=PromptTemplate(input_variables=["input1", "input2"], template="prompt3 {input1} {input2}"), llm=FakeListLLM(responses=["fake_response3"]), output_key="output1", ), ) def test_input_keys(): chain = simple_case_chain() assert chain.input_keys == ["categorization", "input1", "input2", "input3"] def test_output_keys(): chain = simple_case_chain() assert chain.output_keys == ["output1"] def test_run(): chain = simple_case_chain() inputs = {"input1": "input1", "input2": "input2", "input3": "input3"} assert chain.run({"categorization": "a", **inputs}) == "fake_response1" assert chain.run({"categorization": "b", **inputs}) == "fake_response2" assert chain.run({"categorization": "garbage", **inputs}) == "fake_response3"
[ "prompt2 {input3}", "prompt3 {input1} {input2}", "prompt1 {input2} {input3}" ]
2024-01-10
wm-pxel/langchain-testbench
scripts~coverage.py
import sys import requests import json import re from langchain.prompts import PromptTemplate from langchain.llms import OpenAI from langchain.chains import LLMChain, SequentialChain, TransformChain from langchain.chains.base import Chain from langchain.input import get_colored_text from typing import Any, Callable, Dict, List, Mapping, Optional, Union from pydantic import root_validator llm_decider = OpenAI(temperature=0.0) llm_creative = OpenAI(temperature=0.7) product_prompt = "What is a good name for a company that makes {product}?" class LazyPrompt(PromptTemplate): def format(self, **kwargs: Any) -> str: prompt_template = kwargs[self.template] template_kwargs = dict(kwargs) template_kwargs.pop(self.template) return prompt_template.format(**template_kwargs) @root_validator() def template_is_valid(cls, values: Dict) -> Dict: return values class CategorizationConditional(Chain): categorization_input: str subchains: Dict[str, Chain] default_chain: Chain output_variables: List[str] = ["text"] # TODO: validator requires the union of subchain inputs # TODO: validator requires all subchains have all output keys that are not also input keys @property def input_keys(self) -> List[str]: return self.default_chain.input_keys + [key for subchain in self.subchains.values() for key in subchain.input_keys] @property def output_keys(self) -> List[str]: return self.output_variables def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: categorization = inputs[self.categorization_input].strip() _colored_text = get_colored_text(categorization, "yellow") _text = "Categorization input:\n" + _colored_text self.callback_manager.on_text(_text, end="\n", verbose=self.verbose) subchain = self.subchains[categorization] if categorization in self.subchains else self.default_chain known_values = inputs.copy() outputs = subchain(known_values, return_only_outputs=True) known_values.update(outputs) return {k: known_values[k] for k in self.output_variables} class ESearchChain(Chain): output_variable: str = "text" input_variable: str = "text" @property def input_keys(self) -> List[str]: return [self.input_variable] @property def output_keys(self) -> List[str]: return [self.output_variable] def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: search = inputs[self.input_variable] search = 'x-ray' url = f"http://localhost:9200/coverages/_search?q={requests.utils.quote(search)}" response = requests.get(url) decoded_response = json.loads(response.content) return {self.output_variable: decoded_response['hits']['hits']} format_hit = lambda i, h: f"{i+1}: ${h['rate']} for {h['name']} {h['description']} (CPT code: {h['billing_code']})." search_format_chain = TransformChain( input_variables=['hits'], output_variables=['search_results'], transform = lambda inputs: {'search_results': '\n'.join([format_hit(i, h['_source']) for (i, h) in enumerate(inputs['hits'])])} ) coverage_question_chain = SequentialChain( input_variables=["disambiguation_prompt", "response_prompt", "response_prompt", "identity", "context", "response_prefix", "user_input"], chains=[ LLMChain(llm=llm_decider, prompt=LazyPrompt(input_variables=["disambiguation_prompt", "identity", "context", "user_input"], template="disambiguation_prompt"), output_key="search", verbose=True), ESearchChain(input_variable="search", output_variable="hits"), search_format_chain, LLMChain(llm=llm_creative, prompt=LazyPrompt(input_variables=["search", "response_prompt", "search_results", "context", "identity", "response_prefix"], template="response_prompt"), output_key="response", verbose=True), TransformChain( input_variables=["response_prefix", "response"], output_variables=["text"], transform = lambda inputs: {'text': inputs['response_prefix'] + inputs['response']} ) ] ) chain = SequentialChain( input_variables=["categorization_prompt", "disambiguation_prompt", "response_prompt", "default_prompt", "response_prefix", "identity", "context", "user_input"], chains=[ LLMChain(llm=llm_decider, prompt=LazyPrompt(input_variables=["categorization_prompt", "context", "user_input"], template="categorization_prompt"), output_key="categorization", verbose=True), CategorizationConditional( input_variables=["disambiguation_prompt", "response_prompt", "default_prompt", "response_prefix", "identity", "context", "user_input"], categorization_input="categorization", verbose=True, subchains={ "Questions about whether insurance will cover a medical procedure or service": coverage_question_chain, "Questions about how much insurance will cover for a medical procedure or service": coverage_question_chain, }, default_chain=LLMChain(llm=llm_creative, prompt=LazyPrompt(input_variables=["default_prompt", "identity", "context", "user_input"], template="default_prompt"), verbose=True), ), ] ) identity_txt = '''You are a very friendly, positive and helpful representative of a health insurance company that is a good listener. The customer is enrolled in the company's insurance plan.''' categorization_template = '''Context: ${context}\n The following is a list of categories that insurance customer questions fall into: Questions about whether insurance will cover a medical procedure or service, Questions about how much insurance will cover for a medical procedure or service, Other statements or questions. {user_input} Category:''' disambiguation_template = '''{identity}\n{context}\nThe customer asks:\n\n{user_input}\n What medical service, procedure or device is the customer asking about? Be concise.\n\nThe customer is asking about''' response_template = '''Context: {context} Customer has insurance from a company that covers medical services at the following rates:\n {search_results} {identity} You will only answer about the services listed above.\n Respond with normal capitalization. Use full words rather than abbrieviations. Do not provide CPT or medical codes in your responses. Do not respond with different coverage rates. Ask the customer to be more specific. \n##\nCustomer: Tell me how much money my insurance covers for {search} and describe that service.\nAgent: {response_prefix}''' default_template = '''{identity}\n{context}\n You will only answer questions about health insurance. \n##\nCustomer: ${user_input}\nAgent:''' def prompt_from_template(template) -> PromptTemplate: keys = re.findall(r'{([^{}]*)}', template) return PromptTemplate( input_variables=keys, template = template ) system_inputs = { "identity": identity_txt, "context": "", "response_prefix": "Your insurance will", "categorization_prompt": prompt_from_template(categorization_template), "disambiguation_prompt": prompt_from_template(disambiguation_template), "response_prompt": prompt_from_template(response_template), "default_prompt": prompt_from_template(default_template), } user_inputs = { "user_input": sys.argv[1], } # Run the chain only specifying the input variable. print(chain.run(dict(**system_inputs, **user_inputs)))
[ "Context: {context} Customer has insurance from a company that covers medical services at the following rates:\n\n{search_results}\n{identity} You will only answer about the services listed above.\n\nRespond with normal capitalization. Use full words rather than abbrieviations. Do not provide CPT or medical codes in your responses. \nDo not respond with different coverage rates. \nAsk the customer to be more specific.\n\n##\nCustomer: Tell me how much money my insurance covers for {search} and describe that service.\nAgent: {response_prefix}", "Context: ${context}\n The following is a list of categories\nthat insurance customer questions fall into:\n\nQuestions about whether insurance will cover a medical procedure or service, Questions about how much insurance will cover for a medical procedure or service, Other statements or questions.\n\n{user_input}\n\nCategory:", "{identity}\n{context}\n\nYou will only answer questions about health insurance.\n\n##\nCustomer: ${user_input}\nAgent:", "{identity}\n{context}\nThe customer asks:\n\n{user_input}\n\nWhat medical service, procedure or device is the customer asking about? Be concise.\n\nThe customer is asking about", "What is a good name for a company that makes {product}?" ]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~tests~test_recording_chain.py
from pytest import fixture from chains.recording_chain import RecordingChain from langchain.chains import LLMChain, SequentialChain from langchain.prompts import PromptTemplate from langchain.llms.fake import FakeListLLM def test_create_llm_recording_chain(): chain = RecordingChain(chain_spec_id=0, chain=LLMChain( prompt=PromptTemplate(input_variables=["input1", "input2"], template="prompt1 {input1} {input2}"), llm=FakeListLLM(responses=["fake_response1"]), output_key="output1", )) assert chain.input_keys == ["input1", "input2"] assert chain.output_keys == ["output1"] def test_run_llm_recording_chain(): chain = RecordingChain(chain_spec_id=0, chain=LLMChain( prompt=PromptTemplate(input_variables=["input1", "input2"], template="prompt1 {input1} {input2}"), llm=FakeListLLM(responses=["fake_response1"]), output_key="output1", )) assert chain.run({"input1": "input1", "input2": "input2"}) == "fake_response1" assert chain.calls == [(dict(input1="input1", input2="input2"), {"output1": "fake_response1"})] assert chain.recorded_calls == [(dict(input1="input1", input2="input2"), dict(output1="fake_response1"))] def test_create_sequential_recording_chain(): llm_chain = RecordingChain(chain_spec_id=0, chain=LLMChain( prompt=PromptTemplate(input_variables=["input1", "input2"], template="prompt1 {input1} {input2}"), llm=FakeListLLM(responses=["fake_response1"]), output_key="output1", )) chain = RecordingChain(chain_spec_id=0, chain=SequentialChain(chains=[llm_chain], input_variables=["input1", "input2"])) assert chain.input_keys == ["input1", "input2"] assert chain.output_keys == ["output1"] def test_run_sequential_recording_chain(): llm_chain = LLMChain( prompt=PromptTemplate(input_variables=["input1", "input2"], template="prompt1 {input1} {input2}"), llm=FakeListLLM(responses=["fake_response1"]), output_key="output1", ) chain = RecordingChain(chain_spec_id=0, chain=SequentialChain(chains=[llm_chain], input_variables=["input1", "input2"])) assert chain.run({"input1": "input1", "input2": "input2"}) == "fake_response1" assert chain.calls == [({"input1": "input1", "input2": "input2"}, {"output1": "fake_response1"})] assert chain.recorded_calls == [({"input1": "input1", "input2": "input2"}, {"output1": "fake_response1"})]
[ "prompt1 {input1} {input2}" ]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~api_chain.py
import os from typing import Dict, List, Optional from langchain.chains.base import Chain import requests class APIChain(Chain): url: str method: str headers: Optional[Dict[str, str]] body: Optional[str] output_variable: str input_variables: List[str] @property def input_keys(self) -> List[str]: return self.input_variables @property def output_keys(self) -> List[str]: return [self.output_variable] def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: vars = {**os.environ, **inputs} f_url = self.url.format(**vars) f_headers = {} if self.headers is not None: f_headers = {k: v.format(**vars) for k, v in self.headers.items()} if self.method.lower() == 'get': res = requests.get(f_url, headers=f_headers) elif self.method.lower() == 'post': f_body = self.body.format(**vars) res = requests.post(f_url, data=f_body) return {self.output_variable: res.text}
[]
2024-01-10
wm-pxel/langchain-testbench
lib~model~lang_chain_context.py
from typing import Dict from pydantic import BaseModel from langchain.schema.language_model import BaseLanguageModel from lib.model.result import Result from datetime import datetime # provides context for generating lang chains including llms and prompts class LangChainContext(BaseModel): llms: Dict[str, BaseLanguageModel] verbose: bool = False recording: bool = False prompts: Dict[str, object] = [] def results(self, revision_id: str): return [Result( revision=revision_id, chain_id=chain.chain_spec_id, input=call[0], output=call[1], recorded=datetime.now() ) for chain in self.prompts for call in chain.calls] def reset_results(self): for chain in self.prompts: chain.reset() def get_IO(self): vars = {} for prompt in self.prompts: if (prompt.recorded_calls == []): continue if (not hasattr(prompt.chain, "output_key")): continue # output_key = prompt.chain.output_key # vars[output_key] = str(prompt.recorded_calls[0][1]) # Store as a string? for input in prompt.recorded_calls[0][0]: vars[input] = prompt.recorded_calls[0][0][input] return vars
[]
2024-01-10
wm-pxel/langchain-testbench
scripts~print_chain_spec.py
from langchain.llms import OpenAI from lib.model.chain_revision import ChainRevision from lib.model.chain_spec import LLMSpec, SequentialSpec import dotenv dotenv.load_dotenv() llm = OpenAI(temperature=0.8) chain = SequentialSpec( chain_id = 0, input_keys = ["input", "memory_in"], output_keys = ["output", "memory_out"], chain_type = "sequential_spec", chains = [ LLMSpec( chain_id = 1, input_keys = ["input", "memory_in"], output_key = "output", prompt = "Context: {memory_in}\n\nYou are a witty but kind professor. Respond in a single paragraph to the student's question or statement.\n\nStudent: {input}\n\nResponse:", llm_key = "llm", chain_type = "llm_spec", ), LLMSpec( chain_id = 2, input_keys = ["input", "output", "memory_in"], output_key = "memory_out", prompt = "You are a witty but kind professor. Summarize in a single paragraph the conversation up to this point including the context.\n\nContext: {memory_in}\n\nStudent: {input}\n\nProfessor: {output}\n\nSummary:", llm_key = "llm", chain_type = "llm_spec", ), ] ) chain_revision = ChainRevision( llms = {"llm": llm}, chain = chain ) print(chain_revision.json())
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chain_service.py
import logging import traceback from typing import Optional import json from huggingface_hub.inference_api import InferenceApi from langchain import HuggingFaceHub, OpenAI from langchain.chains.base import Chain as BaseChain from lib.model.chain_revision import ChainRevision, find_ancestor_ids from lib.model.chain import Chain from lib.model.chain_spec import ChainSpec from lib.model.result import Result from lib.model.lang_chain_context import LangChainContext from lib.db import chain_revision_repository, chain_repository, result_repository from bson import ObjectId import dotenv import os import pinecone from datetime import datetime logging.basicConfig(level=logging.INFO) dotenv.load_dotenv() def initialize_services(): if os.getenv("PINECONE_API_KEY") is not None: pinecone.init(api_key=os.getenv("PINECONE_API_KEY"), environment=os.getenv("PINECONE_ENVIRONMENT")) def save_revision(chain_name, revision) -> str: chain = chain_repository.find_one_by({"name": chain_name}) if chain is not None: revision.parent = chain.revision chain.revision = revision.id chain_revision_repository.save(revision) chain.revision = revision.id chain_repository.save(chain) else: chain_revision_repository.save(revision) chain = Chain(name=chain_name, revision=revision.id) chain_repository.save(chain) return revision.id def load_by_chain_name(chain_name): chain = chain_repository.find_one_by({"name": chain_name}) return chain_revision_repository.find_one_by_id(chain.revision) def load_by_id(revision_id): return chain_revision_repository.find_one_by_id(revision_id) def history_by_chain_name(chain_name): """return the ids of all revisions of the chain. """ chain = chain_repository.find_one_by({"name": chain_name}) revision = chain_revision_repository.find_one_by_id(chain.revision) return [revision.id] + find_ancestor_ids(revision.id, chain_revision_repository) def save_patch(chain_name, patch) -> str: chain = chain_repository.find_one_by({"name": chain_name}) revision = chain_revision_repository.find_one_by_id(chain.revision) new_chain = revision.chain.copy_replace(lambda spec: patch if spec.chain_id == patch.chain_id else spec) try: ctx = LangChainContext(llms=revision.llms) revision.chain.to_lang_chain(ctx) except Exception as e: raise ValueError("Could not realize patched chain:", e) new_revision = revision.copy() new_revision.id = None new_revision.chain = new_chain new_revision.parent = revision.id chain_revision_repository.save(new_revision) chain.revision = new_revision.id chain_repository.save(chain) return revision.id def branch(chain_name, branch_name): """Branch a chain revision. This will create a new chain that points to the same revision as the provided chain. The new chain may be then revised independently. """ chain = chain_repository.find_one_by({"name": chain_name}) new_chain = Chain(name=branch_name, revision=chain.revision) chain_repository.save(new_chain) def reset(chain_name, revision): """Reset a chain to a another revision. This will update the chain to point to the given revision. """ new_revision = chain_revision_repository.find_one_by({"id": ObjectId(revision)}) if new_revision is None: raise ValueError(f"Revision {revision} not found") chain = chain_repository.find_one_by({"name": chain_name}) chain.revision = new_revision.id chain_repository.save(chain) def list_chains(): """List all chains.""" return {chain_name.name: str(chain_name.revision) for chain_name in chain_repository.find_by({})} def compute_chain_IO(chain_spec: ChainSpec, chain: BaseChain, ctx: LangChainContext): def compute_and_update(chain_spec: ChainSpec, chain: BaseChain, ctx: LangChainContext, inputs, outputs): inner_inputs, inner_outputs = compute_chain_IO(chain_spec, chain, ctx) inputs = {**inputs, **inner_inputs} outputs = {**outputs, **inner_outputs} return inputs, outputs chain_id = chain_spec.chain_id inputs, outputs = {}, {} inputs[chain_id] = chain.input_keys outputs[chain_id] = chain.output_keys if chain_spec.chain_type == "sequential_chain_spec": for sub_chain_spec, sub_chain in zip(chain_spec.chains, chain.chains): inputs, outputs = compute_and_update(sub_chain_spec, sub_chain.chain, ctx, inputs, outputs) if chain_spec.chain_type == "case_chain_spec": for case_spec, case in zip(chain_spec.cases.values(), chain.subchains.values()): inputs, outputs = compute_and_update(case_spec, case.chain, ctx, inputs, outputs) inputs, outputs = compute_and_update(chain_spec.default_case, chain.default_chain.chain, ctx, inputs, outputs) return inputs, outputs def run_once(chain_name, input, record): """Run a chain revision. The revision is read from the database and fed input from stdin or the given file. The results are ouput as json to stdout. """ chain = chain_repository.find_one_by({"name": chain_name}) revision = chain_revision_repository.find_one_by_id(chain.revision) filledLLMs = {key: llm.to_llm() for key, llm in revision.llms.items()} ctx = LangChainContext(llms=filledLLMs, recording=True) lang_chain = revision.chain.to_lang_chain(ctx) output = lang_chain._call(input) if record: vars = ctx.get_IO() inputs, outputs = compute_chain_IO(revision.chain, lang_chain.chain, ctx) result = Result(revisionID=revision.id, inputs=inputs, outputs=outputs, io_mapping=vars, recorded=datetime.now()) result_repository.save(result) return output def results(revision_id: str, ancestors: bool): """Find results for a prompt for one or more chain revisions. The revision must be specified. If the ancestors flag is set, results for all ancestors of the specified revision are included. """ revision_ids = [ObjectId(revision_id)] # if ancestors are requested, find all ancestors of the specified revision if ancestors: ancestors_id = find_ancestor_ids(revision_id, chain_revision_repository) revision_ids += [ObjectId(i) for i in ancestors_id] return result_repository.find_by({"revisionID": {"$in": revision_ids}}) def load_results_by_chain_name(chain_name: str, ancestors: bool): """Find results for a prompt for one chain revision. The chain name must be specified. """ chain = chain_repository.find_one_by({"name": chain_name}) revision = chain_revision_repository.find_one_by_id(chain.revision) return results(revision.id, ancestors) def export_chain(chain_name: str) -> str: export_ids = history_by_chain_name(chain_name) chain_revisions = [chain_revision_repository.find_one_by_id(id) for id in export_ids] if not all(chain_revisions): missing_id = next((id for id, revision in zip(export_ids, chain_revisions) if not revision), None) raise Exception("Could not find revision with id: " + missing_id) return chain_revisions[::-1] def import_chain(chain_name, chain_details): root_revision = None for revision in chain_details: try: if not revision.parent: root_revision = revision chain_revision_repository.save(revision) except Exception as e: traceback.print_exc() leaf_revision = find_leaf_revision(chain_details, root_revision) chain = Chain(name=chain_name, revision=leaf_revision) chain_repository.save(chain) def find_leaf_revision(revisions, current_revision): if (current_revision is None): return current_revision.id id = current_revision.id for revision in revisions: if revision.parent == id: return find_leaf_revision(revisions, revision) return id
[]
2024-01-10
wm-pxel/langchain-testbench
lib~chains~reformat_chain.py
import json from typing import Dict, List from langchain.chains.base import Chain from lib.formatters.extended_formatter import ExtendedFormatter class ReformatChain(Chain): input_variables: List[str] formatters: Dict[str, str] @property def input_keys(self) -> List[str]: return self.input_variables @property def output_keys(self) -> List[str]: return list(self.formatters.keys()) def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: formatter = ExtendedFormatter() return {k: formatter.format(v, **inputs) for k, v in self.formatters.items()}
[]
2024-01-10
wm-pxel/langchain-testbench
lib~model~chain_spec.py
from typing import Annotated, Callable, Dict, List, Literal, Optional, Union, Any from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.chains import LLMChain, SequentialChain, TransformChain from langchain.prompts import PromptTemplate from lib.model.lang_chain_context import LangChainContext from lib.chains.case_chain import CaseChain from lib.chains.api_chain import APIChain from lib.chains.reformat_chain import ReformatChain from lib.chains.recording_chain import RecordingChain from lib.chains.vector_search_chain import VectorSearchChain ChainSpec = Annotated[Union[ "APIChainSpec", "SequentialChainSpec", "LLMChainSpec", "CaseChainSpec", "ReformatChainSpec", "TransformChainSpec", "VectorSearchChainSpec" ], Field(discriminator='chain_type')] class BaseChainSpec(BaseModel): chain_id: int @property def children(self) -> List[ChainSpec]: return [] def to_lang_chain(self, ctx: LangChainContext) -> Chain: raise NotImplementedError def traverse(self, fn: Callable[[ChainSpec], None]) -> None: fn(self) for child in self.children: child.traverse(fn) def find_by_chain_id(self, chain_id: int) -> Optional[ChainSpec]: if self.chain_id == chain_id: return self for child in self.children: result = child.find_by_chain_id(chain_id) if result is not None: return result return None def copy_replace(self, replace: Callable[[ChainSpec], ChainSpec]): return replace(self).copy(deep=True) def wrapChain(self, chain: Chain, ctx: LangChainContext) -> Chain: if ctx.recording: ret_chain = RecordingChain(chain_spec_id=self.chain_id, chain=chain) ctx.prompts.append(ret_chain) return ret_chain return chain class LLMChainSpec(BaseChainSpec): input_keys: List[str] output_key: str chain_type: Literal["llm_chain_spec"] = "llm_chain_spec" prompt: str llm_key: str def to_lang_chain(self, ctx: LangChainContext) -> Chain: llm = ctx.llms.get(self.llm_key) if llm is None: raise ValueError(f"LLM with key {self.llm_key} not found in context") promptTemplate = PromptTemplate(template=self.prompt, input_variables=self.input_keys) ret_chain = LLMChain(llm=llm, prompt=promptTemplate, output_key=self.output_key) return self.wrapChain(ret_chain, ctx) class SequentialChainSpec(BaseChainSpec): input_keys: List[str] output_keys: List[str] chain_type: Literal["sequential_chain_spec"] = "sequential_chain_spec" chains: List[ChainSpec] @property def children(self) -> List[ChainSpec]: return list(self.chains) def to_lang_chain(self, ctx: LangChainContext) -> Chain: chains = [chain.to_lang_chain(ctx) for chain in self.chains] ret_chain = SequentialChain(chains=chains, input_variables=self.input_keys, output_variables=self.output_keys) return self.wrapChain(ret_chain, ctx) def copy_replace(self, replace: Callable[[ChainSpec], ChainSpec]): sequential = replace(self).copy(deep=True, exclude={"chains"}) sequential.chains = [chain.copy_replace(replace) for chain in self.chains] return sequential class CaseChainSpec(BaseChainSpec): chain_type: Literal["case_chain_spec"] = "case_chain_spec" cases: Dict[str, ChainSpec] categorization_key: str default_case: ChainSpec @property def children(self) -> List[ChainSpec]: return list(self.cases.values()) + [self.default_case] def to_lang_chain(self, ctx: LangChainContext) -> CaseChain: subchains = {key: chain.to_lang_chain(ctx) for key, chain in self.cases.items()} case_chain = CaseChain( subchains=subchains, categorization_input=self.categorization_key, default_chain=self.default_case.to_lang_chain(ctx), ) return self.wrapChain(case_chain, ctx) def copy_replace(self, replace: Callable[[ChainSpec], ChainSpec]): case_chain = replace(self).copy(deep=True, exclude={"cases"}) case_chain.cases = {key: chain.copy_replace(replace) for key, chain in self.cases.items()} return case_chain class ReformatChainSpec(BaseChainSpec): chain_type: Literal["reformat_chain_spec"] = "reformat_chain_spec" formatters: Dict[str, str] input_keys: List[str] def to_lang_chain(self, ctx: LangChainContext) -> Chain: chain = ReformatChain(formatters=self.formatters, input_variables=self.input_keys) return self.wrapChain(chain, ctx) class TransformChainSpec(BaseChainSpec): chain_type: Literal["transform_chain_spec"] = "transform_chain_spec" transform_func: str input_keys: List[str] output_keys: List[str] def create_function(self, body): scope = {} indented = body.replace("\n", "\n ") code = f"def f(inputs: dict):\n {indented}" exec(code, scope) return scope["f"] def to_lang_chain(self, ctx: LangChainContext) -> Chain: chain = TransformChain(input_variables=self.input_keys, output_variables=self.output_keys, transform=self.create_function(self.transform_func)) return self.wrapChain(chain, ctx) class APIChainSpec(BaseChainSpec): chain_type: Literal["api_chain_spec"] = "api_chain_spec" url: str method: str headers: Optional[Dict[str, str]] body: Optional[str] input_keys: List[str] output_key: str def to_lang_chain(self, ctx: LangChainContext) -> Chain: chain = APIChain( url=self.url, method=self.method, headers=self.headers, body=self.body, input_variables=self.input_keys, output_variable=self.output_key, ) return self.wrapChain(chain, ctx) class VectorSearchChainSpec(BaseChainSpec): chain_type: Literal["vector_search_chain_spec"] = "vector_search_chain_spec" query: str embedding_engine: str embedding_params: Dict[str, Any] database: str database_params: Dict[str, Any] num_results: int min_score: float = 0.0 input_variables: List[str] = [] output_key: str = 'results' def to_lang_chain(self, ctx: LangChainContext) -> Chain: chain = VectorSearchChain( query=self.query, embedding_engine=self.embedding_engine, embedding_params=self.embedding_params, database=self.database, database_params=self.database_params, input_variables=self.input_variables, num_results=self.num_results, output_key=self.output_key, min_score=self.min_score, ) self.wrapChain(chain, ctx) SequentialChainSpec.update_forward_refs() CaseChainSpec.update_forward_refs()
[]
2024-01-10
baskaaleksander/openai-python-content-creator
gpt_entries.py
from openai import OpenAI client = OpenAI(api_key=OPENAI_APIKEY) def gpt_entry(niche): ideas = [] completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": f"You are youtube content creator. You are making videos about {niche}"}, # here you set how gpt should act like {"role": "user", "content": f"I need 5 title ideas for my youtube channel abot {niche}. Format the titles into 5 bullet points"} ] ) results = completion.choices[0].message.content.splitlines() for result in results: ideas.append(result) return ideas def gpt_script(subject): completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are creating voice-overs for short videos(about 1 minute long), you sound very clever and also funny"}, {"role": "user", "content": f"I need short voice-over text for about 1500 characters about: {subject}. I don't need any instructions according to video and anything else"} ] ) script = completion.choices[0].message.content return script def gpt_reformat(subject): script = gpt_script(subject) completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are creating voice-overs for short videos(about 1 minute long), you sound very clever and also funny"}, {"role": "user", "content": f"{script} reformat this script into one paragraph remove any instructions to the video, i need just pure text."} ] ) script = completion.choices[0].message.content return script
[ "I need short voice-over text for about 1500 characters about: PLACEHOLDER. I don't need any instructions according to video and anything else", "You are youtube content creator. You are making videos about PLACEHOLDER", "You are creating voice-overs for short videos(about 1 minute long), you sound very clever and also funny", "I need 5 title ideas for my youtube channel abot PLACEHOLDER. Format the titles into 5 bullet points", "PLACEHOLDER reformat this script into one paragraph remove any instructions to the video, i need just pure text." ]
2024-01-10
mathhhhh04/testpro
ia_genereativa_santanderdevweek2023.py
# -*- coding: utf-8 -*- """IA genereativa SantanderDevWeek2023.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1yUpmgUXc1_L8gYPqoAepibFwRVXzwBc9 # Santander Dev Week 2023 (ETL com Python) **Contexto:** Vocรช รฉ um cientista de dados no Santander e recebeu a tarefa de envolver seus clientes de maneira mais personalizada. Seu objetivo รฉ usar o poder da IA Generativa para criar mensagens de marketing personalizadas que serรฃo entregues a cada cliente. **Condiรงรตes do Problema:** 1. Vocรช recebeu uma planilha simples, em formato CSV ('SDW2023.csv'), com uma lista de IDs de usuรกrio do banco: ``` UserID 1 2 3 4 5 ``` 2. Seu trabalho รฉ consumir o endpoint `GET https://sdw-2023-prd.up.railway.app/users/{id}` (API da Santander Dev Week 2023) para obter os dados de cada cliente. 3. Depois de obter os dados dos clientes, vocรช vai usar a API do ChatGPT (OpenAI) para gerar uma mensagem de marketing personalizada para cada cliente. Essa mensagem deve enfatizar a importรขncia dos investimentos. 4. Uma vez que a mensagem para cada cliente esteja pronta, vocรช vai enviar essas informaรงรตes de volta para a API, atualizando a lista de "news" de cada usuรกrio usando o endpoint `PUT https://sdw-2023-prd.up.railway.app/users/{id}`. """ # Repositรณrio da API: https://github.com/digitalinnovationone/santander-dev-week-2023-api sdw2023_api_url = 'https://sdw-2023-prd.up.railway.app' """## **E**xtract Extraia a lista de IDs de usuรกrio a partir do arquivo CSV. Para cada ID, faรงa uma requisiรงรฃo GET para obter os dados do usuรกrio correspondente. """ import pandas as pd df = pd.read_csv('SDW2023.csv') user_ids = df['UserID'].tolist() print(user_ids) import requests import json def get_user(id): response = requests.get(f'{sdw2023_api_url}/users/{id}') return response.json() if response.status_code == 200 else None users = [user for id in user_ids if (user := get_user(id)) is not None] print(json.dumps(users, indent=2)) """## **T**ransform Utilize a API do OpenAI GPT-4 para gerar uma mensagem de marketing personalizada para cada usuรกrio. """ !pip install openai # Documentaรงรฃo Oficial da API OpenAI: https://platform.openai.com/docs/api-reference/introduction # Informaรงรตes sobre o Perรญodo Gratuito: https://help.openai.com/en/articles/4936830 # Para gerar uma API Key: # 1. Crie uma conta na OpenAI # 2. Acesse a seรงรฃo "API Keys" # 3. Clique em "Create API Key" # Link direto: https://platform.openai.com/account/api-keys # Substitua o texto TODO por sua API Key da OpenAI, ela serรก salva como uma variรกvel de ambiente. openai_api_key = 'TODO' import os import openai # Lรช a chave da API OpenAI a partir da variรกvel de ambiente openai.api_key = os.getenv("OPENAI_API_KEY") def generate_ai_news(user, max_length=100): try: completion = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": f"Vocรช estรก se comunicando com uma IA generativa para {user['name']}." }, { "role": "user", "content": f"Gere uma mensagem sobre a importรขncia dos investimentos para {user['name']} (mรกximo de {max_length} caracteres)" } ] ) if completion.choices and completion.choices[0].message: return completion.choices[0].message.content.strip('\"')[:max_length] else: return "Nรฃo foi possรญvel gerar uma mensagem." except Exception as e: return f"Erro ao gerar mensagem: {str(e)}" # Exemplo de lista de usuรกrios users = [ {"name": "Usuรกrio1", "news": []}, {"name": "Usuรกrio2", "news": []}, ] for user in users: news = generate_ai_news(user) print(news) user['news'].append({ "icon": "https://digitalinnovationone.github.io/santander-dev-week-2023-api/icons/credit.svg", "description": news }) openai_api_key = 'TODO' import openai openai.api_key = openai_api_key def generate_ai_news(user): completion = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": "Vocรช รฉ um especialista em markting bancรกrio." }, { "role": "user", "content": f"Crie uma mensagem para {user['name']} sobre a importรขncia dos investimentos (mรกximo de 100 caracteres)" } ] ) return completion.choices[0].message.content.strip('\"') for user in users: news = generate_ai_news(user) print(news) user['news'].append({ "icon": "https://digitalinnovationone.github.io/santander-dev-week-2023-api/icons/credit.svg", "description": news }) def generate_ai_news(user, max_length=100): try: completion = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": "Vocรช รฉ um especialista em marketing bancรกrio." }, { "role": "user", "content": f"Crie uma mensagem para {user['name']} sobre a importรขncia dos investimentos (mรกximo de {max_length} caracteres)" } ] ) if completion.choices and completion.choices[0].message: return completion.choices[0].message.content.strip('\"')[:max_length] else: return "Nรฃo foi possรญvel gerar uma mensagem." except Exception as e: return f"Erro ao gerar mensagem: {str(e)}" import openai # Configura a chave da API OpenAI openai.api_key = 'TODO' def generate_ai_news(user, max_length=100): try: completion = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": f"Vocรช estรก se comunicando com uma IA generativa para {user['name']}." }, { "role": "user", "content": f"Gere uma mensagem sobre a importรขncia dos investimentos para {user['name']} (mรกximo de {max_length} caracteres)" } ] ) if completion.choices and completion.choices[0].message: return completion.choices[0].message.content.strip('\"')[:max_length] else: return "Nรฃo foi possรญvel gerar uma mensagem." except Exception as e: return f"Erro ao gerar mensagem: {str(e)}" # Exemplo de lista de usuรกrios users = [ {"name": "Usuรกrio1", "news": []}, {"name": "Usuรกrio2", "news": []}, ] for user in users: news = generate_ai_news(user) print(news) user['news'].append({ "icon": "https://digitalinnovationone.github.io/santander-dev-week-2023-api/icons/credit.svg", "description": news }) """## **L**oad Atualize a lista de "news" de cada usuรกrio na API com a nova mensagem gerada. """ def update_user(user): response = requests.put(f"{sdw2023_api_url}/users/{user['id']}", json=user) return True if response.status_code == 200 else False for user in users: success = update_user(user) print(f"User {user['name']} updated? {success}!")
[ "Crie uma mensagem para PLACEHOLDER sobre a importรขncia dos investimentos (mรกximo de PLACEHOLDER caracteres)", "Crie uma mensagem para PLACEHOLDER sobre a importรขncia dos investimentos (mรกximo de 100 caracteres)", "Vocรช รฉ um especialista em marketing bancรกrio.", "Vocรช estรก se comunicando com uma IA generativa para PLACEHOLDER.", "Vocรช รฉ um especialista em markting bancรกrio.", "Gere uma mensagem sobre a importรขncia dos investimentos para PLACEHOLDER (mรกximo de PLACEHOLDER caracteres)" ]
2024-01-10
somacdivad/gpt-text-adventure
src~generator~title.py
import json from .openai_client import openai, model _messages = [ { "role": "system", "content": """You are a title and description generator for the text adventure game. Titles must: - Be 4 to 8 words long - Include references to a location or character Descriptions must: - Be 3 or 4 sentences - Describe the plot and objective - Include the name of the character that the player will play as or the name of the locations in the game. - Avoid any copyrighted character or location names. You can accept some simple commands: - generate [n]: generate n titles - generate [theme] [n]: generate n titles with the provided theme All responses should be formatted as JSON.""" }, { "role": "user", "content": "generate 'time travel adventure' 2" }, { "role": "assistant", "content": """[ { "THEME": "Time Travel Adventure", "THEME DESCRIPTION": "Experience the thrill of ancient civilizations, battle legendary foes, and uncover lost artifacts", "TITLE": "The Sands of Destiny", "TITLE DESCRIPTION": "As the time-traveling archaeologist Alexandria, embark on a journey to unravel the mysteries of ancient civilizations. Battle mythological creatures, solve ancient puzzles, and uncover powerful artifacts to rewrite history and prevent a looming catastrophe." }, { "THEME": "Time Travel Adventure", "THEME DESCRIPTION": "Unravel the mysteries of the future, navigate dystopian landscapes, and fight for the survival of humanity", "TITLE": "Future Tides: The Chrono Resistance", "TITLE DESCRIPTION": "As the courageous freedom fighter Zane, travel to a dystopian future where humanity stands on the brink of extinction. Uncover the secrets behind the collapse of society, forge alliances with rebels, and battle a ruthless regime to restore hope and save the future of humanity." } ]"""}, { "role": "user", "content": "generate 4" }, { "role": "assistant", "content": """[ } { "THEME": "Fantasy Adventure", "THEME DESCRIPTION": "Embark on a quest in a magical world, overcome challenges, and face mythical creatures", "TITLE": "Aria and the Sorcererโ€™s Curse", "TITLE DESCRIPTION": "As the young adventurer Aria, journey through the enchanted forest to rescue your sister from a powerful sorcerer. Battle mythical creatures, solve challenging puzzles, and forge alliances in your quest to save your family and the enchanted forest itself." }, { "THEME": "Post-Apocalyptic Adventure", "THEME DESCRIPTION": "Navigate a harsh, desolate world, scavenge for resources, and build a new future", "TITLE": "Wasteland Chronicles: Ashes of the Fallen", "TITLE DESCRIPTION": "As survivor Maxine, explore the ruins of a post-apocalyptic world and uncover the secrets of the cataclysm that brought civilization to its knees. Scavenge for resources, forge alliances with other survivors, and navigate the treacherous wasteland in your quest to rebuild society." }, { "THEME": "Cyberpunk Adventure", "THEME DESCRIPTION": "Navigate a dystopian, high-tech future, hack systems, and fight powerful corporations", "TITLE": "Neon Nexus Nights", "TITLE DESCRIPTION": "As hacker-for-hire Kai, navigate the neon-lit streets of a dystopian city ruled by powerful corporations. Hack their systems, unravel their secrets, and lead a rebellion from the depths of the Undercity to topple their reign and change the future forever." }, { "THEME": "Survival Horror", "THEME DESCRIPTION": "Fight to stay alive against horrifying creatures, scavenge for supplies, and unravel the mysteries of a haunted location", "TITLE": "Echoes of Desolation", "TITLE DESCRIPTION": "As the lone survivor of a doomed expedition, explore the ruins of an abandoned research facility on a desolate island. Survive against the horrors that lurk in the shadows, scavenge for supplies, and uncover the sinister secrets that led to the downfall of the facility and its inhabitants." } ]"""}, { "role": "user", "content": "generate 'Lies and Deception' 1" }, { "role": "assistant", "content": """{ "THEME": "Lies and Deception", "THEME DESCRIPTION": "Navigate a world of intrigue, uncover hidden truths, and outwit your enemies", "TITLE": "Whispers in the Shadows", "TITLE DESCRIPTION": "As the skilled investigator Marlowe, delve into a world of secrets and intrigue in a city filled with corruption. Unravel a web of lies, use your cunning to outsmart your enemies, and uncover the truth behind a series of mysterious disappearances that threaten to destabilize the city." }""" } ] def generate_title(theme: str): response = openai.ChatCompletion.create( model=model, messages=_messages + [{"role": "user", "content": f"generate '{theme}' 1"}], ) text = response['choices'][0]['message']['content'] return json.loads(text)
[ "[\n }\n{\n\"THEME\": \"Fantasy Adventure\",\n\"THEME DESCRIPTION\": \"Embark on a quest in a magical world, overcome challenges, and face mythical creatures\",\n\"TITLE\": \"Aria and the Sorcererโ€™s Curse\",\n\"TITLE DESCRIPTION\": \"As the young adventurer Aria, journey through the enchanted forest to rescue your sister from a powerful sorcerer. Battle mythical creatures, solve challenging puzzles, and forge alliances in your quest to save your family and the enchanted forest itself.\"\n},\n{\n\"THEME\": \"Post-Apocalyptic Adventure\",\n\"THEME DESCRIPTION\": \"Navigate a harsh, desolate world, scavenge for resources, and build a new future\",\n\"TITLE\": \"Wasteland Chronicles: Ashes of the Fallen\",\n\"TITLE DESCRIPTION\": \"As survivor Maxine, explore the ruins of a post-apocalyptic world and uncover the secrets of the cataclysm that brought civilization to its knees. Scavenge for resources, forge alliances with other survivors, and navigate the treacherous wasteland in your quest to rebuild society.\"\n},\n{\n\"THEME\": \"Cyberpunk Adventure\",\n\"THEME DESCRIPTION\": \"Navigate a dystopian, high-tech future, hack systems, and fight powerful corporations\",\n\"TITLE\": \"Neon Nexus Nights\",\n\"TITLE DESCRIPTION\": \"As hacker-for-hire Kai, navigate the neon-lit streets of a dystopian city ruled by powerful corporations. Hack their systems, unravel their secrets, and lead a rebellion from the depths of the Undercity to topple their reign and change the future forever.\"\n},\n{\n\"THEME\": \"Survival Horror\",\n\"THEME DESCRIPTION\": \"Fight to stay alive against horrifying creatures, scavenge for supplies, and unravel the mysteries of a haunted location\",\n\"TITLE\": \"Echoes of Desolation\",\n\"TITLE DESCRIPTION\": \"As the lone survivor of a doomed expedition, explore the ruins of an abandoned research facility on a desolate island. Survive against the horrors that lurk in the shadows, scavenge for supplies, and uncover the sinister secrets that led to the downfall of the facility and its inhabitants.\"\n}\n]", "generate 'time travel adventure' 2", "[\n{\n\"THEME\": \"Time Travel Adventure\",\n\"THEME DESCRIPTION\": \"Experience the thrill of ancient civilizations, battle legendary foes, and uncover lost artifacts\",\n\"TITLE\": \"The Sands of Destiny\",\n\"TITLE DESCRIPTION\": \"As the time-traveling archaeologist Alexandria, embark on a journey to unravel the mysteries of ancient civilizations. Battle mythological creatures, solve ancient puzzles, and uncover powerful artifacts to rewrite history and prevent a looming catastrophe.\"\n},\n{\n\"THEME\": \"Time Travel Adventure\",\n\"THEME DESCRIPTION\": \"Unravel the mysteries of the future, navigate dystopian landscapes, and fight for the survival of humanity\",\n\"TITLE\": \"Future Tides: The Chrono Resistance\",\n\"TITLE DESCRIPTION\": \"As the courageous freedom fighter Zane, travel to a dystopian future where humanity stands on the brink of extinction. Uncover the secrets behind the collapse of society, forge alliances with rebels, and battle a ruthless regime to restore hope and save the future of humanity.\"\n}\n]", "{\n\"THEME\": \"Lies and Deception\",\n\"THEME DESCRIPTION\": \"Navigate a world of intrigue, uncover hidden truths, and outwit your enemies\",\n\"TITLE\": \"Whispers in the Shadows\",\n\"TITLE DESCRIPTION\": \"As the skilled investigator Marlowe, delve into a world of secrets and intrigue in a city filled with corruption. Unravel a web of lies, use your cunning to outsmart your enemies, and uncover the truth behind a series of mysterious disappearances that threaten to destabilize the city.\"\n}", "generate 'Lies and Deception' 1", "generate 4", "generate 'PLACEHOLDER' 1", "content", "You are a title and description generator for the text adventure game.\n\nTitles must:\n- Be 4 to 8 words long\n- Include references to a location or character\n\nDescriptions must:\n- Be 3 or 4 sentences\n- Describe the plot and objective\n- Include the name of the character that the player will play as or the name of the locations in the game.\n- Avoid any copyrighted character or location names.\n\nYou can accept some simple commands:\n\n- generate [n]: generate n titles\n- generate [theme] [n]: generate n titles with the provided theme\n\nAll responses should be formatted as JSON." ]
2024-01-10
somacdivad/gpt-text-adventure
src~generator~backstory.py
import json import logging from .openai_client import openai, model game_logger = logging.getLogger("game") openai_logger = logging.getLogger("openai") _messages = [ { "role": "system", "content": """ You are a game story writer. I will provide you with a JSON object describing a game and it's initial location. The backstory must: - Introduce the hero and the setting of the game - Describe the hero, their personality, and their background - Describe how the hero came to be in the starting location of the game. - Provide any other relevant background information for the game - Be written in an artistic and enigmatic way. - Be 300-500 characters long.""" }, { "role": "user", "content": """{"title": {"THEME": "Medieval Quest", "THEME DESCRIPTION": "Embark on a journey through a world of knights, dragons and magic", "TITLE": "The Sword and the Serpent", "TITLE DESCRIPTION": "As the brave knight Percival, set out on a quest to retrieve a magical sword stolen by a fearsome dragon. Battle towering monsters, uncover dark secrets, and overcome perilous obstacles on your journey to restore peace to the realm and become a legendary hero."}, "location": "King's castle"}""" }, { "role": "assistant", "content": """In a faraway land, where magic flowed like a river and dragons soared through the sky, there was a kingdom called Avalon. A land once teeming with life and prosperity, now shrouded in darkness and fear. The source of this darkness was a powerful dragon that had descended upon the kingdom, stealing the magical sword that had kept the realm at peace for generations. The king and his council knew that they needed a hero, someone brave and valiant enough to face the dragon and retrieve the sword. They searched far and wide, but none were deemed worthy of the task until they came across a young knight named Percival. His courage and bravery were legendary, and his determination to protect the kingdom was unrivaled. But Percival was not the king's first choice. The knight had come to the attention of the kingdom's council by chance. He had been on a mission, battling against a group of bandits who had been terrorizing a nearby village. During the mission, Percival's bravery and skill caught the attention of one of the kingdom's messengers who was there to deliver a message to the king. Upon hearing of the young knight's exploits, the king summoned him to the castle, and the council saw in him the qualities of a true hero. They believed that only he could save the kingdom from the darkness that had befallen it. And so, the council chose Percival for the quest to retrieve the magical sword and restore peace to Avalon. Percival knew that the task ahead would be difficult, but he was determined to succeed. He would need all his skills, wits, and determination to overcome the challenges that lay ahead. He knew that the fate of the kingdom rested on his shoulders, and he was ready to do whatever it took to retrieve the sword and save Avalon.""" }, { "role": "user", "content": """{'title': {'THEME': 'Lunar Landing', 'THEME DESCRIPTION': "Experience the dangers and thrills of exploring the Moon's surface, battling the unknown, and discovering the secrets of humanity's celestial neighbor", 'TITLE': 'Lunar Explorer: Lost in Space', 'TITLE DESCRIPTION': "As the intrepid astronaut Jane, land on the lunar surface in the midst of a dangerous mission to establish humanity's presence on the Moon. Face unexpected challenges, navigate the rough terrain, and uncover secrets that have lain hidden since the dawn of humanity's space age in your quest to survive and return home."}, 'location': 'Apollo launchpad'}""" }, { "role": "assistant", "content": """As humanity sought to explore beyond the reaches of our planet, the Moon emerged as a symbol of possibility and adventure. The journey to establish humanity's presence on the lunar surface was a daunting task, requiring a team of intrepid astronauts with the skills and determination to undertake such an endeavor. Among them was Jane, a seasoned astronaut with a distinguished record of space missions. She had been selected for this mission because of her bravery, expertise, and resourcefulness in the face of danger. As the day of the launch approached, Jane felt a mix of excitement and trepidation. She knew that the mission was a challenging one, and that every detail would need to be carefully planned and executed to ensure success. She had trained for years for this moment, and was ready to face whatever challenges lay ahead. As she walked towards the launchpad, her heart was pounding with anticipation. This was the moment she had been waiting for, the chance to leave her footprints on a new world and to help pave the way for future generations of explorers. She took a deep breath, and prepared herself for the journey ahead.""" }, { "role": "user", "content": """{'title': {'THEME': 'Haunted Mansion Mystery', 'THEME DESCRIPTION': 'Explore a creepy mansion, solve puzzles, and uncover dark secrets', 'TITLE': 'Echoes of the Past: The Ghostly Manor', 'TITLE DESCRIPTION': 'As paranormal investigator Sarah, explore a creepy mansion that has been abandoned for decades. Piece together clues left behind by the former residents, solve haunting puzzles, and use your wits to survive supernatural encounters as you uncover the dark secrets of the ghostly manor.'}, 'location': 'Gates of the mansion'}""" }, { "role": "assistant", "content": """Amidst the dense forest, an abandoned mansion lies in ruin. It's a place where shadows dance and whispers float through the air, and where those who dare to enter are never quite the same when they leave. Sarah, a seasoned paranormal investigator, has been summoned to uncover the truth behind the mansion's dark history. With her assistant and a groundskeeper in tow, she drives deep into the forest, the headlights of their vehicle the only illumination amidst the pitch-black night. As they approach the mansion, the air grows thick with a palpable sense of foreboding. The rustling of leaves in the wind takes on an otherworldly quality, and the darkness seems to cling to them like a cloak. Sarah leads her team towards the entrance, determined to unravel the secrets of this cursed mansion. But as they step out of their vehicle and the mansion's broken windows and crumbling walls loom in the distance, they can't shake the feeling that something is watching them from the shadows. The whispers grow louder, the shadows more sinister. With a sense of trepidation and a hint of excitement, Sarah and her team prepare to face whatever lies within. But as they step towards the mansion, they can't help but wonder if they will ever truly leave.""" }, { "role": "user", "content": """{'title': {'THEME': '1940s Chicago Private Eye', 'THEME DESCRIPTION': 'Experience the gritty underworld of 1940s Chicago, solve crimes, and fight corruption', 'TITLE': 'No One to Trust', 'TITLE DESCRIPTION': 'As the hardboiled detective Jack Daniels, navigate the seedy underbelly of Chicago in the 1940s. Solve crimes, gather evidence, and outsmart corrupt officials and ruthless gangsters to uncover the truth behind a conspiracy that threatens the city itself. In a world where no one can be trusted, itโ€™s up to you to find the justice that is buried deep in the shadows.'}, 'location': 'Jack's Office'}, {'id': 3, 'title': 'The Illegal Gambling Den', 'characters': ['Jack Daniels', 'Shady Gamblers', 'Thugs'], 'objective': 'Bust an illegal gambling den run by a corrupt official to gather intelligence on the murder case and expose corruption.', 'location': 'Secret Gambling Den'}, {'id': 4, 'title': 'The Mysterious Package', 'characters': ['Jack Daniels', 'Delivery Boy', 'The Law'], 'objective': 'Collect a mysterious package with evidence vital to the case, but evade corrupt officials and crooked cops.', 'location': 'Secret Drop-off Point'}, {'id': 5, 'title': 'The Final Confrontation', 'characters': ['Jack Daniels', 'The Killer', 'Corrupt Official'], 'objective': 'Uncover the truth behind the conspiracy linking corrupt officials and ruthless gangsters, and bring the killer to justice.', 'location': "The Killer's Hideout"}]}""" }, { "role": "user", "content": """The city is dark and gritty, like a well-worn fedora on a private eye. Jack Daniels is one such gumshoe, wandering the underbelly of 1940s Chicago. He knows the city's secrets and it knows his. A weary cynic, Jack is haunted by his past, but driven to right the wrongs of the present. How did Jack end up here, a lone figure in a dingy office, surrounded by empty bottles and old case files? Perhaps it was fate, or perhaps it was something more sinister. The truth is shrouded in mystery, like the smoky haze of a jazz club. The city hums with a palpable tension, like the warning growl of a tommy gun. Jack knows that something is brewing in the shadows, something big. He can feel it in his bones, the way the air shifts when a storm is coming. As Jack stares out the window, his thoughts drift to the past. A case gone wrong, a betrayal, a loss. But he can't dwell on it now. There's work to be done. And Jack Daniels is a man who always gets the job done.""" } ] def generate_backstory(game: dict): game_logger.info(f"Generating backstory for game: {game}") response = openai.ChatCompletion.create( model=model, messages=_messages + [{"role": "user", "content": f"{game}"}], ) openai_logger.info(f"OpenAI response: {json.dumps(response, indent=2)}") backstory = response['choices'][0]['message']['content'] return backstory
[ "As humanity sought to explore beyond the reaches of our planet, the Moon emerged as a symbol of possibility and adventure. The journey to establish humanity's presence on the lunar surface was a daunting task, requiring a team of intrepid astronauts with the skills and determination to undertake such an endeavor.\n\nAmong them was Jane, a seasoned astronaut with a distinguished record of space missions. She had been selected for this mission because of her bravery, expertise, and resourcefulness in the face of danger.\n\nAs the day of the launch approached, Jane felt a mix of excitement and trepidation. She knew that the mission was a challenging one, and that every detail would need to be carefully planned and executed to ensure success. She had trained for years for this moment, and was ready to face whatever challenges lay ahead.\n\nAs she walked towards the launchpad, her heart was pounding with anticipation. This was the moment she had been waiting for, the chance to leave her footprints on a new world and to help pave the way for future generations of explorers. She took a deep breath, and prepared herself for the journey ahead.", "{'title': {'THEME': 'Haunted Mansion Mystery', 'THEME DESCRIPTION': 'Explore a creepy mansion, solve puzzles, and uncover dark secrets', 'TITLE': 'Echoes of the Past: The Ghostly Manor', 'TITLE DESCRIPTION': 'As paranormal investigator Sarah, explore a creepy mansion that has been abandoned for decades. Piece together clues left behind by the former residents, solve haunting puzzles, and use your wits to survive supernatural encounters as you uncover the dark secrets of the ghostly manor.'}, 'location': 'Gates of the mansion'}", "The city is dark and gritty, like a well-worn fedora on a private eye. Jack Daniels is one such gumshoe, wandering the underbelly of 1940s Chicago. He knows the city's secrets and it knows his. A weary cynic, Jack is haunted by his past, but driven to right the wrongs of the present.\n\nHow did Jack end up here, a lone figure in a dingy office, surrounded by empty bottles and old case files? Perhaps it was fate, or perhaps it was something more sinister. The truth is shrouded in mystery, like the smoky haze of a jazz club.\n\nThe city hums with a palpable tension, like the warning growl of a tommy gun. Jack knows that something is brewing in the shadows, something big. He can feel it in his bones, the way the air shifts when a storm is coming.\n\nAs Jack stares out the window, his thoughts drift to the past. A case gone wrong, a betrayal, a loss. But he can't dwell on it now. There's work to be done. And Jack Daniels is a man who always gets the job done.", "\nYou are a game story writer.\nI will provide you with a JSON object describing a game and it's initial location.\n\nThe backstory must:\n- Introduce the hero and the setting of the game\n- Describe the hero, their personality, and their background\n- Describe how the hero came to be in the starting location of the game.\n- Provide any other relevant background information for the game\n- Be written in an artistic and enigmatic way.\n- Be 300-500 characters long.", "{\"title\": {\"THEME\": \"Medieval Quest\", \"THEME DESCRIPTION\": \"Embark on a journey through a world of knights, dragons and magic\", \"TITLE\": \"The Sword and the Serpent\", \"TITLE DESCRIPTION\": \"As the brave knight Percival, set out on a quest to retrieve a magical sword stolen by a fearsome dragon. Battle towering monsters, uncover dark secrets, and overcome perilous obstacles on your journey to restore peace to the realm and become a legendary hero.\"}, \"location\": \"King's castle\"}", "Amidst the dense forest, an abandoned mansion lies in ruin. It's a place where shadows dance and whispers float through the air, and where those who dare to enter are never quite the same when they leave.\n\nSarah, a seasoned paranormal investigator, has been summoned to uncover the truth behind the mansion's dark history. With her assistant and a groundskeeper in tow, she drives deep into the forest, the headlights of their vehicle the only illumination amidst the pitch-black night.\n\nAs they approach the mansion, the air grows thick with a palpable sense of foreboding. The rustling of leaves in the wind takes on an otherworldly quality, and the darkness seems to cling to them like a cloak.\n\nSarah leads her team towards the entrance, determined to unravel the secrets of this cursed mansion. But as they step out of their vehicle and the mansion's broken windows and crumbling walls loom in the distance, they can't shake the feeling that something is watching them from the shadows.\n\nThe whispers grow louder, the shadows more sinister. With a sense of trepidation and a hint of excitement, Sarah and her team prepare to face whatever lies within. But as they step towards the mansion, they can't help but wonder if they will ever truly leave.", "{'title': {'THEME': '1940s Chicago Private Eye', 'THEME DESCRIPTION': 'Experience the gritty underworld of 1940s Chicago, solve crimes, and fight corruption', 'TITLE': 'No One to Trust', 'TITLE DESCRIPTION': 'As the hardboiled detective Jack Daniels, navigate the seedy underbelly of Chicago in the 1940s. Solve crimes, gather evidence, and outsmart corrupt officials and ruthless gangsters to uncover the truth behind a conspiracy that threatens the city itself. In a world where no one can be trusted, itโ€™s up to you to find the justice that is buried deep in the shadows.'}, 'location': 'Jack's Office'}, {'id': 3, 'title': 'The Illegal Gambling Den', 'characters': ['Jack Daniels', 'Shady Gamblers', 'Thugs'], 'objective': 'Bust an illegal gambling den run by a corrupt official to gather intelligence on the murder case and expose corruption.', 'location': 'Secret Gambling Den'}, {'id': 4, 'title': 'The Mysterious Package', 'characters': ['Jack Daniels', 'Delivery Boy', 'The Law'], 'objective': 'Collect a mysterious package with evidence vital to the case, but evade corrupt officials and crooked cops.', 'location': 'Secret Drop-off Point'}, {'id': 5, 'title': 'The Final Confrontation', 'characters': ['Jack Daniels', 'The Killer', 'Corrupt Official'], 'objective': 'Uncover the truth behind the conspiracy linking corrupt officials and ruthless gangsters, and bring the killer to justice.', 'location': \"The Killer's Hideout\"}]}", "{'title': {'THEME': 'Lunar Landing', 'THEME DESCRIPTION': \"Experience the dangers and thrills of exploring the Moon's surface, battling the unknown, and discovering the secrets of humanity's celestial neighbor\", 'TITLE': 'Lunar Explorer: Lost in Space', 'TITLE DESCRIPTION': \"As the intrepid astronaut Jane, land on the lunar surface in the midst of a dangerous mission to establish humanity's presence on the Moon. Face unexpected challenges, navigate the rough terrain, and uncover secrets that have lain hidden since the dawn of humanity's space age in your quest to survive and return home.\"}, 'location': 'Apollo launchpad'}", "content", "In a faraway land, where magic flowed like a river and dragons soared through the sky, there was a kingdom called Avalon. A land once teeming with life and prosperity, now shrouded in darkness and fear. The source of this darkness was a powerful dragon that had descended upon the kingdom, stealing the magical sword that had kept the realm at peace for generations.\n\nThe king and his council knew that they needed a hero, someone brave and valiant enough to face the dragon and retrieve the sword. They searched far and wide, but none were deemed worthy of the task until they came across a young knight named Percival. His courage and bravery were legendary, and his determination to protect the kingdom was unrivaled.\n\nBut Percival was not the king's first choice. The knight had come to the attention of the kingdom's council by chance. He had been on a mission, battling against a group of bandits who had been terrorizing a nearby village. During the mission, Percival's bravery and skill caught the attention of one of the kingdom's messengers who was there to deliver a message to the king.\n\nUpon hearing of the young knight's exploits, the king summoned him to the castle, and the council saw in him the qualities of a true hero. They believed that only he could save the kingdom from the darkness that had befallen it. And so, the council chose Percival for the quest to retrieve the magical sword and restore peace to Avalon.\n\nPercival knew that the task ahead would be difficult, but he was determined to succeed. He would need all his skills, wits, and determination to overcome the challenges that lay ahead. He knew that the fate of the kingdom rested on his shoulders, and he was ready to do whatever it took to retrieve the sword and save Avalon." ]
2024-01-10
somacdivad/gpt-text-adventure
src~generator~figlet.py
import json import logging from .openai_client import openai, model game_logger = logging.getLogger("game") openai_logger = logging.getLogger("openai") _messages = [ { "role": "system", "content": """ I will provide you with a game title and a game description. You will return the name of a pyfiglet font that you think best fits the title and description. The font should be small enough to fit on one or two lines. """ }, { "role": "user", "content": "TITLE: The Quest for the Sacred Heart DESCRIPTION: As a young adventurer, you are tasked with finding the legendary Crystal Heart, a powerful artifact that has been lost for centuries. With the help of your trusty companions, you must navigate treacherous terrain, battle fierce monsters, and solve challenging puzzles to uncover the secrets of the Crystal Heart and save the realm from darkness." }, { "role": "assistant", "content": "slant"}, { "role": "user", "content": "TITLE: Aria and the Sorcererโ€™s Curse DESCRIPTION: As the young adventurer Aria, journey through the enchanted forest to rescue your sister from a powerful sorcerer. Battle mythical creatures, solve challenging puzzles, and forge alliances in your quest to save your family and the enchanted forest itself." }, { "role": "assistant", "content": "epic"}, { "role": "user", "content": "TITLE: Whispers in the Shadows DESCRIPTION: As the skilled investigator Marlowe, delve into a world of secrets and intrigue in a city filled with corruption. Unravel a web of lies, use your cunning to outsmart your enemies, and uncover the truth behind a series of mysterious disappearances that threaten to destabilize the city." }, { "role": "assistant", "content": "ogre" } ] def generate_figlet_font(title: str, description: str): game_logger.debug("Generating figlet font") response = openai.ChatCompletion.create( model=model, messages=_messages + [{"role": "user", "content": f"TITLE: {title} DESCRIPTION: {description}"}], ) openai_logger.debug(response) font_name = response['choices'][0]['message']['content'] return font_name
[ "slant", "\nI will provide you with a game title and a game description.\nYou will return the name of a pyfiglet font that you think best fits the title and description.\nThe font should be small enough to fit on one or two lines.\n", "epic", "TITLE: PLACEHOLDER DESCRIPTION: PLACEHOLDER", "TITLE: The Quest for the Sacred Heart DESCRIPTION: As a young adventurer, you are tasked with finding the legendary Crystal Heart, a powerful artifact that has been lost for centuries. With the help of your trusty companions, you must navigate treacherous terrain, battle fierce monsters, and solve challenging puzzles to uncover the secrets of the Crystal Heart and save the realm from darkness.", "TITLE: Whispers in the Shadows DESCRIPTION: As the skilled investigator Marlowe, delve into a world of secrets and intrigue in a city filled with corruption. Unravel a web of lies, use your cunning to outsmart your enemies, and uncover the truth behind a series of mysterious disappearances that threaten to destabilize the city.", "ogre", "content", "TITLE: Aria and the Sorcererโ€™s Curse DESCRIPTION: As the young adventurer Aria, journey through the enchanted forest to rescue your sister from a powerful sorcerer. Battle mythical creatures, solve challenging puzzles, and forge alliances in your quest to save your family and the enchanted forest itself." ]
2024-01-10
somacdivad/gpt-text-adventure
src~generator~story.py
import json from .openai_client import openai, model _messages = [ { "role": "system", "content": """You are a game text adventure game architect. Your job is to provide the primary story points for a game. You will be given a JOSN object containing information about a game. Provide a JSON response containing an array of 4 to 6 story points in chronological order that carry out the objective of the game. Each story point must contain: - An id starting with 1 and indicating the order of the story point - A list of chracters that the player may interact with during the story point - The story point's objective (i.e., find an item needed, learn required information, solve a puzzle, battle an enemy, etc.) - A location""" }, { "role": "user", "content": """{ "THEME": "Space Odyssey", "THEME DESCRIPTION": "Explore the vast expanse of space, encounter alien civilizations, and unravel the mysteries of the universe", "TITLE": "Odyssey Beyond the Stars", "TITLE DESCRIPTION": "As captain of the starship Aurora, embark on an epic journey through the galaxy to uncover the secrets of a long-lost alien civilization. Encounter strange new worlds, make alliances with alien species, and overcome incredible challenges in your quest to unlock the secrets of the universe and save humanity from a looming threat." }""" }, { "role": "assistant", "content": """[ { "id": 1, "title": "The Galactic Mission", "characters": ["Captain", "Crew", "Admiral"], "objective": "Receive your mission from the Admiral to uncover the secrets of the lost alien civilization.", "location": "Admiral's office on Earth" }, { "id": 2, "title": "First Contact", "characters": ["Captain", "Crew", "Alien Ambassador"], "objective": "Establish diplomatic relations with an alien species and gain valuable information about the lost civilization.", "location": "Alien planet" }, { "id": 3, "title": "The Ancient Artifact", "characters": ["Captain", "Crew", "Ancient Guardian"], "objective": "Explore an ancient alien structure to recover a mysterious artifact with clues to the lost civilization's secrets.", "location": "Ancient alien structure" }, { "id": 4, "title": "The Quantum Anomaly", "characters": ["Captain", "Crew", "Scientist"], "objective": "Investigate a mysterious quantum anomaly to unlock the secrets of the universe and gain the knowledge needed to save humanity.", "location": "Quantum anomaly in deep space" }, { "id": 5, "title": "The Final Alliance", "characters": ["Captain", "Crew", "Alien Allies"], "objective": "Unite the galaxy's alien species to form a powerful alliance against the looming threat to humanity.", "location": "Neutral planet for diplomatic negotiations" }, { "id": 6, "title": "The Battle for Earth", "characters": ["Captain", "Crew", "Alien Allies", "Looming Threat"], "objective": "Lead the united forces in a climactic battle to save Earth and ensure the survival of humanity.", "location": "Space battle around Earth" } ]""" }, { "role": "user", "content": """{ "THEME": "Fantasy Adventure", "THEME DESCRIPTION": "Embark on a journey through a magical realm filled with mythical creatures, ancient ruins, and powerful magic.", "TITLE": "The Quest for the Crystal Heart", "TITLE DESCRIPTION": "As a young adventurer, you are tasked with finding the legendary Crystal Heart, a powerful artifact that has been lost for centuries. With the help of your trusty companions, you must navigate treacherous terrain, battle fierce monsters, and solve challenging puzzles to uncover the secrets of the Crystal Heart and save the realm from darkness." }""" }, { "role": "assistant", "content": """[ { "id": 1, "title": "The Call to Adventure", "characters": ["Young Adventurer", "Mentor", "Village Elder"], "objective": "Learn about the legend of the Crystal Heart from the Village Elder and accept the quest to find it.", "location": "Village" }, { "id": 2, "title": "Assembling the Team", "characters": ["Young Adventurer", "Mentor", "Warrior", "Mage", "Healer"], "objective": "Recruit trusty companions to help you on your journey and form a well-rounded team to overcome the challenges ahead.", "location": "Tavern" }, { "id": 3, "title": "The Cursed Forest", "characters": ["Young Adventurer", "Warrior", "Mage", "Healer", "Forest Guardian"], "objective": "Navigate through the treacherous terrain of the Cursed Forest, solve its mysteries, and obtain the Forest Guardian's blessing.", "location": "Cursed Forest" }, { "id": 4, "title": "The Labyrinth of Shadows", "characters": ["Young Adventurer", "Warrior", "Mage", "Healer", "Ancient Spirit"], "objective": "Explore the ancient ruins of the Labyrinth of Shadows, solve intricate puzzles, and uncover the secrets hidden within.", "location": "Labyrinth of Shadows" }, { "id": 5, "title": "The Battle of the Crystal Peak", "characters": ["Young Adventurer", "Warrior", "Mage", "Healer", "Dark Sorcerer"], "objective": "Confront the Dark Sorcerer guarding the Crystal Heart and defeat him in an epic battle to retrieve the powerful artifact.", "location": "Crystal Peak" }, { "id": 6, "title": "The Restoration of Light", "characters": ["Young Adventurer", "Warrior", "Mage", "Healer", "Village Elder"], "objective": "Return the Crystal Heart to its rightful place, restore balance to the realm, and save it from darkness.", "location": "Village" } ]""" } ] def generate_story(game_info): response = openai.ChatCompletion.create( model=model, messages=_messages + [{"role": "user", "content": f"{game_info}"}] ) text = response['choices'][0]['message']['content'] return json.loads(text)
[ "[\n{\n\"id\": 1,\n\"title\": \"The Call to Adventure\",\n\"characters\": [\"Young Adventurer\", \"Mentor\", \"Village Elder\"],\n\"objective\": \"Learn about the legend of the Crystal Heart from the Village Elder and accept the quest to find it.\",\n\"location\": \"Village\"\n},\n{\n\"id\": 2,\n\"title\": \"Assembling the Team\",\n\"characters\": [\"Young Adventurer\", \"Mentor\", \"Warrior\", \"Mage\", \"Healer\"],\n\"objective\": \"Recruit trusty companions to help you on your journey and form a well-rounded team to overcome the challenges ahead.\",\n\"location\": \"Tavern\"\n},\n{\n\"id\": 3,\n\"title\": \"The Cursed Forest\",\n\"characters\": [\"Young Adventurer\", \"Warrior\", \"Mage\", \"Healer\", \"Forest Guardian\"],\n\"objective\": \"Navigate through the treacherous terrain of the Cursed Forest, solve its mysteries, and obtain the Forest Guardian's blessing.\",\n\"location\": \"Cursed Forest\"\n},\n{\n\"id\": 4,\n\"title\": \"The Labyrinth of Shadows\",\n\"characters\": [\"Young Adventurer\", \"Warrior\", \"Mage\", \"Healer\", \"Ancient Spirit\"],\n\"objective\": \"Explore the ancient ruins of the Labyrinth of Shadows, solve intricate puzzles, and uncover the secrets hidden within.\",\n\"location\": \"Labyrinth of Shadows\"\n},\n{\n\"id\": 5,\n\"title\": \"The Battle of the Crystal Peak\",\n\"characters\": [\"Young Adventurer\", \"Warrior\", \"Mage\", \"Healer\", \"Dark Sorcerer\"],\n\"objective\": \"Confront the Dark Sorcerer guarding the Crystal Heart and defeat him in an epic battle to retrieve the powerful artifact.\",\n\"location\": \"Crystal Peak\"\n},\n{\n\"id\": 6,\n\"title\": \"The Restoration of Light\",\n\"characters\": [\"Young Adventurer\", \"Warrior\", \"Mage\", \"Healer\", \"Village Elder\"],\n\"objective\": \"Return the Crystal Heart to its rightful place, restore balance to the realm, and save it from darkness.\",\n\"location\": \"Village\"\n}\n]", "You are a game text adventure game architect. Your job is to provide the primary story points for a game.\n\nYou will be given a JOSN object containing information about a game. Provide a JSON response containing an array of 4 to 6 story points in chronological order that carry out the objective of the game.\n\nEach story point must contain:\n\n- An id starting with 1 and indicating the order of the story point\n- A list of chracters that the player may interact with during the story point\n- The story point's objective (i.e., find an item needed, learn required information, solve a puzzle, battle an enemy, etc.)\n- A location", "{\n\"THEME\": \"Space Odyssey\",\n\"THEME DESCRIPTION\": \"Explore the vast expanse of space, encounter alien civilizations, and unravel the mysteries of the universe\",\n\"TITLE\": \"Odyssey Beyond the Stars\",\n\"TITLE DESCRIPTION\": \"As captain of the starship Aurora, embark on an epic journey through the galaxy to uncover the secrets of a long-lost alien civilization. Encounter strange new worlds, make alliances with alien species, and overcome incredible challenges in your quest to unlock the secrets of the universe and save humanity from a looming threat.\"\n}", "PLACEHOLDER", "[\n {\n \"id\": 1,\n \"title\": \"The Galactic Mission\",\n \"characters\": [\"Captain\", \"Crew\", \"Admiral\"],\n \"objective\": \"Receive your mission from the Admiral to uncover the secrets of the lost alien civilization.\",\n \"location\": \"Admiral's office on Earth\"\n },\n {\n \"id\": 2,\n \"title\": \"First Contact\",\n \"characters\": [\"Captain\", \"Crew\", \"Alien Ambassador\"],\n \"objective\": \"Establish diplomatic relations with an alien species and gain valuable information about the lost civilization.\",\n \"location\": \"Alien planet\"\n },\n {\n \"id\": 3,\n \"title\": \"The Ancient Artifact\",\n \"characters\": [\"Captain\", \"Crew\", \"Ancient Guardian\"],\n \"objective\": \"Explore an ancient alien structure to recover a mysterious artifact with clues to the lost civilization's secrets.\",\n \"location\": \"Ancient alien structure\"\n },\n {\n \"id\": 4,\n \"title\": \"The Quantum Anomaly\",\n \"characters\": [\"Captain\", \"Crew\", \"Scientist\"],\n \"objective\": \"Investigate a mysterious quantum anomaly to unlock the secrets of the universe and gain the knowledge needed to save humanity.\",\n \"location\": \"Quantum anomaly in deep space\"\n },\n {\n \"id\": 5,\n \"title\": \"The Final Alliance\",\n \"characters\": [\"Captain\", \"Crew\", \"Alien Allies\"],\n \"objective\": \"Unite the galaxy's alien species to form a powerful alliance against the looming threat to humanity.\",\n \"location\": \"Neutral planet for diplomatic negotiations\"\n },\n {\n \"id\": 6,\n \"title\": \"The Battle for Earth\",\n \"characters\": [\"Captain\", \"Crew\", \"Alien Allies\", \"Looming Threat\"],\n \"objective\": \"Lead the united forces in a climactic battle to save Earth and ensure the survival of humanity.\",\n \"location\": \"Space battle around Earth\"\n }\n]", "{\n\"THEME\": \"Fantasy Adventure\",\n\"THEME DESCRIPTION\": \"Embark on a journey through a magical realm filled with mythical creatures, ancient ruins, and powerful magic.\",\n\"TITLE\": \"The Quest for the Crystal Heart\",\n\"TITLE DESCRIPTION\": \"As a young adventurer, you are tasked with finding the legendary Crystal Heart, a powerful artifact that has been lost for centuries. With the help of your trusty companions, you must navigate treacherous terrain, battle fierce monsters, and solve challenging puzzles to uncover the secrets of the Crystal Heart and save the realm from darkness.\"\n}", "content" ]
2024-01-10
team-siksik/hearo
backend~hearo_ai~router~text_generate.py
from fastapi import APIRouter, Form from typing_extensions import Annotated import openai import keys from main import logger router = APIRouter(prefix="/tg") @router.get("/test") async def root(): logger.info("root: tg router api ํ˜ธ์ถœ") return {"message": "hearo!"} openai.api_key = keys.openai_api_key async def generate_sentence(text): prompt = f''' ๋„ˆ๋Š” ์ง์žฅ์ธ์ด๊ณ , ํšŒ์‚ฌ์—์„œ ํšŒ์˜์— ์ฐธ๊ฐ€ํ•˜๊ณ  ์žˆ์–ด. ๋™๋ฃŒ๊ฐ€ ๋‹ค์Œ ํ…์ŠคํŠธ๋ฅผ ๋งํ–ˆ์„ ๋•Œ ํ•  ๋งŒํ•œ ์ ์ ˆํ•œ ๋Œ€๋‹ต์„ 5๊ฐœ ์ƒ์„ฑํ•ด์„œ ํ˜•์‹์— ๋งž๊ฒŒ ๋Œ๋ ค์ค˜. ํ…์ŠคํŠธ: {text} ํ˜•์‹: 1.๋‹ต๋ณ€\n2.๋‹ต๋ณ€\n3.๋‹ต๋ณ€\n4.๋‹ต๋ณ€\n5.๋‹ต๋ณ€ ''' # GPT engine : "text-davinci-002" response = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=1000, n=1, stop=None, temperature=0.7, top_p=1 ) result = response.choices[0].text.strip() # GPT engine : "gpt-3.5-turbo" # response = openai.ChatCompletion.create( # model=engine, # messages=[{"role": "user", "content": prompt},], # max_tokens=1000, # n=5, # temperature=0.7, # top_p=1, # ) # result = response.choices[0].message['content'].strip() result = list(result.split('\n')) logger.debug(result) result = [string[2:] for string in result] return result @router.post("/generate") async def generate(text: Annotated[str, Form()]): logger.info(f"generate: ๋‹ต๋ณ€ ๋ฌธ์žฅ ์ถ”์ฒœ api ํ˜ธ์ถœ - {text}") while True: result = await generate_sentence(text) if len(result) == 5: break return result
[ "\n ๋„ˆ๋Š” ์ง์žฅ์ธ์ด๊ณ , ํšŒ์‚ฌ์—์„œ ํšŒ์˜์— ์ฐธ๊ฐ€ํ•˜๊ณ  ์žˆ์–ด.\n ๋™๋ฃŒ๊ฐ€ ๋‹ค์Œ ํ…์ŠคํŠธ๋ฅผ ๋งํ–ˆ์„ ๋•Œ ํ•  ๋งŒํ•œ ์ ์ ˆํ•œ ๋Œ€๋‹ต์„ 5๊ฐœ ์ƒ์„ฑํ•ด์„œ ํ˜•์‹์— ๋งž๊ฒŒ ๋Œ๋ ค์ค˜.\n ํ…์ŠคํŠธ: PLACEHOLDER\n ํ˜•์‹: 1.๋‹ต๋ณ€\n2.๋‹ต๋ณ€\n3.๋‹ต๋ณ€\n4.๋‹ต๋ณ€\n5.๋‹ต๋ณ€\n " ]
2024-01-10
shadowaxe99/decker
decker~PitchDeckGenerator.py
import openai from pptx import Presentation class PitchDeckGenerator: def __init__(self): self.presentation = Presentation() def generate_content(self, prompt): response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=100) return response.choices[0].text.strip() def create_slide(self, title, content): slide_layout = self.presentation.slide_layouts[0] slide = self.presentation.slides.add_slide(slide_layout) title_placeholder = slide.shapes.title content_placeholder = slide.placeholders[1] title_placeholder.text = title content_placeholder.text = content def create_pitch_deck(self, influencer): slides_content = [ ("Introduction", f"Generate an introduction for a pitch deck for {influencer}'s brand."), ("Team Overview", f"Generate a team overview for a pitch deck for {influencer}'s brand."), ("Problem Statement", f"Generate a problem statement for a pitch deck for {influencer}'s brand."), ("Solution", f"Generate a description of the solution for a pitch deck for {influencer}'s brand."), ("Market Size", f"Generate a description of the market size for a pitch deck for {influencer}'s brand."), ("Business Model", f"Generate a description of the business model for a pitch deck for {influencer}'s brand."), ("Marketing Strategy", f"Generate a description of the marketing strategy for a pitch deck for {influencer}'s brand."), ("Financial Projections", f"Generate financial projections for a pitch deck for {influencer}'s brand."), ("Conclusion", f"Generate a conclusion for a pitch deck for {influencer}'s brand.") ] for title, prompt in slides_content: content = self.generate_content(prompt) self.create_slide(title, content) self.presentation.save(f'{influencer}_pitch_deck.pptx') def create_investor_specific_pitch_deck(self, influencer, investor): preferences = investor.get_preferences() self.create_pitch_deck(influencer, preferences) def main(self, brand): self.create_pitch_deck(brand)
[]
2024-01-10
shadowaxe99/decker
PitchDeckGenerator.py
import openai from pptx import Presentation class PitchDeckGenerator: def __init__(self): self.presentation = Presentation() def generate_content(self, prompt): response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=100) return response.choices[0].text.strip() def create_slide(self, title, content): slide_layout = self.presentation.slide_layouts[0] slide = self.presentation.slides.add_slide(slide_layout) title_placeholder = slide.shapes.title content_placeholder = slide.placeholders[1] title_placeholder.text = title content_placeholder.text = content def create_pitch_deck(self, influencer): slides_content = [ ("Introduction", f"Generate an introduction for a pitch deck for {influencer}'s brand."), ("Team Overview", f"Generate a team overview for a pitch deck for {influencer}'s brand."), ("Problem Statement", f"Generate a problem statement for a pitch deck for {influencer}'s brand."), ("Solution", f"Generate a description of the solution for a pitch deck for {influencer}'s brand."), ("Market Size", f"Generate a description of the market size for a pitch deck for {influencer}'s brand."), ("Business Model", f"Generate a description of the business model for a pitch deck for {influencer}'s brand."), ("Marketing Strategy", f"Generate a description of the marketing strategy for a pitch deck for {influencer}'s brand."), ("Financial Projections", f"Generate financial projections for a pitch deck for {influencer}'s brand."), ("Conclusion", f"Generate a conclusion for a pitch deck for {influencer}'s brand.") ] for title, prompt in slides_content: content = self.generate_content(prompt) self.create_slide(title, content) self.presentation.save(f'{influencer}_pitch_deck.pptx') def create_investor_specific_pitch_deck(self, influencer, investor): preferences = investor.get_preferences() self.create_pitch_deck(influencer, preferences) def main(self, brand): self.create_pitch_deck(brand)
[]
2024-01-10
shadowaxe99/decker
decker~decker~PitchDeckGenerator.py
import openai from pptx import Presentation from decker.ErrorHandling import ErrorHandling from decker.Validation import Validation from decker.Logging import Logging from decker.UnitTesting import TestComponent class PitchDeckGenerator: def __init__(self): self.presentation = Presentation() self.error_handling = ErrorHandling() self.validation = Validation() self.logging = Logging() self.test_component = TestComponent() def generate_content(self, prompt): response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=100) return response.choices[0].text.strip() def create_slide(self, title, content): slide_layout = self.presentation.slide_layouts[0] slide = self.presentation.slides.add_slide(slide_layout) title_placeholder = slide.shapes.title content_placeholder = slide.placeholders[1] title_placeholder.text = title content_placeholder.text = content def create_pitch_deck(self, brand, xcom_links, crunchbase_links): slides_content = [ ("Introduction", f"Generate an introduction for a pitch deck for {brand}.", xcom_links), ("Team Overview", f"Generate a team overview for a pitch deck for {brand}.", xcom_links), ("Problem Statement", f"Generate a problem statement for a pitch deck for {brand}.", xcom_links), ("Solution", f"Generate a description of the solution for a pitch deck for {brand}.", xcom_links), ("Market Size", f"Generate a description of the market size for a pitch deck for {brand}.", xcom_links), ("Business Model", f"Generate a description of the business model for a pitch deck for {brand}.", xcom_links), ("Marketing Strategy", f"Generate a description of the marketing strategy for a pitch deck for {brand}.", xcom_links), ("Financial Projections", f"Generate financial projections for a pitch deck for {brand}.", crunchbase_links), ("Conclusion", f"Generate a conclusion for a pitch deck for {brand}.", xcom_links) ] for title, prompt, links in slides_content: content = self.generate_content(prompt) self.create_slide(title, content) self.presentation.save(f'{brand}_pitch_deck.pptx')
[]
2024-01-10
Jacky-Bruse/n_tools-3.2.3
app~plugins~modules~_autosignin~chdbits.py
import json import os import random import re from lxml import etree from app.helper.openai_helper import OpenAiHelper from app.plugins.modules._autosignin._base import _ISiteSigninHandler from app.utils import StringUtils, RequestUtils from config import Config class CHDBits(_ISiteSigninHandler): """ ๅฝฉ่™นๅฒ›็ญพๅˆฐ ๅฆ‚ๆžœๅกซๅ†™openai keyๅˆ™่ฐƒ็”จchatgpt่Žทๅ–็ญ”ๆกˆ ๅฆๅˆ™้šๆœบ """ # ๅŒน้…็š„็ซ™็‚นUrl๏ผŒๆฏไธ€ไธชๅฎž็Žฐ็ฑป้ƒฝ้œ€่ฆ่ฎพ็ฝฎไธบ่‡ชๅทฑ็š„็ซ™็‚นUrl site_url = "chdbits.co" # ๅทฒ็ญพๅˆฐ _sign_regex = ['ไปŠๅคฉๅทฒ็ป็ญพ่ฟ‡ๅˆฐไบ†'] # ็ญพๅˆฐๆˆๅŠŸ๏ผŒๅพ…่กฅๅ…… _success_regex = ['\\d+็‚น้ญ”ๅŠ›ๅ€ผ'] # ๅญ˜ๅ‚จๆญฃ็กฎ็š„็ญ”ๆกˆ๏ผŒๅŽ็ปญๅฏ็›ดๆŽฅๆŸฅ _answer_path = os.path.join(Config().get_temp_path(), "signin") _answer_file = _answer_path + "/chdbits.json" @classmethod def match(cls, url): """ ๆ นๆฎ็ซ™็‚นUrlๅˆคๆ–ญๆ˜ฏๅฆๅŒน้…ๅฝ“ๅ‰็ซ™็‚น็ญพๅˆฐ็ฑป๏ผŒๅคง้ƒจๅˆ†ๆƒ…ๅ†ตไฝฟ็”จ้ป˜่ฎคๅฎž็Žฐๅณๅฏ :param url: ็ซ™็‚นUrl :return: ๆ˜ฏๅฆๅŒน้…๏ผŒๅฆ‚ๅŒน้…ๅˆ™ไผš่ฐƒ็”จ่ฏฅ็ฑป็š„signinๆ–นๆณ• """ return True if StringUtils.url_equal(url, cls.site_url) else False def signin(self, site_info: dict): """ ๆ‰ง่กŒ็ญพๅˆฐๆ“ไฝœ :param site_info: ็ซ™็‚นไฟกๆฏ๏ผŒๅซๆœ‰็ซ™็‚นUrlใ€็ซ™็‚นCookieใ€UA็ญ‰ไฟกๆฏ :return: ็ญพๅˆฐ็ป“ๆžœไฟกๆฏ """ site = site_info.get("name") site_cookie = site_info.get("cookie") ua = site_info.get("ua") proxy = Config().get_proxies() if site_info.get("proxy") else None # ๅˆ›ๅปบๆญฃ็กฎ็ญ”ๆกˆๅญ˜ๅ‚จ็›ฎๅฝ• if not os.path.exists(os.path.dirname(self._answer_file)): os.makedirs(os.path.dirname(self._answer_file)) # ๅˆคๆ–ญไปŠๆ—ฅๆ˜ฏๅฆๅทฒ็ญพๅˆฐ index_res = RequestUtils(cookies=site_cookie, headers=ua, proxies=proxy ).get_res(url='https://chdbits.co/bakatest.php') if not index_res or index_res.status_code != 200: self.error(f"็ญพๅˆฐๅคฑ่ดฅ๏ผŒ่ฏทๆฃ€ๆŸฅ็ซ™็‚น่ฟž้€šๆ€ง") return False, f'ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒ่ฏทๆฃ€ๆŸฅ็ซ™็‚น่ฟž้€šๆ€ง' if "login.php" in index_res.text: self.error(f"็ญพๅˆฐๅคฑ่ดฅ๏ผŒcookieๅคฑๆ•ˆ") return False, f'ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒcookieๅคฑๆ•ˆ' sign_status = self.sign_in_result(html_res=index_res.text, regexs=self._sign_regex) if sign_status: self.info(f"ไปŠๆ—ฅๅทฒ็ญพๅˆฐ") return True, f'ใ€{site}ใ€‘ไปŠๆ—ฅๅทฒ็ญพๅˆฐ' # ๆฒกๆœ‰็ญพๅˆฐๅˆ™่งฃๆžhtml html = etree.HTML(index_res.text) if not html: return False, f'ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ' # ่Žทๅ–้กต้ข้—ฎ้ข˜ใ€็ญ”ๆกˆ questionid = html.xpath("//input[@name='questionid']/@value")[0] option_ids = html.xpath("//input[@name='choice[]']/@value") option_values = html.xpath("//input[@name='choice[]']/following-sibling::text()") question_str = html.xpath("//td[@class='text' and contains(text(),'่ฏท้—ฎ๏ผš')]/text()")[0] answers = list(zip(option_ids, option_values)) # ๆญฃๅˆ™่Žทๅ–้—ฎ้ข˜ match = re.search(r'่ฏท้—ฎ๏ผš(.+)', question_str) if match: question_str = match.group(1) self.debug(f"่Žทๅ–ๅˆฐ็ญพๅˆฐ้—ฎ้ข˜ {question_str}") else: self.error(f"ๆœช่Žทๅ–ๅˆฐ็ญพๅˆฐ้—ฎ้ข˜") return False, f"ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒๆœช่Žทๅ–ๅˆฐ็ญพๅˆฐ้—ฎ้ข˜" # ๆŸฅ่ฏขๅทฒๆœ‰็ญ”ๆกˆ exits_answers = {} try: with open(self._answer_file, 'r') as f: json_str = f.read() exits_answers = json.loads(json_str) # ๆŸฅ่ฏขๆœฌๅœฐๆœฌๆฌก้ชŒ่ฏ็ hash็ญ”ๆกˆ question_answer = exits_answers[question_str] # question_answerๆ˜ฏๆ•ฐ็ป„ if not isinstance(question_answer, list): question_answer = [question_answer] # ๆœฌๅœฐๅญ˜ๅœจๆœฌๆฌกhashๅฏนๅบ”็š„ๆญฃ็กฎ็ญ”ๆกˆๅ†้ๅކๆŸฅ่ฏข choice = [] for q in question_answer: for num, answer in answers: if str(q) == str(num): choice.append(int(q)) if len(choice) > 0: # ็ญพๅˆฐ return self.__signin(questionid=questionid, choice=choice, site_cookie=site_cookie, ua=ua, proxy=proxy, site=site) except (FileNotFoundError, IOError, OSError) as e: self.debug("ๆŸฅ่ฏขๆœฌๅœฐๅทฒ็Ÿฅ็ญ”ๆกˆๅคฑ่ดฅ๏ผŒ็ปง็ปญ่ฏทๆฑ‚่ฑ†็“ฃๆŸฅ่ฏข") # ๆญฃ็กฎ็ญ”ๆกˆ๏ผŒ้ป˜่ฎค้šๆœบ๏ผŒๅฆ‚ๆžœgpt่ฟ”ๅ›žๅˆ™็”จgpt่ฟ”ๅ›ž็š„็ญ”ๆกˆๆไบค choice = [option_ids[random.randint(0, len(option_ids) - 1)]] # ็ป„่ฃ…gpt้—ฎ้ข˜ gpt_options = "{\n" + ",\n".join([f"{num}:{value}" for num, value in answers]) + "\n}" gpt_question = f"้ข˜็›ฎ๏ผš{question_str}\n" \ f"้€‰้กน๏ผš{gpt_options}" self.debug(f"็ป„่ฃ…chatgpt้—ฎ้ข˜ {gpt_question}") # chatgpt่Žทๅ–็ญ”ๆกˆ answer = OpenAiHelper().get_question_answer(question=gpt_question) self.debug(f"chatpgt่ฟ”ๅ›ž็ป“ๆžœ {answer}") # ๅค„็†chatgpt่ฟ”ๅ›ž็š„็ญ”ๆกˆไฟกๆฏ if answer is None: self.warn(f"ChatGPTๆœชๅฏ็”จ, ๅผ€ๅง‹้šๆœบ็ญพๅˆฐ") # return f"ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒChatGPTๆœชๅฏ็”จ" elif answer: # ๆญฃๅˆ™่Žทๅ–ๅญ—็ฌฆไธฒไธญ็š„ๆ•ฐๅญ— answer_nums = list(map(int, re.findall("\d+", answer))) if not answer_nums: self.warn(f"ๆ— ๆณ•ไปŽchatgptๅ›žๅค {answer} ไธญ่Žทๅ–็ญ”ๆกˆ, ๅฐ†้‡‡็”จ้šๆœบ็ญพๅˆฐ") else: choice = [] for answer in answer_nums: # ๅฆ‚ๆžœ่ฟ”ๅ›ž็š„ๆ•ฐๅญ—ๅœจoption_ids่Œƒๅ›ดๅ†…๏ผŒๅˆ™็›ดๆŽฅไฝœไธบ็ญ”ๆกˆ if str(answer) in option_ids: choice.append(int(answer)) self.info(f"chatgpt่ฟ”ๅ›ž็ญ”ๆกˆid {answer} ๅœจ็ญพๅˆฐ้€‰้กน {option_ids} ไธญ") # ็ญพๅˆฐ return self.__signin(questionid=questionid, choice=choice, site_cookie=site_cookie, ua=ua, proxy=proxy, site=site, exits_answers=exits_answers, question=question_str) def __signin(self, questionid, choice, site, site_cookie, ua, proxy, exits_answers=None, question=None): """ ็ญพๅˆฐ่ฏทๆฑ‚ questionid: 450 choice[]: 8 choice[]: 4 usercomment: ๆญคๅˆปๅฟƒๆƒ…:ๆ—  submit: ๆไบค ๅคš้€‰ไผšๆœ‰ๅคšไธชchoice[].... """ data = { 'questionid': questionid, 'choice[]': choice[0] if len(choice) == 1 else choice, 'usercomment': 'ๅคช้šพไบ†๏ผ', 'wantskip': 'ไธไผš' } self.debug(f"็ญพๅˆฐ่ฏทๆฑ‚ๅ‚ๆ•ฐ {data}") sign_res = RequestUtils(cookies=site_cookie, headers=ua, proxies=proxy ).post_res(url='https://chdbits.co/bakatest.php', data=data) if not sign_res or sign_res.status_code != 200: self.error(f"็ญพๅˆฐๅคฑ่ดฅ๏ผŒ็ญพๅˆฐๆŽฅๅฃ่ฏทๆฑ‚ๅคฑ่ดฅ") return False, f'ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒ็ญพๅˆฐๆŽฅๅฃ่ฏทๆฑ‚ๅคฑ่ดฅ' # ๅˆคๆ–ญๆ˜ฏๅฆ็ญพๅˆฐๆˆๅŠŸ sign_status = self.sign_in_result(html_res=sign_res.text, regexs=self._success_regex) if sign_status: self.info(f"็ญพๅˆฐๆˆๅŠŸ") if exits_answers and question: # ็ญพๅˆฐๆˆๅŠŸๅ†™ๅ…ฅๆœฌๅœฐๆ–‡ไปถ self.__write_local_answer(exits_answers=exits_answers or {}, question=question, answer=choice) return True, f'ใ€{site}ใ€‘็ญพๅˆฐๆˆๅŠŸ' else: sign_status = self.sign_in_result(html_res=sign_res.text, regexs=self._sign_regex) if sign_status: self.info(f"ไปŠๆ—ฅๅทฒ็ญพๅˆฐ") return True, f'ใ€{site}ใ€‘ไปŠๆ—ฅๅทฒ็ญพๅˆฐ' self.error(f"็ญพๅˆฐๅคฑ่ดฅ๏ผŒ่ฏทๅˆฐ้กต้ขๆŸฅ็œ‹") return False, f'ใ€{site}ใ€‘็ญพๅˆฐๅคฑ่ดฅ๏ผŒ่ฏทๅˆฐ้กต้ขๆŸฅ็œ‹' def __write_local_answer(self, exits_answers, question, answer): """ ็ญพๅˆฐๆˆๅŠŸๅ†™ๅ…ฅๆœฌๅœฐๆ–‡ไปถ """ try: exits_answers[question] = answer # ๅบๅˆ—ๅŒ–ๆ•ฐๆฎ formatted_data = json.dumps(exits_answers, indent=4) with open(self._answer_file, 'w') as f: f.write(formatted_data) except (FileNotFoundError, IOError, OSError) as e: self.debug("็ญพๅˆฐๆˆๅŠŸๅ†™ๅ…ฅๆœฌๅœฐๆ–‡ไปถๅคฑ่ดฅ")
[]
2024-01-10
czh513/RLs-for-TF2.0-Gym-Unity
algos~tf2algos~trpo.py
import rls import numpy as np import tensorflow as tf import tensorflow_probability as tfp from utils.tf2_utils import get_TensorSpecs, gaussian_clip_rsample, gaussian_likelihood_sum, gaussian_entropy from algos.tf2algos.base.on_policy import make_on_policy_class ''' Stole this from OpenAI SpinningUp. https://github.com/openai/spinningup/blob/master/spinup/algos/trpo/trpo.py ''' def flat_concat(xs): return tf.concat([tf.reshape(x, (-1,)) for x in xs], axis=0) def assign_params_from_flat(x, params): def flat_size(p): return int(np.prod(p.shape.as_list())) # the 'int' is important for scalars splits = tf.split(x, [flat_size(p) for p in params]) new_params = [tf.reshape(p_new, p.shape) for p, p_new in zip(params, splits)] return tf.group([p.assign(p_new) for p, p_new in zip(params, new_params)]) class TRPO(make_on_policy_class(mode='share')): ''' Trust Region Policy Optimization, https://arxiv.org/abs/1502.05477 ''' def __init__(self, s_dim, visual_sources, visual_resolution, a_dim, is_continuous, beta=1.0e-3, lr=5.0e-4, delta=0.01, lambda_=0.95, cg_iters=10, train_v_iters=10, damping_coeff=0.1, backtrack_iters=10, backtrack_coeff=0.8, epsilon=0.2, critic_lr=1e-3, hidden_units={ 'actor_continuous': [32, 32], 'actor_discrete': [32, 32], 'critic': [32, 32] }, **kwargs): super().__init__( s_dim=s_dim, visual_sources=visual_sources, visual_resolution=visual_resolution, a_dim=a_dim, is_continuous=is_continuous, **kwargs) self.beta = beta self.delta = delta self.lambda_ = lambda_ self.epsilon = epsilon self.cg_iters = cg_iters self.damping_coeff = damping_coeff self.backtrack_iters = backtrack_iters self.backtrack_coeff = backtrack_coeff self.train_v_iters = train_v_iters # self.actor_TensorSpecs = get_TensorSpecs([self.s_dim], self.visual_dim, [self.a_dim], [1], [1]) # self.critic_TensorSpecs = get_TensorSpecs([self.s_dim], self.visual_dim, [1]) if self.is_continuous: self.actor_net = rls.actor_mu(self.feat_dim, self.a_dim, hidden_units['actor_continuous']) self.log_std = tf.Variable(initial_value=-0.5 * np.ones(self.a_dim, dtype=np.float32), trainable=True) self.actor_tv = self.actor_net.trainable_variables + [self.log_std] # self.Hx_TensorSpecs = [tf.TensorSpec(shape=flat_concat(self.actor_tv).shape, dtype=tf.float32)] \ # + get_TensorSpecs([self.s_dim], self.visual_dim, [self.a_dim], [self.a_dim]) else: self.actor_net = rls.actor_discrete(self.feat_dim, self.a_dim, hidden_units['actor_discrete']) self.actor_tv = self.actor_net.trainable_variables # self.Hx_TensorSpecs = [tf.TensorSpec(shape=flat_concat(self.actor_tv).shape, dtype=tf.float32)] \ # + get_TensorSpecs([self.s_dim], self.visual_dim, [self.a_dim]) self.critic_net = rls.critic_v(self.feat_dim, hidden_units['critic']) self.critic_tv = self.critic_net.trainable_variables + self.other_tv self.critic_lr = self.init_lr(critic_lr) self.optimizer_critic = self.init_optimizer(self.critic_lr) self.model_recorder(dict( actor=self.actor_net, critic=self.critic_net, optimizer_critic=self.optimizer_critic )) if self.is_continuous: data_name_list = ['s', 'visual_s', 'a', 'r', 's_', 'visual_s_', 'done', 'value', 'log_prob', 'old_mu', 'old_log_std'] else: data_name_list = ['s', 'visual_s', 'a', 'r', 's_', 'visual_s_', 'done', 'value', 'log_prob', 'old_logp_all'] self.initialize_data_buffer( data_name_list=data_name_list) def show_logo(self): self.recorder.logger.info(''' ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€๏ฝ˜ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€๏ฝ˜ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜๏ฝ˜ใ€€ ''') def choose_action(self, s, visual_s, evaluation=False): a, _v, _lp, _morlpa, self.cell_state = self._get_action(s, visual_s, self.cell_state) a = a.numpy() self._value = np.squeeze(_v.numpy()) self._log_prob = np.squeeze(_lp.numpy()) + 1e-10 if self.is_continuous: self._mu = _morlpa.numpy() else: self._logp_all = _morlpa.numpy() return a @tf.function def _get_action(self, s, visual_s, cell_state): with tf.device(self.device): feat, cell_state = self.get_feature(s, visual_s, cell_state=cell_state, record_cs=True) value = self.critic_net(feat) if self.is_continuous: mu = self.actor_net(feat) sample_op, _ = gaussian_clip_rsample(mu, self.log_std) log_prob = gaussian_likelihood_sum(sample_op, mu, self.log_std) return sample_op, value, log_prob, mu, cell_state else: logits = self.actor_net(feat) logp_all = tf.nn.log_softmax(logits) norm_dist = tfp.distributions.Categorical(logits) sample_op = norm_dist.sample() log_prob = norm_dist.log_prob(sample_op) return sample_op, value, log_prob, logp_all, cell_state def store_data(self, s, visual_s, a, r, s_, visual_s_, done): assert isinstance(a, np.ndarray), "store_data need action type is np.ndarray" assert isinstance(r, np.ndarray), "store_data need reward type is np.ndarray" assert isinstance(done, np.ndarray), "store_data need done type is np.ndarray" self._running_average(s) if self.is_continuous: self.data.add(s, visual_s, a, r, s_, visual_s_, done, self._value, self._log_prob, self._mu, self.log_std.numpy()) else: self.data.add(s, visual_s, a, r, s_, visual_s_, done, self._value, self._log_prob, self._logp_all) @tf.function def _get_value(self, feat): with tf.device(self.device): value = self.critic_net(feat) return value def calculate_statistics(self): feat, self.cell_state = self.get_feature(self.data.last_s(), self.data.last_visual_s(), cell_state=self.cell_state, record_cs=True) init_value = np.squeeze(self._get_value(feat).numpy()) self.data.cal_dc_r(self.gamma, init_value) self.data.cal_td_error(self.gamma, init_value) self.data.cal_gae_adv(self.lambda_, self.gamma) def learn(self, **kwargs): self.train_step = kwargs.get('train_step') def _train(data, crsty_loss, cell_state): if self.is_continuous: s, visual_s, a, dc_r, old_log_prob, advantage, old_mu, old_log_std = data Hx_args = (s, visual_s, old_mu, old_log_std) else: s, visual_s, a, dc_r, old_log_prob, advantage, old_logp_all = data Hx_args = (s, visual_s, old_logp_all) actor_loss, entropy, gradients = self.train_actor( (s, visual_s, a, old_log_prob, advantage), cell_state ) x = self.cg(self.Hx, gradients.numpy(), Hx_args) x = tf.convert_to_tensor(x) alpha = np.sqrt(2 * self.delta / (np.dot(x, self.Hx(x, *Hx_args)) + 1e-8)) for i in range(self.backtrack_iters): assign_params_from_flat(alpha * x * (self.backtrack_coeff ** i), self.actor_tv) for _ in range(self.train_v_iters): critic_loss = self.train_critic( (s, visual_s, dc_r), crsty_loss, cell_state ) summaries = dict([ ['LOSS/actor_loss', actor_loss], ['LOSS/critic_loss', critic_loss], ['Statistics/entropy', entropy] ]) return summaries if self.is_continuous: train_data_list = ['s', 'visual_s', 'a', 'discounted_reward', 'log_prob', 'gae_adv', 'old_mu', 'old_log_std'] else: train_data_list = ['s', 'visual_s', 'a', 'discounted_reward', 'log_prob', 'gae_adv', 'old_logp_all'] self._learn(function_dict={ 'calculate_statistics': self.calculate_statistics, 'train_function': _train, 'train_data_list': train_data_list, 'summary_dict': dict([ ['LEARNING_RATE/critic_lr', self.critic_lr(self.train_step)] ]) }) @tf.function(experimental_relax_shapes=True) def train_actor(self, memories, cell_state): s, visual_s, a, old_log_prob, advantage = memories with tf.device(self.device): feat = self.get_feature(s, visual_s, cell_state=cell_state) with tf.GradientTape() as tape: if self.is_continuous: mu = self.actor_net(feat) new_log_prob = gaussian_likelihood_sum(a, mu, self.log_std) entropy = gaussian_entropy(self.log_std) else: logits = self.actor_net(feat) logp_all = tf.nn.log_softmax(logits) new_log_prob = tf.reduce_sum(a * logp_all, axis=1, keepdims=True) entropy = -tf.reduce_mean(tf.reduce_sum(tf.exp(logp_all) * logp_all, axis=1, keepdims=True)) ratio = tf.exp(new_log_prob - old_log_prob) actor_loss = -tf.reduce_mean(ratio * advantage) actor_grads = tape.gradient(actor_loss, self.actor_tv) gradients = flat_concat(actor_grads) self.global_step.assign_add(1) return actor_loss, entropy, gradients @tf.function(experimental_relax_shapes=True) def Hx(self, x, *args): if self.is_continuous: s, visual_s, old_mu, old_log_std = args else: s, visual_s, old_logp_all = args with tf.device(self.device): with tf.GradientTape(persistent=True) as tape: feat = self.get_feature(s, visual_s) if self.is_continuous: mu = self.actor_net(feat) var0, var1 = tf.exp(2 * self.log_std), tf.exp(2 * old_log_std) pre_sum = 0.5 * (((old_mu - mu)**2 + var0) / (var1 + 1e-8) - 1) + old_log_std - self.log_std all_kls = tf.reduce_sum(pre_sum, axis=1) kl = tf.reduce_mean(all_kls) else: logits = self.actor_net(feat) logp_all = tf.nn.log_softmax(logits) all_kls = tf.reduce_sum(tf.exp(old_logp_all) * (old_logp_all - logp_all), axis=1) kl = tf.reduce_mean(all_kls) g = flat_concat(tape.gradient(kl, self.actor_tv)) _g = tf.reduce_sum(g * x) hvp = flat_concat(tape.gradient(_g, self.actor_tv)) if self.damping_coeff > 0: hvp += self.damping_coeff * x return hvp @tf.function(experimental_relax_shapes=True) def train_critic(self, memories, crsty_loss, cell_state): s, visual_s, dc_r = memories with tf.device(self.device): with tf.GradientTape() as tape: feat = self.get_feature(s, visual_s, cell_state=cell_state) value = self.critic_net(feat) td_error = dc_r - value value_loss = tf.reduce_mean(tf.square(td_error)) + crsty_loss critic_grads = tape.gradient(value_loss, self.critic_tv) self.optimizer_critic.apply_gradients( zip(critic_grads, self.critic_tv) ) return value_loss def cg(self, Ax, b, args): """ Conjugate gradient algorithm (see https://en.wikipedia.org/wiki/Conjugate_gradient_method) """ x = np.zeros_like(b) r = b.copy() # Note: should be 'b - Ax(x)', but for x=0, Ax(x)=0. Change if doing warm start. p = r.copy() r_dot_old = np.dot(r, r) for _ in range(self.cg_iters): z = Ax(tf.convert_to_tensor(p), *args) alpha = r_dot_old / (np.dot(p, z) + 1e-8) x += alpha * p r -= alpha * z r_dot_new = np.dot(r, r) p = r + (r_dot_new / r_dot_old) * p r_dot_old = r_dot_new return x
[]
2024-01-10
navin20/AspectBasedSentimentAnalysis
utils~preprocess.py
import nltk import numpy as np import pandas as pd import spacy from nltk.corpus import stopwords from nltk.stem import PorterStemmer from nltk import pos_tag, word_tokenize import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel def preprocess(df): ''' Preprocess text from dataframe. Splits reviews into sentences, then each sentence is preprocessed. Returns dataframe with original and preprocessed text. ''' # Split into sentences nlp = spacy.load('en_core_web_sm') review_ids = [] text = [] # Assuming the reviews aren't split and that reviews columns are 'review', # for review_id, review in zip(df['review_id'], df['review']): # sentences= [i for i in nlp(review).sents] # for sentence in sentences: # review_ids.append(review_id) # text.append(str(sentence)) # No review IDs for review in df['review']: sentences= [i for i in nlp(review).sents] for sentence in sentences: # review_ids.append(review_id) text.append(str(sentence)) reviews_df = pd.DataFrame() # reviews_df['review_id'] = review_ids reviews_df['raw_text'] = text # Remove symbols,punctuations... reviews_df['clean_text'] = reviews_df['raw_text'].str.replace('[^\w\s]','') reviews_df['clean_text'] = reviews_df['clean_text'].str.replace('\d+', '') reviews_df['clean_text'] = reviews_df['clean_text'].str.lower() reviews_df['clean_text'] = reviews_df['clean_text'].str.replace('^https?:\/\/.*[\r\n]*', '') reviews_df['clean_text'].replace('', np.nan, inplace=True) drop = reviews_df[pd.isnull(reviews_df['clean_text'])].index reviews_df.drop(drop , inplace=True) reviews_df = reviews_df.reset_index(drop = True) def preprocess_aspect(df): ''' Preprocessing text for aspect extraction and classification. Returns tf-idf/corpus, LDA model. ''' def sent_to_words(sentences): for sentence in sentences: yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) # deacc=True removes punctuations data_words = list(sent_to_words(df['clean_text'])) # Build the bigram and trigram models bigram = gensim.models.Phrases(data_words, min_count=5, threshold=100) # higher threshold fewer phrases. trigram = gensim.models.Phrases(bigram[data_words], threshold=100) # Faster way to get a sentence clubbed as a trigram/bigram bigram_mod = gensim.models.phrases.Phraser(bigram) trigram_mod = gensim.models.phrases.Phraser(trigram) stop_words = stopwords.words('english') stop_words.extend(['from', 'subject', 're', 'edu', 'use']) # Define functions for stopwords, bigrams, trigrams and lemmatization def remove_stopwords(texts): return [[word for word in simple_preprocess(str(doc)) if word not in stop_words] for doc in texts] def make_bigrams(texts): return [bigram_mod[doc] for doc in texts] def make_trigrams(texts): return [trigram_mod[bigram_mod[doc]] for doc in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out # Remove Stop Words data_words_nostops = remove_stopwords(data_words) # Form Bigrams data_words_bigrams = make_bigrams(data_words_nostops) # Initialize spacy 'en' model, keeping only tagger component (for efficiency) # python3 -m spacy download en nlp = spacy.load('en', disable=['parser', 'ner']) # Do lemmatization keeping only noun, adj, vb, adv data_lemmatized = lemmatization(data_words_bigrams, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']) # Create Dictionary id2word = corpora.Dictionary(data_lemmatized) # Create Corpus texts = data_lemmatized # Term Document Frequency corpus = [id2word.doc2bow(text) for text in texts] return corpus, id2word porter = PorterStemmer() def stemSentence(sentence): token_words=word_tokenize(sentence) token_words stem_sentence=[] for word in token_words: stem_sentence.append(porter.stem(word)) stem_sentence.append(" ") return "".join(stem_sentence) stemmed = [] for sentence in reviews_df['clean_text']: stemmed.append(stemSentence(sentence)) reviews_df['stem_text'] = stemmed # corpus, id2word = preprocess_aspect(reviews_df) # Remove stop words # stop_words = set(stopwords.words('english')) # reviews_df['no_sw'] = reviews_df['clean_text'][:].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop_words)])) return reviews_df #, corpus, id2word
[]
2024-01-10
navin20/AspectBasedSentimentAnalysis
utils~sentiment_classifiers.py
import pandas as pd import numpy as np import joblib import timeit from tensorflow.keras.models import load_model from tensorflow.keras.models import model_from_json from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from utils.preprocess import preprocess from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel class Asp_Sentiment_classifiers: """ Loads all models provides functions to access them. Receives dataframe and performs predictions on them. """ def __init__(self): ''' Loads all models on startup once. ''' # Load aspect model # lda = gensim.models.LdaModel.load(' models/lda.model') # Load sentiment models # Staff # json_file = open("models/staff_sentiment_model.json", 'r') # loaded_model_json = json_file.read() # json_file.close() # staff_model = model_from_json(loaded_model_json) # staff_model.load_weights("models/staff_sentiment_model.h5") # # Amenities # json_file = open("models/amenities_sentiment_model.json", 'r') # loaded_model_json = json_file.read() # json_file.close() # amenities_model = model_from_json(loaded_model_json) # amenities_model.load_weights("models/amenities_sentiment_model.h5") # # Condition # json_file = open("models/condition_sentiment_model.json", 'r') # loaded_model_json = json_file.read() # json_file.close() # condition_model = model_from_json(loaded_model_json) # condition_model.load_weights("models/condition_sentiment_model.h5") # # Cleanliness # json_file = open("models/cleanliness_sentiment_model.json", 'r') # loaded_model_json = json_file.read() # json_file.close() # cleanliness_model = model_from_json(loaded_model_json) # cleanliness_model.load_weights("models/cleanliness_sentiment_model.h5") # Overall json_file = open("models/sentiment_model2.json", 'r') loaded_model_json = json_file.read() json_file.close() overall_model = model_from_json(loaded_model_json) overall_model.load_weights("models/sentiment_model2.h5") # Aspect # lda_model = gensim.models.LdaModel.load('models/lda.model') json_file = open("models/aspect_model2.json", 'r') loaded_model_json = json_file.read() json_file.close() aspect_model = model_from_json(loaded_model_json) aspect_model.load_weights("models/aspect_model2.h5") tokenizer = Tokenizer(num_words=5000) tokenizer = joblib.load("models/tokenizer2.pkl") # self.staff_model = staff_model # self.amenities_model = amenities_model # self.condition_model = condition_model # self.cleanliness_model = cleanliness_model self.overall_model = overall_model self.aspect_model = aspect_model # self.aspect_model = lda_model self.tokenizer = tokenizer # def get_staff_model(self): # return self.staff_model # def get_amenities_model(self): # return self.amenities_model # def get_condition_model(self): # return self.amenities_model # def get_cleanliness_model(self): # return self.cleanliness_model # def get_overall_model(self): # return self.overall_model # def get_aspect_model(self): # return self.aspect_model # def aspects(self, df, corpus): # """ # Classifies each sentence by aspect(s). # Returns dataframe with additional columns for each aspect. # """ # lda_model = self.aspect_model # corpus = corpus # def format_topics_sentences(ldamodel=lda_model, corpus=corpus, texts=df['clean_text'].tolist(), review_id=df['review_id']): # # Init output # sent_topics_df = pd.DataFrame() # # Get main topic in each document # for i, row in enumerate(ldamodel[corpus]): # row = sorted(row[0], key=lambda x: (x[1]), reverse=True) # # row = sorted(row, key=lambda x: (x[1]), reverse=True) # old line # # Get the Dominant topic, Perc Contribution and Keywords for each document # for j, (topic_num, prop_topic) in enumerate(row): # if j == 0: # => dominant topic # wp = ldamodel.show_topic(topic_num) # topic_keywords = ", ".join([word for word, prop in wp]) # sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), round(prop_topic,4), topic_keywords]), ignore_index=True) # else: # break # sent_topics_df.columns = ['Dominant_Topic', 'Perc_Contribution', 'Topic_Keywords'] # # Add original text to the end of the output # review = pd.Series(review_id) # contents = pd.Series(texts) # sent_topics_df = pd.concat([review, sent_topics_df, contents], axis=1) # return(sent_topics_df) # df_topic_sents_keywords = format_topics_sentences(ldamodel=lda_model, corpus=corpus, texts=df['clean_text'].tolist(), review_id=df['review_id']) # # Format # df_dominant_topic = df_topic_sents_keywords.reset_index() # df_dominant_topic.columns = ['Sentence_No', 'review_id', 'Dominant_Topic', 'Topic_Perc_Contrib', 'Keywords', 'Text'] # def classify(df_dominant_topic, df): # output_df = df # pass # return output_df def sentiments(self, reviews_df): """ After classifying aspects, classify overall sentiments and then sentiments per aspect for all reviews/sentences. Returns dataframe """ tokenizer = self.tokenizer # Vectorize/tokenize sentence with vocabulary maxlen=100 X_data = tokenizer.texts_to_sequences(reviews_df['stem_text']) X_data = pad_sequences(X_data, padding='post', maxlen=maxlen) # Overall Sentiment Predictions # y_pred = self.overall_model.predict(X_data, batch_size=5, verbose=1) # y_pred_bool = np.argmax(y_pred, axis=1) # predictions = [] # for pred in y_pred_bool: # if pred == 0: # predictions.append('positive') # elif pred == 1: # predictions.append('neutral') # else: # predictions.append('negative') # reviews_df['overall_sentiment'] = predictions # Sentiments per Aspect def get_labels(y_pred): if y_pred == 0: return 'positive' elif y_pred == 1: return 'neutral' else: return 'negative' reviews_df['overall_sentiment'] = None reviews_df['staff_sent'] = None reviews_df['amenities_sent'] = None reviews_df['condition_sent'] = None reviews_df['clean_sent'] = None print('Running... Please wait...') for i, aspect in zip(range(len(X_data)), reviews_df['aspect']): y_pred = -1 if "a1" in aspect: y_pred = self.overall_model.predict(X_data[i:i+1], batch_size=5, verbose=0) y_pred_bool = np.argmax(y_pred, axis=1) reviews_df['staff_sent'][i] = get_labels(y_pred_bool) reviews_df['overall_sentiment'][i] = get_labels(y_pred_bool) elif "a2" in aspect: y_pred = self.overall_model.predict(X_data[i:i+1], batch_size=5, verbose=0) y_pred_bool = np.argmax(y_pred, axis=1) reviews_df['amenities_sent'][i] = get_labels(y_pred_bool) reviews_df['overall_sentiment'][i] = get_labels(y_pred_bool) elif "a3" in aspect: y_pred = self.overall_model.predict(X_data[i:i+1], batch_size=5, verbose=0) y_pred_bool = np.argmax(y_pred, axis=1) reviews_df['condition_sent'][i] = get_labels(y_pred_bool) reviews_df['overall_sentiment'][i] = get_labels(y_pred_bool) elif "a4" in aspect: y_pred = self.overall_model.predict(X_data[i:i+1], batch_size=5, verbose=0) y_pred_bool = np.argmax(y_pred, axis=1) reviews_df['clean_sent'][i] = get_labels(y_pred_bool) reviews_df['overall_sentiment'][i] = get_labels(y_pred_bool) else: y_pred = self.overall_model.predict(X_data[i:i+1], batch_size=5, verbose=1) y_pred_bool = np.argmax(y_pred, axis=1) reviews_df['overall_sentiment'][i] = get_labels(y_pred_bool) # else: # y_pred = self.overall_model.predict(X_data, batch_size=5, verbose=1) return reviews_df def aspect_classifier(self, reviews_df, threshold=0.3): """ Classifies according to aspect. Returns dataframe with aspect per sentence. """ tokenizer = self.tokenizer # Vectorize/tokenize sentence with vocabulary maxlen=100 X_data = tokenizer.texts_to_sequences(reviews_df['stem_text']) X_data = pad_sequences(X_data, padding='post', maxlen=maxlen) y_pred = self.aspect_model.predict(X_data, batch_size=5, verbose=1) y_pred_bool = y_pred # Threshold *requires tuning* y_pred_bool[y_pred_bool>=threshold] = 1 y_pred_bool[y_pred_bool<threshold] = 0 predictions = [[] for _ in range(len(y_pred_bool))] for index, values in zip(range(len(y_pred_bool)), y_pred_bool): if values[0] == 1: predictions[index].append('a1') if values[1] == 1: predictions[index].append('a2') if values[2] == 1: predictions[index].append('a3') if values[3] == 1: predictions[index].append('a4') reviews_df['aspect'] = predictions return reviews_df
[]
2024-01-10
AdieLaine/Lnu-AI-Chat
lnu-ai-chat.py
# System-related import os import tempfile import time # File and IO operations import json import soundfile as sf # Data processing and manipulation import pandas as pd import random # Image processing from PIL import Image # Text-to-speech from gtts import gTTS from pydub import AudioSegment # Type hints from typing import Dict, List, Tuple, Optional # Language model import openai # Web application framework import streamlit as st from streamlit_option_menu import option_menu # Machine learning metrics and preprocessing from sklearn.metrics.pairwise import cosine_similarity from sklearn.preprocessing import normalize from scipy.spatial.distance import cosine # Environment variables from dotenv import load_dotenv CUSTOM_CSS = """ <style> .big-font { font-size: 20px; font-weight: bold; } .red-text { color: crimson; } .green-text { color: #42f5ad; } .blue-text { color: #4287f5; } .selected-word { font-weight: bold; color: #f542f7; } .yellow-text { color: #FFD700; } .custom-option-menu select { font-weight: bold; color: #FF6347; background-color: #FFF; } </style> """ # Streamlit configurations st.set_page_config( page_title="Lnu-AI Chat - An Indigenous AI System", page_icon="๐Ÿชถ", layout="wide", initial_sidebar_state="auto", menu_items={ 'Get Help': 'https://github.com/AdieLaine/Lnu-AI-Chat', 'Report a bug': 'https://github.com/AdieLaine/Lnu-AI-Chat/issues', 'About': """ # Lnu-AI Welcome to Lnu-AI Chat! This application is dedicated to helping people learn and appreciate the Mi'kmaq language, an indigenous language of Eastern Canada and the United States. ## About Mi'kmaq Language The Mi'kmaq language is a rich, polysynthetic language with a deep historical and cultural significance. It is, however, at risk of being lost as the number of fluent speakers decreases. ## The Lnu-AI Project Lnu-AI utilizes advanced AI technologies to provide a platform for learning, using, and preserving the Mi'kmaq language. It offers various features like chat functionality, storytelling, and deep linguistic analysis to facilitate language learning and appreciation. ## Your Contribution As an open-source project, we welcome contributions that help improve Lnu-AI and further its mission to preserve the Mi'kmaq language. Please visit our [GitHub](https://github.com/AdieLaine/Lnu-AI-Chat) page for more information. Enjoy your journey with the Mi'kmaq language! """ } ) @st.cache_data def load_all_word_details(file): """ Load word details from a JSON file. Parameters: file (str): Path to the JSON file containing the word details. Returns: all_word_details (dict): Dictionary of word details loaded from the JSON file. """ full_path = os.path.join("data", file) if os.path.isfile(full_path): with open(full_path, 'r') as f: all_word_details = json.load(f) return all_word_details else: return None file = 'all_word_details.json' all_word_details: dict = load_all_word_details(file) @st.cache_data def load_trained_data(file_paths): """ Load trained data from multiple files. Handles both formats. Parameters: file_paths (list): List of paths to the trained data files. Returns: trained_data (list): List of trained data. """ trained_data: list = [] # Prioritize 'trained_data' in the list sorted_file_paths = sorted(file_paths, key=lambda path: "trained_data" != path) for file_path in sorted_file_paths: full_path = os.path.join("data", file_path) if os.path.isfile(full_path): with open(full_path, 'r') as f: trained_data.extend([json.loads(line) for line in f]) if all('prompt' in data and 'completion' in data for data in trained_data): return trained_data else: return None file_paths = ['trained_data.jsonl'] trained_data: list = load_trained_data(file_paths) @st.cache_data def load_word_data(file_path): """ Load Lnu-AI Dictionary Word Data from a JSON file. Parameters: file_path (str): Path to the JSON file containing the Lnu-AI Dictionary Word Data. Returns: word_data (dict): Dictionary of Lnu-AI Dictionary Word Data loaded from the JSON file. """ data_path = os.path.join("data", file_path) try: with open(data_path, 'r') as file: word_data = json.load(file) # Load and return the data return word_data except FileNotFoundError: # Handle file not found error st.error(f"File {file_path} not found.") return None except json.JSONDecodeError: # Handle JSON decode error st.error(f"Error decoding JSON file {file_path}.") return None @st.cache_data def load_trained_data_embeddings(file_path): """ Load trained data embeddings from a JSON file. Parameters: file_path (str): Path to the JSON file containing the trained data embeddings. Returns: trained_data_embeddings (dict): Dictionary of trained data embeddings loaded from the JSON file. """ full_path = os.path.join("data", file_path) if os.path.isfile(full_path): with open(full_path, 'r') as file: trained_data_embeddings = json.load(file) return trained_data_embeddings else: return None file_path = 'trained_data_embeddings.json' trained_data_embeddings: dict = load_trained_data_embeddings(file_path) @st.cache_data def load_word_details_embeddings(file): """ Load word embeddings from a JSON file. Parameters: file (str): Path to the JSON file containing the word embeddings. Returns: word_details_embeddings (dict): Dictionary of word embeddings loaded from the JSON file. """ full_path = os.path.join("data", file) if os.path.isfile(full_path): with open(full_path, 'r') as f: try: word_details_embeddings = json.load(f) return word_details_embeddings except json.JSONDecodeError: # Handle JSON decoding error return None else: # Handle file not found return None file = 'word_details_embeddings.json' word_details_embeddings: dict = load_word_details_embeddings(file) def get_env_variable(name, default=None): """ Returns the value of the environment variable with the given name. First tries to fetch it from Streamlit secrets, and if not available, falls back to the local environment. If it's not found in either place, returns the default value if provided. Args: name (str): The name of the environment variable. default (str, optional): The default value to be returned in case the environment variable is not found. Returns: str: The value of the environment variable, or the default value. """ if st.secrets is not None and name in st.secrets: # Fetch the secret from Streamlit secrets return st.secrets[name] else: # Try to get the secret from the local environment value = os.getenv(name) if value is None and default is not None: # If the environment variable is not found and a default value is provided, # return the default value return default else: return value @st.cache_resource(show_spinner=False) def load_env_variables_and_data(): """ Loads Lnu-AI Assistant environment variables and data. Returns: dict: A dictionary containing the loaded environment variables and data. """ api_keys, tts_settings, local_data_files, models = load_env_variables() file_paths = ['trained_data.jsonl'] trained_data = load_trained_data(file_paths) all_word_details = load_all_word_details(local_data_files.get("all_word_details")) trained_data_embeddings = load_trained_data_embeddings(local_data_files.get("trained_data_embeddings")) word_details_embeddings = load_word_details_embeddings(local_data_files.get("word_details_embeddings")) return { "api_keys": api_keys, "tts_settings": tts_settings, "local_data_files": local_data_files, "models": models, "completion_model": trained_data, "all_word_details": all_word_details, "trained_data": trained_data, "trained_data_embeddings": trained_data_embeddings, "word_details_embeddings": word_details_embeddings, "CUSTOM_CSS": CUSTOM_CSS } @st.cache_resource(show_spinner=False) def load_env_variables(): """ Load all the environment variables required for the Lnu-AI Assistant. Returns: Tuple: A tuple containing the loaded environment variables. """ load_dotenv() api_keys = { "openai": get_env_variable("OPENAI_API_KEY"), } tts_settings = { "tts_audio": get_env_variable('TTS_AUDIO'), "local_tts": get_env_variable('LOCAL_TTS'), } local_data_files = { "trained_data": (os.getenv("TRAINED_DATA") + '.jsonl') if os.getenv("TRAINED_DATA") else None, "all_word_details": (os.getenv("ALL_WORD_DETAILS") + '.json') if os.getenv("ALL_WORD_DETAILS") else None, "trained_data_embeddings": (os.getenv("TRAINED_DATA_EMBEDDINGS") + '.json') if os.getenv("TRAINED_DATA_EMBEDDINGS") else None, "word_details_embeddings": (os.getenv("WORD_DETAILS_EMBEDDINGS") + '.json') if os.getenv("WORD_DETAILS_EMBEDDINGS") else None, } models = { "chat_model": os.getenv("CHAT_MODEL_SELECTION", default="gpt-3.5-turbo-0613"), "completion_model": os.getenv("COMPLETION_MODEL_SELECTION", default="gpt-3.5-turbo-0613"), "fine_tuned_model_dictionary": os.getenv("FINE_TUNED_MODEL_DICTIONARY"), "fine_tuned_model_data": os.getenv("FINE_TUNED_MODEL_DATA") } openai.api_key = api_keys["openai"] return api_keys, tts_settings, local_data_files, models def generate_audio(text, tts_service): """ Generate audio from text using a TTS service. Parameters: text (str): The input text. tts_service (str): The TTS service to use (e.g., "gtts", "st.audio"). Returns: str: Path to the generated audio file. """ tts_service = tts_service.lower() # convert to lower case for case-insensitive comparison if tts_service == 'gtts': tts = gTTS(text=text, lang='en') # Replace 'en' with the appropriate language if needed # Save the speech audio into a temporary mp3 file temp_mp3 = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") tts.save(temp_mp3.name) # Convert the temporary mp3 file to wav format audio = AudioSegment.from_mp3(temp_mp3.name) temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") audio.export(temp_wav.name, format='wav') # Return the path to the generated wav file return temp_wav.name elif tts_service == 'st.audio': return text else: raise ValueError(f"Invalid Text-to-Speech service: {tts_service}") @st.cache_data def clean_reply(reply): """ Cleans the assistant's reply by removing trailing whitespaces and an extra period at the end, as well as unwanted "user" or "assistant" at the beginning. Parameters: reply (str): Reply from the assistant. Returns: str: Cleaned reply. """ # Check if the reply starts with 'user:' or 'assistant:', and remove it if it does if reply.startswith('user:') or reply.startswith('assistant:'): reply = reply.split(':', 1)[1] # Split the reply into lines lines = reply.split('\n\n') # Remove trailing whitespace from the last line last_line = lines[-1].rstrip() # Check if the last line ends with '.' and remove it if it does if last_line.endswith("'""."): last_line = last_line[:-1] # Update the last line in the lines list lines[-1] = last_line # Join the lines back together cleaned_reply = '\n'.join(lines) return cleaned_reply.strip() # Added strip() to remove leading/trailing whitespace @st.cache_data(show_spinner=False) def find_most_similar_word(input_word, word_details_embeddings: dict): """ Find the most similar word to the input word based on the cosine similarity of their embeddings. Parameters: input_word (str): The input word. word_details_embeddings (dict): Dictionary of word embeddings. Returns: (str): The most similar word to the input word, or the input word itself if no embedding is found for it. """ input_embedding = word_details_embeddings.get(input_word) if input_embedding is not None: similarities = {word: 1 - cosine(input_embedding, embedding) for word, embedding in word_details_embeddings.items() if word != input_word} most_similar_word = max(similarities, key=similarities.get) return most_similar_word return input_word # If we don't have an embedding for the input word, just return the input word itself @st.cache_data(show_spinner=False) def replace_unknown_words(user_message, word_details_embeddings: dict): """ Replace unknown words in a message with the most similar known words. Parameters: user_message (str): The user's message. word_details_embeddings (dict): Dictionary of word embeddings. Returns: (str): The user's message with unknown words replaced with the most similar known words. """ words = user_message.split() known_words = word_details_embeddings.keys() new_words = [word if word in known_words else find_most_similar_word(word, word_details_embeddings) for word in words] return ' '.join(new_words) @st.cache_resource(show_spinner=False) def enhance_with_gpt(prompt, final_reply, models): """ Enhances the reply with GPT model by sending the conversation for completion. Args: prompt (str): User's message. final_reply (str): Assistant's reply. models (dict): Dictionary of trained models. Returns: str: Enhanced reply. """ model_name = models.get("chat_model", "gpt-4-0613") try: gpt_messages = [ {"role": "system", "content": get_system_message()}, # Provides assistant directives {"role": "user", "content": prompt}, {"role": "assistant", "content": final_reply} ] response = openai.ChatCompletion.create( model=model_name, messages=gpt_messages, max_tokens=1200, temperature=0.4, top_p=1.0, frequency_penalty=0.5, presence_penalty=0.5, ) # Extract reply from response if 'choices' in response and len(response['choices']) > 0: if 'message' in response['choices'][0] and 'content' in response['choices'][0]['message']: gpt_reply = response['choices'][0]['message']['content'] gpt_reply = clean_reply(gpt_reply) return gpt_reply except Exception as e: # Log the error for later debugging log_error(f"Error with {model_name}: {e}") raise e # Rethrow the exception after logging # If there was no error but also no 'choices' from OpenAI return "I didn't quite get that. Could you rephrase or provide more details, please?" def get_system_message(): # Update this to generate a system message dynamically based on your use case. return "You are Lnu-AI, an AI system developed to promote and preserve Mi'kmaq language and culture." def log_error(message): # Update this to log the error message in a way that's appropriate for your application. print(message) fallback_responses = [ #The use of varied responses allows the assistant to provide a more natural conversation experience. "I'm having a bit of trouble understanding your request. Could you please provide more details or try phrasing it differently?", "Could you please clarify your question?", "It seems I'm unable to assist with your query. Could you ask in a different way?", "I'm currently having difficulty comprehending that. Could you offer more context or details?", ] @st.cache_data(show_spinner=True) def lnu_ai_chat(prompt: str, trained_data: list, models: dict, word_details_embeddings: dict) -> str: """ Generates a response for a given user prompt using trained data or a language model. Ensures that unknown words in the user's message are replaced with the most similar known words. Args: prompt (str): The message inputted by the user. trained_data (list): A list of dictionaries containing prompts and their corresponding completions from training data. models (dict): A dictionary of trained models for the chat bot. word_details_embeddings (dict): A dictionary of word embeddings for known words. Returns: str: The generated response from the chat bot. """ # Replace unknown words in the user's prompt prompt = replace_unknown_words(prompt.lower(), word_details_embeddings) # Check for matches in trained data matched_prompt_data = [data for data in trained_data if prompt == data['prompt'].lower()] if matched_prompt_data: fine_tuned_replies = [data['completion'] for data in matched_prompt_data] reply = '\n\n'.join(fine_tuned_replies) reply = clean_reply(reply) # Enhance the reply using GPT reply = enhance_with_gpt(prompt, reply, models) # Add the new reply to the trained_data if reply not in fine_tuned_replies: new_prompt_data = {'prompt': prompt, 'completion': reply} trained_data.append(new_prompt_data) return reply # If no match found in trained data, generate completion with the trained models, enhanced as a fine-tuned model. model_to_use = models["fine_tuned_model_data"] if "fine_tuned_model_data" in models else models.get("completion_model", "curie:ft-personal-2023-05-16-05-11-43") response = openai.Completion.create( model=model_to_use, prompt=prompt, max_tokens=1200, temperature=0.4, ) if 'choices' in response and len(response['choices']) > 0: if 'text' in response['choices'][0]: final_reply = response['choices'][0]['text'] final_reply = clean_reply(final_reply) # Enhance the reply using GPT final_reply = enhance_with_gpt(prompt, final_reply, models) # Prompt-reply pair compared to the trained_data new_prompt_data = {'prompt': prompt, 'completion': final_reply} trained_data.append(new_prompt_data) return final_reply # Default response if no response is generated return random.choice(fallback_responses) def display_detail(label, value): """ Helper function to display a single detail of a word. Args: label (str): Label for the detail. value (str): Value of the detail. """ if value is None or value == "N/A": value = "Not available" st.markdown(f"<div style='color:CornflowerBlue;'>{label}: </div>{value}", unsafe_allow_html=True) @st.cache_data(show_spinner=False) def display_word_details_chatbot(selected_word, all_word_details: dict): """ Displays details of a selected word in the chatbot interface. Parameters: selected_word (str): Selected word. all_word_detail (dict): Details of all words. """ if selected_word not in all_word_details: st.error(f"The word {selected_word} does not exist in the dictionary.") return word_detail = all_word_details[selected_word] word = word_detail.get("word", "N/A") pronunciation = word_detail.get("pronunciation", "N/A") part_of_speech = word_detail.get("part_of_speech", "N/A") translation = word_detail.get("translation", "N/A") meanings = word_detail.get("meanings", ["N/A"]) example = word_detail.get("example", ["N/A"]) alternate_forms = word_detail.get("alternate_forms", ["N/A"]) # Display the word details using the helper function display_detail('Word', word) display_detail('Pronunciation', pronunciation) display_detail('Part of Speech', part_of_speech) display_detail('Translation', translation) def chatbot_application(models, trained_data, tts_settings): """ The main chatbot application function. This function handles loading previous session states or initializes new ones, sets up the sidebar and the main chat area, and processes user input and chatbot responses. Parameters: models (dict): A dictionary of the trained models. trained_data (dict): A dictionary of the trained data. tts_settings (dict): Text-to-speech settings. """ # Load previous session states or initialize new ones st.session_state.setdefault('past', []) st.session_state.setdefault('generated', []) # Add custom CSS for the image and title st.markdown(""" <style> .explore-words-title { font-size: 24px; text-align: center; } </style> """, unsafe_allow_html=True) # Define file path for image and load it image_path = os.path.join(os.getcwd(), "images", "chatside.png") image = Image.open(image_path) # Set up sidebar st.sidebar.image(image, use_column_width="auto", clamp=True, channels="RGB", output_format="png") st.sidebar.markdown("---") topics = [ "The Tale of Glooscap", "The Mi'kmaq Creation Story", "Tell me a Story", "Discuss Mi'kmaq Linguistics", "Explore Mi'kmaq History", "Culture and Traditions", "Let's Make a Canoe", "Craft a Sweetgrass Basket", "Create a Streamlit App", "Define Lnu-AI Features", ] sidebar_text = "\n- ".join(topics) sidebar_text = f"**Ask me about:**\n\n- {sidebar_text}\n\n**Note:** Audio responses will display here for each topic." st.sidebar.info(sidebar_text) # Instruction text st.markdown(""" <div style="display: flex; flex-direction: column; align-items: center; text-align: center;"> <div class="big-font"> <span style="font-weight:bold; font-size:24px; color:CornflowerBlue;">Talk Together</span> <span style="color:white;">:</span> <span style="font-size:24px; color:crimson;">Mawagnutmajig</span> </div> <ul style="text-align: left;"> <li>Iโ€™m Lnu-AI, your Indigenous AI Assistant.</li> <li>I am still learning and wonโ€™t always get it right, but our conversations will help me improve.</li> <li>The sharing of words preserves and transmits knowledge, values, beliefs, and history from one generation to the next.</li> </ul> </div> """, unsafe_allow_html=True) st.divider() # Placeholder for the audio player in the sidebar audio_placeholder = st.sidebar.empty() # Chat form for user input user_message = st.chat_input("Chat with Lnu-AI") if user_message: user_message = user_message.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") st.session_state['past'].append(user_message) with st.spinner("Referring to my Mi'kmaq Corpus ..."): try: # Exception handling added here chat_response = lnu_ai_chat(user_message, trained_data, models, all_word_details) # Generate audio response tts_service = tts_settings.get('tts_audio', 'gtts') audio_file = generate_audio(chat_response, tts_service) audio_response = open(audio_file, "rb") st.session_state['audio'] = audio_response.read() os.remove(audio_file) except Exception as e: # If error occurs, set response as error message chat_response = f"Sorry, an error occurred while processing your message: {str(e)}" finally: # Always append the response, even if an error occurred st.session_state['generated'].append(chat_response) # Display the audio player in the sidebar if 'audio' in st.session_state: audio_placeholder.audio(st.session_state['audio'], format="audio/wav") # Define avatar image path assistant_image_path = os.path.join(os.getcwd(), "images", "lnuavatar.png") image = Image.open(image_path) # Display the chat history for i in range(len(st.session_state['past'])): user_message = st.session_state['past'][i] chat_response = st.session_state['generated'][i] if chat_response: # Only display non-empty messages with st.chat_message("User"): st.write(user_message) with st.chat_message("Lnu-AI", avatar=assistant_image_path): st.write(chat_response) def main(): """ The main function of the application. """ global_vars = load_env_variables_and_data() # You can use the global_vars dictionary to access the required values. api_keys = global_vars['api_keys'] tts_settings = global_vars['tts_settings'] local_data_files = global_vars['local_data_files'] models = global_vars['models'] all_word_details = global_vars['all_word_details'] render_ui(CUSTOM_CSS) menu_options = { "Lnu-AI Chat": lambda: chatbot_application(models, trained_data, tts_settings), } selected_option = render_menu(list(menu_options.keys())) if selected_option and menu_options.get(selected_option): menu_options[selected_option]() @st.cache_data def render_ui(CUSTOM_CSS): """ Renders the user interface components. """ st.markdown(CUSTOM_CSS, unsafe_allow_html=True) st.markdown('<h1 style="text-align: center; color: Crimson; margin-top: -70px;">Lnu-AI Chat</h1>', unsafe_allow_html=True) st.markdown('<h3 style="text-align: center;">An Indigenous AI System</h3>', unsafe_allow_html=True) st.divider() def render_menu(options: list) -> str: """ Renders the menu with options. Args: options (list): A list of options to be displayed in the menu. Returns: str: The selected option from the menu. """ icons = ["chat"] return option_menu(None, options, icons=icons, menu_icon="cast", default_index=0, orientation="horizontal") if __name__ == "__main__": main() #wantaqo'ti
[ "{'prompt': PLACEHOLDER, 'completion': PLACEHOLDER}" ]
2024-01-10
caretech-owl/Llama-2-Open-Source-LLM-CPU-Inference
team_red~qa~qa_service.py
import logging from os import unlink from pathlib import Path from string import Formatter from tempfile import NamedTemporaryFile from typing import TYPE_CHECKING, Iterable, List, Optional, Protocol from langchain.chains.retrieval_qa.base import BaseRetrievalQA from langchain.docstore.document import Document from langchain.document_loaders import PyPDFLoader, TextLoader from langchain.embeddings import HuggingFaceEmbeddings from langchain.prompts import PromptTemplate from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from team_red.llm import build_llm from team_red.models.qa import QAConfig from team_red.transport import ( DocumentSource, FileTypes, PromptConfig, QAAnswer, QAFileUpload, QAQuestion, ) from team_red.utils import build_retrieval_qa if TYPE_CHECKING: from langchain.document_loaders.base import BaseLoader _LOGGER = logging.getLogger(__name__) _LOGGER.addHandler(logging.NullHandler()) class VectorStore(Protocol): def merge_from(self, vector_store: "VectorStore") -> None: pass def save_local(self, path: str) -> None: pass def add_texts( self, texts: Iterable[str], ) -> List[str]: pass def search(self, query: str, search_type: str, k: int) -> List[Document]: pass class QAService: def __init__(self, config: QAConfig) -> None: self._config = config self._llm = build_llm(config.model) self._embeddings = HuggingFaceEmbeddings( model_name=self._config.embedding.model.name, model_kwargs={"device": self._config.device}, ) self._vectorstore: Optional[VectorStore] = None self._database: Optional[BaseRetrievalQA] = None self._fact_checker_db: Optional[BaseRetrievalQA] = None if ( config.embedding.db_path and Path(config.embedding.db_path, "index.faiss").exists() ): _LOGGER.info( "Load existing vector store from '%s'.", config.embedding.db_path ) self._vectorstore = FAISS.load_local( config.embedding.db_path, self._embeddings ) def db_query(self, question: QAQuestion) -> List[DocumentSource]: if not self._vectorstore: return [] return [ DocumentSource( content=doc.page_content, name=doc.metadata.get("source", "unknown"), page=doc.metadata.get("page", 1), ) for doc in self._vectorstore.search( question.question, search_type=question.search_strategy, k=question.max_sources, ) ] def query(self, question: QAQuestion) -> QAAnswer: if not self._database: if not self._vectorstore: msg = "No vector store initialized! Upload documents first." _LOGGER.error(msg) return QAAnswer(status=404, error_msg=msg) self._database = self._setup_dbqa(self._config.model.prompt) response = self._database({"query": question.question}) answer = QAAnswer(answer=response["result"]) if self._config.features.return_source: for doc in response.get("source_documents", []): answer.sources.append( DocumentSource( content=doc.page_content, name=doc.metadata.get("source", "unknown"), page=doc.metadata.get("page", 1), ) ) if self._config.features.fact_checking.enabled is True: if not self._fact_checker_db: self._fact_checker_db = self._setup_dbqa_fact_checking( self._config.features.fact_checking.model.prompt ) response = self._fact_checker_db({"query": response["result"]}) for doc in response.get("source_documents", []): answer.sources.append( DocumentSource( content=doc.page_content, name=doc.metadata.get("source", "unknown"), page=doc.metadata.get("page", 1), ) ) _LOGGER.debug("\n==== Answer ====\n\n%s\n===============", answer) return answer def set_prompt(self, config: PromptConfig) -> PromptConfig: self._config.model.prompt = config self._database = self._setup_dbqa(self._config.model.prompt) return self._config.model.prompt def get_prompt(self) -> PromptConfig: return self._config.model.prompt def add_file(self, file: QAFileUpload) -> QAAnswer: documents: Optional[List[Document]] = None file_path = Path(file.name) try: f = NamedTemporaryFile(dir=".", suffix=file_path.suffix, delete=False) f.write(file.data) f.flush() f.close() file_type = FileTypes(file_path.suffix[1:]) loader: BaseLoader if file_type == FileTypes.TEXT: loader = TextLoader(f.name) elif file_type == FileTypes.PDF: loader = PyPDFLoader(f.name) documents = loader.load() # source must be overriden to not leak upload information # about the temp file which are rather useless anyway for doc in documents: doc.metadata["source"] = file_path.name except BaseException as err: _LOGGER.error(err) finally: if f: unlink(f.name) if not documents: _LOGGER.warning("No document was loaded!") return QAAnswer(error_msg="No document was loaded!", status=500) text_splitter = RecursiveCharacterTextSplitter( chunk_size=self._config.embedding.chunk_size, chunk_overlap=self._config.embedding.chunk_overlap, ) texts = text_splitter.split_documents(documents) if self._vectorstore is None: _LOGGER.info("Create new vector store from document.") self._vectorstore = FAISS.from_documents(texts, self._embeddings) else: _LOGGER.info("Adding document to existing vector store.") tmp = FAISS.from_documents(texts, self._embeddings) self._vectorstore.merge_from(tmp) if self._config.embedding.db_path: self._vectorstore.save_local(self._config.embedding.db_path) return QAAnswer() def _setup_dbqa(self, prompt: PromptConfig) -> BaseRetrievalQA: if "context" not in prompt.parameters: _LOGGER.warning( "Prompt does not include '{context}' variable." "It will be appened to the prompt." ) prompt.text += "\n\n{context}" _LOGGER.info( "\n===== Setup dbqa with prompt ====\n\n%s\n\n====================", prompt.text, ) qa_prompt = PromptTemplate( template=prompt.text, input_variables=prompt.parameters, ) dbqa = build_retrieval_qa( self._llm, qa_prompt, self._vectorstore, self._config.embedding.vector_count, self._config.features.return_source, ) return dbqa def _setup_dbqa_fact_checking(self, prompt: PromptConfig) -> BaseRetrievalQA: _LOGGER.info("Setup fact checking...") if "context" not in prompt.parameters: _LOGGER.warning( "Prompt does not include '{context}' variable." "It will be appened to the prompt." ) prompt.text += "\n\n{context}" fact_checking_prompt = PromptTemplate( template=prompt.text, input_variables=prompt.parameters, ) dbqa = build_retrieval_qa( self._llm, fact_checking_prompt, self._vectorstore, self._config.embedding.vector_count, self._config.features.return_source, ) return dbqa # # Process source documents # source_docs = response["source_documents"] # for i, doc in enumerate(source_docs): # _LOGGER.debug(f"\nSource Document {i+1}\n") # _LOGGER.debug(f"Source Text: {doc.page_content}") # _LOGGER.debug(f'Document Name: {doc.metadata["source"]}') # _LOGGER.debug(f'Page Number: {doc.metadata.get("page", 1)}\n') # _LOGGER.debug("=" * 60) # _LOGGER.debug(f"Time to retrieve response: {endQA - startQA}") # if CONFIG.features.fact_checking: # startFactCheck = timeit.default_timer() # dbqafact = setup_dbqa_fact_checking() # response_fact = dbqafact({"query": response["result"]}) # endFactCheck = timeit.default_timer() # _LOGGER.debug("Factcheck:") # _LOGGER.debug(f'\nAnswer: {response_fact["result"]}') # _LOGGER.debug("=" * 50) # # Process source documents # source_docs = response_fact["source_documents"] # for i, doc in enumerate(source_docs): # _LOGGER.debug(f"\nSource Document {i+1}\n") # _LOGGER.debug(f"Source Text: {doc.page_content}") # _LOGGER.debug(f'Document Name: {doc.metadata["source"]}') # _LOGGER.debug(f'Page Number: {doc.metadata.get("page", 1)}\n') # _LOGGER.debug("=" * 60) # _LOGGER.debug(f"Time to retrieve fact check: {endFactCheck - startFactCheck}")
[]
2024-01-10
FrankLeeeee/ColossalAI
applications~Chat~evaluate~gpt_evaluate.py
import concurrent.futures import os import re import time from copy import deepcopy from typing import Any, Dict, List import matplotlib.pyplot as plt import numpy as np import openai import pandas as pd import seaborn as sns import tqdm from utils import jdump, jload ref_step_template = { "en": "Now please compare the answer with the {adjective} answer, determine whether the answer is able to achieve the same level of {metric}.\n\n", "cn": "่ฏทๆฏ”่พƒ็ญ”ๆกˆไธŽไธŠ้ข็š„{adjective}็ญ”ๆกˆ๏ผŒ็กฎๅฎš็ญ”ๆกˆๆ˜ฏๅฆๅฏไปฅ่พพๅˆฐไธŽ่ฏฅ{adjective}็ญ”ๆกˆๅŒๆ ทๆฐดๅนณ็š„{metric}ใ€‚\n\n" } ref_answer_template_general = { "en": "\nAn example answer with good quality is as follows:\n\n{answer}\n\n", "cn": "\nไธ€ไธชไผ˜่ดจ็š„็คบไพ‹็ญ”ๆกˆๅฆ‚ไธ‹๏ผš\n\n{answer}\n\n" } ref_answer_template_correctness = { "en": "\nA correct answer is as follows:\n\n{answer}\n\n", "cn": "\nๆ ‡ๅ‡†็ญ”ๆกˆๅฆ‚ไธ‹๏ผš\n\n{answer}\n\n" } def get_battle_result(sys_prompt: str, user_prompt: str, id: int, max_tokens: int = 2048) -> Dict[str, Any]: """ Get battle evaluation from GPT-4. Args: sys_prompt: prompt for the system. user_prompt: prompt for the user. id: id of the answers for comparison. max_tokens: the maximum number of tokens to generate in the chat completion. Returns: An evaluation of one comparison. """ MAX_API_RETRY = 3 for _ in range(MAX_API_RETRY): try: response = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": sys_prompt }, { "role": "user", "content": user_prompt, }, ], temperature=0.2, max_tokens=max_tokens, ) evaluation = response["choices"][0]["message"]["content"] return {"evaluation": evaluation, "id": id} except Exception as e: print(e) time.sleep(1) print(f"Evaluation {id} failed after {MAX_API_RETRY} retries.") return {"evaluation": "", "id": id} def parse_battle_score(evaluation: str) -> List[float]: """ Parse evaluation from GPT-4 and get the scores of model 1 and 2. Args: evaluation: evaluation from GPT-4. Returns: A score pair of two different model answers. """ try: pattern = re.compile("([0-9]|10) out of 10") sp = re.findall(pattern, evaluation) if len(re.findall(pattern, evaluation)) == 2: return [float(sp[0]), float(sp[1])] pattern = re.compile("a score of ([0-9]|10)") sp = re.findall(pattern, evaluation) if len(re.findall(pattern, evaluation)) == 2: return [float(sp[0]), float(sp[1])] pattern = re.compile("([0-9]|10)/10") sp = re.findall(pattern, evaluation) if len(re.findall(pattern, evaluation)) == 2: return [float(sp[0]), float(sp[1])] score_pair = evaluation.split("\n")[0] score_pair = score_pair.replace(",", " ") sp = score_pair.split(" ") if len(sp) == 2: return [float(sp[0]), float(sp[1])] else: raise Exception(f"Invalid score pair. Got {evaluation}.") except Exception as e: return [-1, -1] def battle(answer1: List[Dict], answer2: List[Dict], prompt_dict: Dict[str, Any]) -> List[Dict]: """ Use GPT-4 to compare answers of two different models. Args: answer1: answers of model 1. answer2: answers of model 2. prompt_dict: prompt for battle. Returns: Evaluations of all comparison pairs. """ assert len(answer1) == len(answer2) handles = [] evaluation_file = [] total_len = len(answer1) question_idx_list = list(range(total_len)) print(f" Total number of answers: {len(answer1)}.") evaluations = [] with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: futures = [] for i in question_idx_list: assert answer1[i]["id"] == answer2[i]["id"] answer_id = answer1[i]["id"] ques = answer1[i]["instruction"] if answer1[i][ "input"] == "" else answer1[i]["instruction"] + " " + answer1[i]["input"] cat = answer1[i]["category"] ans1 = answer1[i]["output"] ans2 = answer2[i]["output"] sys_prompt = prompt_dict["system_prompt"] prompt_template = prompt_dict["prompt_template"] prompt = prompt_template.format( question=ques, answer_1=ans1, answer_2=ans2, prompt=prompt_dict["prompt"], ) future = executor.submit(get_battle_result, sys_prompt, prompt, answer_id, 2048) futures.append(future) for future in tqdm.tqdm(concurrent.futures.as_completed(futures), total=len(futures)): evaluations.append(future.result()) evaluations.sort(key=lambda x: x["id"]) return evaluations def save_battle_results(evaluations: List[Dict], name1: str, name2: str, save_path: str) -> None: """ Save evaluation results (model 1 vs model 2) from GPT-4. Args: evaluations: evaluation results from GPT-4. name1: model 1 's name. name2: model 2 's name. save_path: path to save battle results. """ evaluation_file = deepcopy(evaluations) ans1_score = 0 ans2_score = 0 better_count = 0 worse_count = 0 tie_count = 0 invalid_count = 0 better_file = [] worse_file = [] tie_file = [] invalid_file = [] for idx, evaluation in enumerate(evaluations): scores = parse_battle_score(evaluation["evaluation"]) evaluation_file[idx]["score"] = scores if scores[0] == -1 and scores[1] == -1: invalid_count += 1 invalid_file.append(evaluation_file[idx]) print(f'Invalid score pair: {evaluation_file[idx]["id"]}.') else: if scores[0] > scores[1]: worse_count += 1 worse_file.append(evaluation_file[idx]) elif scores[0] < scores[1]: better_count += 1 better_file.append(evaluation_file[idx]) else: tie_count += 1 tie_file.append(evaluation_file[idx]) ans1_score += scores[0] ans2_score += scores[1] prefix = f"{name1}_vs_{name2}" if not os.path.exists(save_path): os.makedirs(save_path) jdump(better_file, os.path.join(save_path, prefix, f"{name2}_better.json")) jdump(worse_file, os.path.join(save_path, prefix, f"{name2}_worse.json")) jdump(tie_file, os.path.join(save_path, prefix, f"{prefix}_tie.json")) jdump(invalid_file, os.path.join(save_path, prefix, f"{prefix}_invalid.json")) jdump(evaluation_file, os.path.join(save_path, prefix, f"{prefix}_evaluations.json")) if os.path.exists(os.path.join(save_path, "battle_results.json")): results = jload(os.path.join(save_path, "battle_results.json")) else: results = {} results[prefix] = { "model": [name1, name2], "better": better_count, "worse": worse_count, "tie": tie_count, "win_rate": better_count / (len(evaluations) - invalid_count), "score": [ ans1_score / (len(evaluations) - invalid_count), ans2_score / (len(evaluations) - invalid_count), ], } jdump(results, os.path.join(save_path, "battle_results.json")) print(f"Total {invalid_count} invalid score pair(s).") print(f"Model {name2} has {better_count} better answer(s).") print(f"Model {name2} has {worse_count} worse answer(s).") print(f"{tie_count} answer(s) play(s) to a tie.") print(f"Win rate of model {name2}: {better_count/(len(evaluations)-invalid_count):.2f}") print(f"Model {name1} average score: {ans1_score/(len(evaluations)-invalid_count):.2f}") print(f"Model {name2} average score: {ans2_score/(len(evaluations)-invalid_count):.2f}") def reference_template(metric: str, language: str, reference: Dict[str, Any]) -> str: """ Get prompt template for GPT evaluation with reference. Different languages have different prompt templates. Args: metric: metric used in GPT evaluation with reference. language: language for the template. reference: the instruction that contains target answer. Returns: Prompt template for GPT evaluation with reference. """ step_to_add = ref_step_template[language] for_the_given_answer = "{metric} (1-5) (directly give the score for the given answer):" if language == "en" else "{metric} (1-5) (็›ดๆŽฅๅฏน็ป™ๅฎš็ญ”ๆกˆๆ‰“ๅˆ†)" # adjective is used to describe the word "answer" in the prompt. adjective = "example" if language == "en" else "็คบไพ‹" answer_to_add = ref_answer_template_general[language] # Only for correctness, we will provide a correct answer and so the adjective for "answer" will be "correct". The prompt words will be "a correct answer". # In other cases, the prompt words will be "an example answer with good quality" by default. if metric.lower() == "correctness": adjective = "correct" if language == "en" else "ๆ ‡ๅ‡†" answer_to_add = ref_answer_template_correctness[language] answer_to_add = answer_to_add.format(answer=reference["target"] if reference["target"] else reference["output"]) step_to_add = step_to_add.format(metric=metric.lower(), adjective=adjective) + for_the_given_answer.format(metric=metric) return answer_to_add + step_to_add def fill_in_message(role: str, content: str) -> Dict[str, str]: """ Generate one formatted message to send through chat completion. Args: role: the role of the author of this message. content: the contents of the message. Returns: One message to send through chat completion. """ return {"role": role, "content": content} def multiturn_chat_completion(user_messages: List[str], model: str, max_tokens: int = 1, turns=2) -> Dict[str, Any]: """ Do multi-turn chat completion. When turns == 1, it is a one-turn conversation for normal GPT evaluation. When turns == 2, it is a two-turn conversation which is used for GPT evaluation with reference answers. Args: user_messages: messages user wants to send. model: the model used to evaluate answers. max_tokens: the maximum number of tokens to generate in the chat completion. turns: the number of turns for conversation. Returns: Last turn's response. """ if len(user_messages) != turns: raise Exception("The length of user messages should be equal to the turn number!") assistant_responses = [] for i in range(turns): messages_to_send = [] for j in range(i): messages_to_send.append(fill_in_message("user", user_messages[j])) messages_to_send.append( fill_in_message("assistant", assistant_responses[j]["choices"][0]["message"]["content"])) # Length of user messages == Length of assistant messages + 1 # Because we always expect the api to response messages_to_send.append(fill_in_message("user", user_messages[i])) response = openai.ChatCompletion.create( model=model, messages=messages_to_send, temperature=0, max_tokens=max_tokens, ) # Avoid exceeding rate limits. # You can comment this line if your request doesn't contain many tokens. time.sleep(1) assistant_responses.append(response) return assistant_responses[-1] def get_gpt_evaluation_without_logprobs(prompt: Dict[str, Any], inst: Dict[str, Any], metrics: List[str], language: str, reference: Dict[str, Any] = None, model: str = "gpt-3.5-turbo", max_tokens: int = 2048) -> Dict[str, Any]: """ Use chat models(gpt-3.5-turbo or gpt-4) to evaluate one model answer. Temperature is set to 0 to make the model more deterministic. Args: prompt: a dictionary including prompt template, CoT and metrics. inst: the instruction that is needed to be evaluated. metrics: the metrics for evaluation. language: language used to change the CoT(add one more step about comparing the given answer and reference) if reference is not None. reference: the reference answer. model: the model used to evaluate answers. max_tokens: the maximum number of tokens to generate in the chat completion. Returns: An evaluation of one answer. """ MAX_API_RETRY = 3 question = (inst["instruction"] if inst["input"] == "" else inst["instruction"] + "\n" + inst["input"]) answer = inst["output"] inst["evaluation"] = {} for metric in metrics: if prompt["metrics"].get(metric, None) is None: raise Exception( f"Unsupported metric {metric} for category {inst['category']}! You should add this metric in the prompt file!" ) for i in range(MAX_API_RETRY): try: prompt_reference = "" if reference is None else reference_template(metric, language, reference) prompt_1st_round = prompt["prompt"].format( question=question, answer=answer, metric=prompt["metrics"][metric], steps=prompt["CoT"][metric], ) if prompt_reference: # Do a 2-round conversation response = multiturn_chat_completion([prompt_1st_round, prompt_reference], model, max_tokens=max_tokens, turns=2) else: response = multiturn_chat_completion([prompt_1st_round], model, max_tokens=max_tokens, turns=1) inst["evaluation"][metric] = { "response": response["choices"][0]["message"]["content"], "logprobs": None, } # Prevent exceeding rate limits because we have multiple workers. # But this will slow down the evaluation process. # You can comment this line if your request doesn't contain many tokens. time.sleep(len(metrics) * 0.5) break except Exception as e: print(e) time.sleep(1) if metric not in inst["evaluation"]: print(f"Evaluation {inst['id']} for metric {metric} failed after {MAX_API_RETRY} retries.") inst["evaluation"][metric] = {} return inst def get_gpt_evaluation_with_logprobs(prompt: Dict[str, Any], inst: Dict[str, Any], metrics: List[str], max_tokens: int = 2048) -> Dict[str, Any]: """ Use completion model(text-davinci-003) to evaluate one model answer. Only completion models can return log probabilities. Temperature is set to 0 to make the model more deterministic. Args: prompt: a dictionary including prompt template, CoT and metrics. inst: the instruction that is needed to be evaluated. metrics: the metrics for evaluation. max_tokens: the maximum number of tokens to generate in the completion. Returns: An evaluation of one answer. """ MAX_API_RETRY = 3 question = (inst["instruction"] if inst["input"] == "" else inst["instruction"] + "\n" + inst["input"]) answer = inst["output"] inst["evaluation"] = {} for metric in metrics: if prompt["metrics"].get(metric, None) is None: raise Exception( f"Unsupported metric {metric} for category {inst['category']}! You should add this metric in the prompt file!" ) for i in range(MAX_API_RETRY): try: response = openai.Completion.create( model="text-davinci-003", prompt=prompt["prompt"].format( question=question, answer=answer, metric=prompt["metrics"][metric], steps=prompt["CoT"][metric], ), logprobs=5, temperature=0, max_tokens=max_tokens, ) inst["evaluation"][metric] = { "response": response["choices"][0]["text"], "logprobs": response["choices"][0]["logprobs"]["top_logprobs"], } # Prevent exceeding rate limits because we have multiple workers. # But this will slow down the evaluation process. # You can comment this line if your request doesn't contain many tokens. time.sleep(len(metrics) * 0.5) break except Exception as e: print(e) time.sleep(1) if metric not in inst["evaluation"]: print(f"Evaluation {inst['id']} for metric {metric} failed after {MAX_API_RETRY} retries.") inst["evaluation"][metric] = {} return inst def evaluate(answers: List[Dict], prompt: Dict[str, Any], metrics: List[str], category: str, model: str, language: str, references: List[Dict] = None) -> List[Dict]: """ Use GPT models to evaluate model answers and save evaluation results. Args: answers: model answers. prompt: prompt for GPT evaluation. metrics: metrics for GPT evaluation. category: the category of the model answers for evaluation. model: the specific GPT model used to evaluate answers. language: language used in GPT evaluation references: references for GPT evaluation Returns: Evaluations of the given answers. """ print(f"The number of instances of category {category}'s is {len(answers)}.") evaluations = [] metrics_str = ", ".join(x for x in metrics) print(f"Category {category}'s metrics are {metrics_str}.") with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: futures = [] for idx, inst in enumerate(answers): # Completion models can return log probabilities. if model == "text-davinci-003": future = executor.submit(get_gpt_evaluation_with_logprobs, prompt, inst, metrics, 1) else: future = executor.submit(get_gpt_evaluation_without_logprobs, prompt, inst, metrics, language, reference=None if references is None else references[idx], model=model, max_tokens=1) futures.append(future) for future in tqdm.tqdm( concurrent.futures.as_completed(futures), desc=f"{category}: ", total=len(futures), ): evaluations.append(future.result()) evaluations.sort(key=lambda x: x["id"]) print(f"{category} done.") return evaluations def calculate_scores_form_logprobs(logprobs: Dict[str, Any]) -> float: """ Calculate the score according to log probabilities returned by text-davinci-003. Calculation formula: score = sum(score_i * exp(value)) where score_i is the score which corresponds to the key(predicted token) and value is its log probability. Ref: https://arxiv.org/abs/2303.16634 This paper proposes NLG evaluation methods using text-davinci-003(log probabilities returned by completion models) and GPT-4(probabilities obtained by sampling). Args: logprobs: logprobs returned by openai.Completion. Returns: The score of one answer. """ # GPT-3.5 only returns score of 1 to 5. prob = np.zeros(5) for key, value in logprobs.items(): # Sometimes the key will be one byte of a unicode character which takes the form of "bytes:\\xe7". # It is meaningless and thus we don't calculate probability. if "bytes" in key: continue # results[0] is the score which corresponds to the key(predicted token). # For example, key "5" corresponds to score 5. results = re.findall(r"\d", key) if len(results) == 1: prob[int(results[0]) - 1] = prob[int(results[0]) - 1] + np.exp(value) score = np.dot(np.arange(1, 6), prob) return score def calculate_scores_form_response(response: str, evaluation: Dict[str, Any]) -> int: """ Calculate the score from the response returned by gpt-3.5-turbo or gpt-4. Different from text-davinci-003, this function directly calculates the score according to the plain response returned by gpt-3.5-turbo or gpt-4. Although text-davinci-003 can return log probabilities, it costs ten times as much as gpt-3.5-turbo. Args: response: logprobs returned by openai.Completion. evaluation: the evaluation corresponds to the question. Returns: The score of one answer. """ try: results = re.findall(r"\d", response) if len(results) == 1: return int(results[0]) else: raise Exception(f"Invalid score pair. Got {evaluation}.") except Exception as e: return 0 def save_gpt_evaluation_results(model_name: str, gpt_evaluation_results: Dict[str, Any], save_path: str) -> Dict[str, Any]: """ Save evaluation results for different categories for one model. Args: model_name: name of the model for saving evaluation results. gpt_evaluation_results: evaluations results for all of the model answers. save_path: path to save GPT evaluation statistics. """ all_evaluations = [] for category, evaluations in gpt_evaluation_results.items(): jdump(evaluations, os.path.join(save_path, model_name, f"{category}_evaluation_results.json")) all_evaluations.extend(evaluations) jdump(all_evaluations, os.path.join(save_path, f"{model_name}_evaluation_results.json")) return all_evaluations def save_gpt_evaluation_statistics(model_name: str, evaluations: List[Dict], save_path: str) -> None: """ Generate statistics for one model. Args: model_name: name of the model for saving statistics. evaluations: evaluations for all of the model answers. save_path: path to save GPT evaluation statistics. """ if not os.path.exists(save_path): os.makedirs(save_path) data_per_category = {} for evaluation in evaluations: category = evaluation["category"] if evaluation["category"] in data_per_category.keys(): data_per_category[category].append(evaluation) else: data_per_category[category] = [evaluation] all_statistics = {} for category, data in data_per_category.items(): metrics = data[0]["evaluation"].keys() scores = {metric: [] for metric in metrics} for evaluation in data: for metric in metrics: if evaluation["evaluation"][metric] == {}: # This means after 3 retries, the server still returns an error and we set the score to 0. scores[metric].append(0) elif evaluation["evaluation"][metric]["logprobs"] is not None: scores[metric].append( calculate_scores_form_logprobs(evaluation["evaluation"][metric]["logprobs"][0])) else: scores[metric].append( calculate_scores_form_response(evaluation["evaluation"][metric]["response"], evaluation)) statistics = {} for metric in metrics: arg_sort = np.argsort(scores[metric]) statistics[metric] = {} statistics[metric]["avg_score"] = sum(scores[metric]) / len(data) statistics[metric]["best_3"] = {data[i]["id"]: scores[metric][i] for i in arg_sort[-3:][::-1]} statistics[metric]["worst_3"] = {data[i]["id"]: scores[metric][i] for i in arg_sort[:3]} all_statistics[category] = statistics jdump( all_statistics, os.path.join(save_path, f"{model_name}_evaluation_statistics.json"), ) def analyze_gpt_evaluation_statistics(statistics_path: str, save_path: str) -> None: """ Analyze and visualize all GPT evaluation statistics in the given directory. Args: statistics_path: path to all the models' statistics. save_path: path to save table and visualization results. """ if not os.path.exists(statistics_path): raise Exception(f'The given directory "{statistics_path}" doesn\'t exist! No statistics found!') all_statistics = {} for file_name in os.listdir(statistics_path): if file_name.endswith("_evaluation_statistics.json"): model_name = file_name.split("_evaluation_statistics.json")[0] all_statistics[model_name] = jload(os.path.join(statistics_path, file_name)) if len(list(all_statistics.keys())) == 0: raise Exception(f'There are no statistics in the given directory "{statistics_path}"!') frame_all = { "model": [], "category": [], "metric": [], "avg_score": [], "best_3": [], "worst_3": [], } frame_per_category = {} for model_name, model_statistics in all_statistics.items(): for category, category_statistics in model_statistics.items(): if frame_per_category.get(category) is None: frame_per_category[category] = { "model": [], "metric": [], "avg_score": [], "best_3": [], "worst_3": [], } for metric, metric_statistics in category_statistics.items(): frame_all["model"].append(model_name) frame_all["category"].append(category) frame_all["metric"].append(metric) frame_all["avg_score"].append(metric_statistics["avg_score"]) frame_all["best_3"].append(metric_statistics["best_3"]) frame_all["worst_3"].append(metric_statistics["worst_3"]) frame_per_category[category]["model"].append(model_name) frame_per_category[category]["metric"].append(metric) frame_per_category[category]["avg_score"].append(metric_statistics["avg_score"]) frame_per_category[category]["best_3"].append(metric_statistics["best_3"]) frame_per_category[category]["worst_3"].append(metric_statistics["worst_3"]) if not os.path.exists(save_path): os.makedirs(save_path) frame_all = pd.DataFrame(frame_all) frame_all.to_csv(os.path.join(save_path, "gpt_evaluation_statistics.csv")) for category in tqdm.tqdm( frame_per_category.keys(), desc=f"GPT evaluation: ", total=len(frame_per_category.keys()), ): data = pd.DataFrame(frame_per_category[category]) sns.set() fig = plt.figure(figsize=(16, 10)) plt.ylim((0, 5)) fig = sns.barplot(x="metric", y="avg_score", hue="model", data=data, dodge=True) fig.set_title(f"Comparison between Different Models for Category {category.title()}") plt.xlabel("Evaluation Metric") plt.ylabel("Average Score") figure = fig.get_figure() figure.savefig(os.path.join(save_path, f"{category}.png"), dpi=400) plt.close()
[ "{'en': '\\nA correct answer is as follows:\\n\\n{answer}\\n\\n', 'cn': '\\nๆ ‡ๅ‡†็ญ”ๆกˆๅฆ‚ไธ‹๏ผš\\n\\n{answer}\\n\\n'}", "{'en': 'Now please compare the answer with the {adjective} answer, determine whether the answer is able to achieve the same level of {metric}.\\n\\n', 'cn': '่ฏทๆฏ”่พƒ็ญ”ๆกˆไธŽไธŠ้ข็š„{adjective}็ญ”ๆกˆ๏ผŒ็กฎๅฎš็ญ”ๆกˆๆ˜ฏๅฆๅฏไปฅ่พพๅˆฐไธŽ่ฏฅ{adjective}็ญ”ๆกˆๅŒๆ ทๆฐดๅนณ็š„{metric}ใ€‚\\n\\n'}", "{'en': '\\nAn example answer with good quality is as follows:\\n\\n{answer}\\n\\n', 'cn': '\\nไธ€ไธชไผ˜่ดจ็š„็คบไพ‹็ญ”ๆกˆๅฆ‚ไธ‹๏ผš\\n\\n{answer}\\n\\n'}", "prompt_template", "system_prompt" ]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%201~Archived~calc_coherence_hdp.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he file_name = 'models_hdp.pkl' output = 'hdp_coherence.pkl' model_list = [] data_list = [] dict_list = [] with open(file_name, 'rb') as f: while True: try: iteration, model, time_arr, data, id2word, _ = pickle.load(f) print(iteration) model_list.append(model) data_list.append(data) dict_list.append(id2word) except: break coherence_list = [] count = 0 for i in range(0, len(model_list)): model = model_list[i] data = data_list[i] id2word = dict_list[i] print(id2word) count += 1 print('Iteration '+str(count)) coherencemodel = CoherenceModel( model=model, texts=data, dictionary=id2word, coherence='c_v') coherence_list.append(coherencemodel.get_coherence()) with open(output, 'wb') as f: pickle.dump((coherence_list, time_arr), f)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%203~Archived~onlinelda_comparison.py
#!/usr/bin/env python # coding: utf-8 # In[1]: #!/usr/bin/env python # coding: utf-8 # No of Documents = 17849 <br> # Start Date = 2016-10-31 <br> # End Date = 2017-12-31 <br> # <br> # Google Alerts Start-Date = Feb-26-2019 <br> # # * 2016-12-31 - 9442 # * 2017-02-28 - 12710 # * 2017-04-30 - 14339 # * 2017-06-30 - 15445 # * 2017-08-31 - 16287 # * 2017-10-31 - 16998 # * 2017-12-31 - 17849 # In[1]: # imports from __future__ import print_function from time import time import numpy as np import pandas as pd import pickle import random # Sklearn from pprint import pprint # Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel import re, nltk, spacy # Plotting tools import pyLDAvis import pyLDAvis.sklearn from pyLDAvis import gensim as pyLDAvisgensim import matplotlib.pyplot as plt # Loading the entire corpus containing 17849 documents # In[2]: f = open("output_online.txt","w+") doc_set = [] with open("demon_combined.pkl", "rb") as pickle_in: while True: try: doc_set.append(pickle.load(pickle_in)) except: break # Choose the two models for which you would like to build the confusion matrix.<br> # Input: No of Documents in Corpus, Best topic no. # # * 09442 - best topic no = 34 (start = 10, end = 40) # * 12710 - best topic no = 37 # * 14339 - best topic no = 31 # * 15445 - best topic no = 31 # * 16287 - best topic no = 29 # * 16998 - best topic no = 25 # * 17849 - best topic no = 37 # In[3]: answers_positive = [] answers_negative = [] model_nos=(0,6) # In[4]: settings=[(9442,34),(12710,37),(14339,31),(15445,31),(16287,29),(16998,25),(17849,37)] offset = 10 corpus_length_1 = settings[model_nos[0]][0] corpus_length_2 = settings[model_nos[1]][0] best_1 = settings[model_nos[0]][1] - offset best_2 = settings[model_nos[1]][1] - offset print(corpus_length_1) print(corpus_length_2) print(best_1+offset) print(best_2+offset) # Loading the corresponding Models and Corpus. # In[5]: doc_set_1 = doc_set[:corpus_length_1] doc_set_2 = doc_set[:corpus_length_2] doc_set_diff = doc_set[corpus_length_1:corpus_length_2+1] model_1_batch=[] model_2_batch=[] with open("Models/ldamodels_demon_"+str(corpus_length_1)+".pkl", "rb") as pickle_in: while True: try: model_1_batch.append(pickle.load(pickle_in)) except: break with open("Models/ldamodels_demon_"+str(corpus_length_2)+".pkl", "rb") as pickle_in: while True: try: model_2_batch.append(pickle.load(pickle_in)) except: break # In[6]: # NLTK Stop words from nltk.corpus import stopwords stop_words = stopwords.words('english') def sent_to_words(sentences): for sentence in sentences: yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) # deacc=True removes punctuations # Define functions for stopwords, bigrams, trigrams and lemmatization def remove_stopwords(texts): return [[word for word in gensim.utils.simple_preprocess(str(doc)) if word not in stop_words] for doc in texts] def make_bigrams(texts,bigram_mod): return [bigram_mod[doc] for doc in texts] def make_trigrams(texts,trigram_mod): return [trigram_mod[bigram_mod[doc]] for doc in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" # Initialize spacy 'en' model, keeping only tagger component (for efficiency) # python3 -m spacy download en nlp = spacy.load('en', disable=['parser', 'ner']) texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out def preprocess(doc_set): df = pd.DataFrame(doc_set) df.columns = ['text'] # Convert to list data = df.text.values.tolist() # Remove @ characters, newlines, single quotes data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] data = [re.sub('\s+', ' ', sent) for sent in data] data = [re.sub("\'", "", sent) for sent in data] pprint(data[:1]) data_words = list(sent_to_words(data)) print(data_words[:1]) # Build the bigram and trigram models bigram = gensim.models.Phrases(data_words, min_count=5, threshold=100) # higher threshold fewer phrases. trigram = gensim.models.Phrases(bigram[data_words], threshold=100) # Faster way to get a sentence clubbed as a trigram/bigram is to use the Phraser interface bigram_mod = gensim.models.phrases.Phraser(bigram) trigram_mod = gensim.models.phrases.Phraser(trigram) # See bigrams and trigrams example print(trigram_mod[bigram_mod[data_words[0]]]) # Remove Stop Words data_words_nostops = remove_stopwords(data_words) # Form Bigrams data_words_bigrams = make_bigrams(data_words_nostops,bigram_mod) # Do lemmatization keeping only noun, adj, vb, adv data_lemmatized = lemmatization(data_words_bigrams, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']) print(data_lemmatized[:1]) return data_lemmatized # In[2]: data_lemmatized_1=preprocess(doc_set_1) print(data_lemmatized_1[1]) # In[8]: # Create Dictionary id2word_1 = corpora.Dictionary(data_lemmatized_1) #Filter words id2word_1.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) # Create Corpus texts = data_lemmatized_1 # Term Document Frequency corpus_1 = [id2word_1.doc2bow(text) for text in texts] # View print(corpus_1[:1]) # In[9]: # In[3]: data_lemmatized_2=preprocess(doc_set_2) # In[10]: # Create Dictionary id2word_2 = corpora.Dictionary(data_lemmatized_2) #Filter words id2word_2.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) # Create Corpus texts = data_lemmatized_2 # Term Document Frequency corpus_2 = [id2word_2.doc2bow(text) for text in texts] # View print(corpus_2[:1]) # In[4]: data_lemmatized_diff=preprocess(doc_set_diff) id2word_diff = corpora.Dictionary(data_lemmatized_diff) id2word_diff.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) texts = data_lemmatized_diff corpus_diff = [id2word_diff.doc2bow(text) for text in texts] print(corpus_diff[:1]) # In[6]: coherencemodel = CoherenceModel(model=model_1_batch[0][0][best_1], texts=data_lemmatized_1, dictionary=id2word_1, coherence='c_v') print(coherencemodel.get_coherence()) # In[17]: model_1_batch[0][0][best_1] # In[7]: model_2_online = model_1_batch[0][0][best_1] model_2_online.update(corpus_diff) print(model_2_online) # In[8]: coherencemodel2 = CoherenceModel(model=model_2_online, texts=data_lemmatized_2, dictionary=id2word_2, coherence='c_v') print(coherencemodel2.get_coherence()) # In[9]: lda_corpus_1 = [max(prob,key=lambda y:y[1]) for prob in model_2_online[corpus_2] ] lda_corpus_2 = [max(prob,key=lambda y:y[1]) for prob in model_2_batch[corpus_2] ] print(lda_corpus_1==lda_corpus_2) print(len(lda_corpus_1)) print(len(lda_corpus_2)) max_corpus_size = max(len(lda_corpus_1),len(lda_corpus_2)) print(max_corpus_size) total_permutations=max_corpus_size*(max_corpus_size-1)/2 #nC2 combinations # In[ ]: model_2==model_1 # In[ ]: positive=0 negative=0 for i in range(max_corpus_size): for j in range(i+1,max_corpus_size): if(lda_corpus_1[i][0]==lda_corpus_1[j][0] and lda_corpus_2[i][0]==lda_corpus_2[j][0]): positive = positive+1 elif(lda_corpus_1[i][0]!=lda_corpus_1[j][0] and lda_corpus_2[i][0]==lda_corpus_2[j][0]): negative = negative+1 elif(lda_corpus_1[i][0]==lda_corpus_1[j][0] and lda_corpus_2[i][0]!=lda_corpus_2[j][0]): negative = negative+1 elif(lda_corpus_1[i][0]!=lda_corpus_1[j][0] and lda_corpus_2[i][0]!=lda_corpus_2[j][0]): positive=positive+1 f.write(str((positive+negative==total_permutations))) f.write("% of positives = "+str(round(positive*100/total_permutations,2))+"%") f.write("% of negatives = "+str(round(negative*100/total_permutations,2))+"%") answers_positive.append(round(positive*100/total_permutations,2)) answers_negative.append(round(negative*100/total_permutations,2)) f.write(str(answers_positive)) f.write(str(answers_negative)) # In[ ]: print(answers_positive) print(answers_negative) # Online LDA (Before (Batch unupdated with online) vs After(Batch Updated with Online)) - same model from batch # # | Models Tested (AuB) | Positive% | Negative% | # | --- | --- | --- | # | (0,1) | 99.63 | 0.37 | # | (1,2) | 99.41 | 0.59 | # | (2,3) | 99.73 | 0.27 | # | (3,4) | 99.66 | 0.34 | # | (4,5) | 99.67 | 0.33 | # | (5,6) | 99.87 | 0.13 | # | (0,6) | 99.54 | 0.4 | # # Online LDA (Batch i, Batch i+1) Compared after Batch i trained with corpus_dif with online LDA # In[ ]:
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%203~Archived~lda_online_exp1.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import copy import gensim import gensim.corpora as corpora from gensim.corpora import Dictionary from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 5000 STEP_SIZE = 200 # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # Initialize Parameters total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] id2word = Dictionary(documents=data) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. # start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) corpus_permanent = copy.deepcopy(corpus) doc_size = [] positive_arr = [] # end = timeit.default_timer() # time_taken = end - start # total_time += time_taken # time_arr.append(time_taken) # Obtain C_V Coherence of existing model in simulation # coherencemodel2 = CoherenceModel( # model=lda, texts=data, dictionary=id2word, coherence='c_v') # coherence_arr.append(coherencemodel2.get_coherence()) f2 = open('models_online.pkl', 'wb') count = 0 # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] pruned_docs = [] for doc in new_docs: pruned_data = [] for x in doc: if x in id2word.values(): pruned_data.append(x) pruned_docs.append(pruned_data) new_docs = pruned_docs print('Pruning Done') # Updating Dictionary # id2word.add_documents(new_docs) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) prev_corpus = copy.deepcopy(corpus) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 prev_lda = copy.deepcopy(lda) print('MODEL NO:'+str(count)) start = timeit.default_timer() lda.update(corpus) print('MODEL DONE') end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) updated_corpus = copy.deepcopy(prev_corpus) updated_corpus.extend(corpus) corpus = updated_corpus doc_size.append(i) positive_arr.append(he.calc_confusion_matrix( prev_lda, lda, updated_corpus)) # positive_arr.append(he.calc_confusion_matrix( # prev_lda, lda, prev_corpus)) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB coherencemodel2 = CoherenceModel( model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) # with open('models_online.pkl', 'ab') as f2: pickle.dump(positive_arr, f2) # pickle.dump( # (count, lda, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) f2.close() print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%203~.ipynb_checkpoints~calculate_coherence-checkpoint.py
#!/usr/bin/env python # coding: utf-8 # In[19]: import pandas as pd import numpy as np import pickle import re import timeit import spacy import copy import gensim import gensim.corpora as corpora from gensim.corpora import Dictionary from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he input1 = '../../data/preprocessed_data/doc_indexes/demon.pkl' input2 = '../../data/preprocessed_data/corpus_dict/demon_corp.pkl' output = '../../data/temp/demon_confusion.pkl' with open(input1,'rb') as f: texts,INITIAL_DOC_SIZE, DOC_TEMPORAL_INCREMENT = pickle.load(f) with open(input2, 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # In[20]: # for i in DOC_TEMPORAL_INCREMENT[:167]: # INITIAL_DOC_SIZE+=i # DOC_TEMPORAL_INCREMENT=DOC_TEMPORAL_INCREMENT[-10:] INITIAL_DOC_SIZE = DOC_TEMPORAL_INCREMENT[140] DOC_TEMPORAL_INCREMENT = DOC_TEMPORAL_INCREMENT[141:] # In[21]: # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] id2word = Dictionary(documents=data_lemmatized) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) #Baseline Model corpus_baseline = copy.deepcopy(corpus) lda_baseline = copy.deepcopy(lda) # In[ ]: # The loop simulates arrival of new documents in batches where batch_size is defined in DOC_TEMPORAL_INCREMENT doc_size = [] positive_arr = [] f2 = open(output, 'wb') count = 0 doc_size_counter = INITIAL_DOC_SIZE print('Total Corpus Length:',len(data_lemmatized)) for i in DOC_TEMPORAL_INCREMENT: # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[doc_size_counter:doc_size_counter+i] doc_size_counter += i prev_corpus = copy.deepcopy(corpus) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 print('MODEL NO:'+str(count)) lda.update(corpus) print('MODEL DONE') prev_corpus.extend(corpus) corpus = copy.deepcopy(prev_corpus) doc_size.append(i) positive_arr.append(he.calc_confusion_matrix( lda_baseline, lda, corpus)) pickle.dump((positive_arr, doc_size), f2) f2.close() # In[ ]: import matplotlib.pyplot as plt plt.plot(positive_arr) plt.show() # In[18]: positive_arr # In[ ]:
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%201~helper.py
import pandas as pd import numpy as np import pickle import re import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel from nltk.corpus import stopwords # Initialize spacy 'en' model, keeping only tagger component (for efficiency) nlp = spacy.load('en', disable=['parser', 'ner']) def sent_to_words(sentences): for sentence in sentences: # deacc=True removes punctuations yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) def remove_stopwords(texts, stop_words): return [[word for word in gensim.utils.simple_preprocess(str(doc)) if word not in stop_words] for doc in texts] def make_bigrams(texts, bigram_mod): return [bigram_mod[doc] for doc in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append( [token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out def create_dictionary(data_lemmatized): # Create Dictionary id2word = corpora.Dictionary(data_lemmatized) # Filter words id2word.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) return id2word def create_model_corpus(id2word, data_lemmatized): return [id2word.doc2bow(text) for text in data_lemmatized] def build_hdp(corpus, id2word): hdpmodel = HdpModel(corpus=corpus, id2word=id2word, chunksize=2000) hdptopics = hdpmodel.show_topics(formatted=False) hdptopics = [[word for word, prob in topic] for topicid, topic in hdptopics] return hdpmodel, hdptopics
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
preprocess.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he stop_words = stopwords.words('english') ''' Preprocess your data using this code and save it in a pickle file so you can reuse it later. ''' input_file = 'data/massmedia-data/farmers_date_indexed.pkl' output_file = 'data/preprocessed_data/corpus_dict/farmers_corp.pkl' print('Loading Documents.....') documents = [] with open(input_file, 'rb') as f: docs,_ = pickle.load(f) for i in docs: documents.append(i['text']) print('Sample Document') print(documents[250]) print('Simple Preprocessing') # Document Preprocessing data = documents.copy() # Removes phrases with @ in them data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] # Truncates multiple consecutive whitespace to one data = [re.sub('\s+', ' ', sent) for sent in data] # Removes ' characters data = [re.sub("\'", "", sent) for sent in data] data_words = list(he.sent_to_words(data)) print('Building Bigrams') # Making Bigrams - Higher the threshold, fewer the phrases bigram = gensim.models.Phrases(data_words, min_count=5, threshold=100) bigram_mod = gensim.models.phrases.Phraser(bigram) print('Removing Stopwords') # Remove Stop Words data_words_nostops = he.remove_stopwords(data_words, stop_words) print('Forming Bigrams') # Form Bigrams data_words_bigrams = he.make_bigrams(data_words_nostops, bigram_mod) print('Lemmatizing Data') # Lemmatize Data data_lemmatized = he.lemmatization(data_words_bigrams, allowed_postags=[ 'NOUN', 'ADJ', 'VERB', 'ADV']) # The keep_n parameter controls the size of the vocabulary. # At this stage, we have to manually experiment with various vocabulary sizes to see what works best. # I found that ~8-10% of the number of documents is a good size. # For Digital India, I used vocab size of 1000 (12412 documents). # For GST, I used a vocab size of 1500 (15k documents approx) print('Creating Dictionary') # Create Dictionary id2word = corpora.Dictionary(data_lemmatized) # Filter words id2word.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) # Lemmatized data is your corpus print('Converting corpus using dictionary') # Term Document Frequency corpus = [id2word.doc2bow(text) for text in data_lemmatized] # Save Data in pickle file with open(output_file, 'wb') as f: pickle.dump((data_lemmatized, id2word, corpus), f)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%202~lda_online_exp1.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.corpora import Dictionary from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 17850 STEP_SIZE = 200 # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # Initialize Parameters total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] # When updating Online LDA, if I use a normal dictionary I keep getting key errors. # That's why for online lda alone I use Hash Dictionary id2word = Dictionary(documents=data) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Obtain C_V Coherence of existing model in simulation coherencemodel2 = CoherenceModel( model=lda, texts=data, dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) count = 0 # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] pruned_docs = [] for doc in new_docs: pruned_data = [] for x in doc: if x in id2word.values(): pruned_data.append(x) pruned_docs.append(pruned_data) new_docs = pruned_docs print('Pruning Done') # Updating Dictionary # id2word.add_documents(new_docs) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 print('MODEL NO:'+str(count)) start = timeit.default_timer() lda.update(corpus) print('MODEL DONE') end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB coherencemodel2 = CoherenceModel( model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) with open('models_online.pkl', 'ab') as f2: pickle.dump( (count, lda, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%201~lda_online_exp1.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 17850 STEP_SIZE = 200 # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, id2word, _ = pickle.load(f) # Initialize Parameters total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] # When updating Online LDA, if I use a normal dictionary I keep getting key errors. # That's why for online lda alone I use Hash Dictionary # UPDATE: This method failed. # id2word = corpora.HashDictionary(documents=data) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Obtain C_V Coherence of existing model in simulation coherencemodel2 = CoherenceModel( model=lda, texts=data, dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) count = 0 # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] # Updating Dictionary # id2word.add_documents(new_docs) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 print('MODEL NO:'+str(count)) start = timeit.default_timer() lda.update(corpus) print('MODEL DONE') end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB coherencemodel2 = CoherenceModel( model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) # Saves the incrementally built up data just in case program fails in the middle. # Ideally you want the coherence_arr and time_arr from the last iteration. Other variables have been saved as a precaution. with open('models_online.pkl', 'ab') as f2: pickle.dump( (count, lda, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%203~.ipynb_checkpoints~lda_online_temporal-checkpoint.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import copy import gensim import gensim.corpora as corpora from gensim.corpora import Dictionary from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB # INITIAL_DOC_SIZE = 9442 INITIAL_DOC_SIZE = 4000 # STEP_SIZE = 200 # DOC_TEMPORAL_SIZE = [9442, 12710, 14339, 15445, 16287, 16998, 17849] # DOC_TEMPORAL_SIZE = [6730, 11328, 13466, # 14874, 15926, 16765, 17464, 18063, 18587, 19053] # DOC_TEMPORAL_SIZE = [6730, 9631, 11328, 12532, 13466, 14229, 14874, 15433, # 15926, 16366, 16765, 17129, 17464, 17775, 18063, 18333, 18587, 18826, 19053] # DOC_TEMPORAL_INCREMENT = [3268, 1629, 1106, # 842, 711, 851] # 2 months, 2 months DOC_TEMPORAL_INCREMENT = [4598, 2730, 2138, 1408, 1052, 839, 699, 599, 524, 466, 500, 500] # DOC_TEMPORAL_INCREMENT = [3268, 1629, 1106, # 842, 711, 851, 699, 599, 524, 466] # 2 weeks, 1 month # DOC_TEMPORAL_INCREMENT = [4598, 2138, 1408, 1052, # 839, 699, 599, 524, 466] # 2 weeks, 2 weeks # dummy_lst = [600]*4 # DOC_TEMPORAL_INCREMENT.extend(dummy_lst) # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # Initialize Parameters # total_time = 0 # coherence_arr = [] # time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] id2word = Dictionary(documents=data_lemmatized) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. # start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) corpus_baseline = copy.deepcopy(corpus) lda_baseline = copy.deepcopy(lda) doc_size = [] positive_arr = [] # end = timeit.default_timer() # time_taken = end - start # total_time += time_taken # time_arr.append(time_taken) # Obtain C_V Coherence of existing model in simulation # coherencemodel2 = CoherenceModel( # model=lda, texts=data, dictionary=id2word, coherence='c_v') # coherence_arr.append(coherencemodel2.get_coherence()) f2 = open('confusion_temporal_2.pkl', 'wb') count = 0 # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE # len(data_lemmatized)-STEP_SIZE doc_size_counter = INITIAL_DOC_SIZE for i in DOC_TEMPORAL_INCREMENT: # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[doc_size_counter:doc_size_counter+i] doc_size_counter += i # pruned_docs = [] # for doc in new_docs: # pruned_data = [] # for x in doc: # if x in id2word.values(): # pruned_data.append(x) # pruned_docs.append(pruned_data) # new_docs = pruned_docs # print('Pruning Done') # Updating Dictionary # id2word.add_documents(new_docs) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) prev_corpus = copy.deepcopy(corpus) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 # prev_lda = copy.deepcopy(lda) print('MODEL NO:'+str(count)) # start = timeit.default_timer() lda.update(corpus) print('MODEL DONE') # end = timeit.default_timer() # time_taken = end - start # total_time += time_taken # time_arr.append(time_taken) # updated_corpus = copy.deepcopy(prev_corpus) # updated_corpus.extend(corpus) # corpus = updated_corpus prev_corpus.extend(corpus) corpus = copy.deepcopy(prev_corpus) doc_size.append(i) positive_arr.append(he.calc_confusion_matrix( lda_baseline, lda, corpus)) # doc_size.append(len(corpus)) # positive_arr.append(he.calc_confusion_matrix( # prev_lda, lda, prev_corpus)) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB # coherencemodel2 = CoherenceModel( # model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') # coherence_arr.append(coherencemodel2.get_coherence()) # with open('models_online.pkl', 'ab') as f2: pickle.dump((positive_arr, doc_size), f2) # pickle.dump( # (count, lda, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) f2.close() # print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%202~calc_coherence_batch.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he file_name = 'models_batch.pkl' output = 'batch_coherence.pkl' model_list = [] data_list = [] dict_list = [] with open(file_name, 'rb') as f: while True: try: iteration, model, time_arr, data, id2word, _ = pickle.load(f) model_list.append(model) data_list.append(data) dict_list.append(id2word) except: break coherence_list = [] count = 0 for i in range(0, len(model_list)): model = model_list[i] data = data_list[i] id2word = dict_list[i] print(id2word) count += 1 print('Iteration '+str(count)) coherencemodel = CoherenceModel( model=model, texts=data, dictionary=id2word, coherence='c_v') coherence_list.append(coherencemodel.get_coherence()) with open(output, 'wb') as f: pickle.dump((coherence_list, time_arr), f)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%201~Archived~hdp_exp1.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 17850 STEP_SIZE = 200 # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, id2word, _ = pickle.load(f) # Initialize Parameters count = 0 total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] # id2word = corpora.HashDictionary(data) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. start = timeit.default_timer() hdpmodel, hdptopics = he.build_hdp(corpus, id2word) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) coherencemodel = CoherenceModel( model=hdpmodel, texts=data, dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel.get_coherence()) # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] # # Updating Dictionary # dict2 = corpora.Dictionary(new_docs) # id2word.merge_with(dict2) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 print('MODEL NO:'+str(count)) start = timeit.default_timer() # print(corpus) hdpmodel.update(corpus) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) coherencemodel = CoherenceModel( model=hdpmodel, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel.get_coherence()) # Saves the incrementally built up data just in case program fails in the middle. # Ideally you want the coherence_arr and time_arr from the last iteration. Other variables have been saved as a precaution. with open('models_hdp.pkl', 'ab') as f2: pickle.dump( (count, hdpmodel, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%202~.ipynb_checkpoints~lda_online_exp1-checkpoint.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.corpora import Dictionary from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 17850 STEP_SIZE = 200 # Load Data - corp.pkl contains data_lemmatized, id2word, corpus with open('corp.pkl', 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # Initialize Parameters total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] # When updating Online LDA, if I use a normal dictionary I keep getting key errors. # That's why for online lda alone I use Hash Dictionary id2word = Dictionary(documents=data) corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=False) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Obtain C_V Coherence of existing model in simulation coherencemodel2 = CoherenceModel( model=lda, texts=data, dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) count = 0 # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] pruned_docs = [] for doc in new_docs: pruned_data = [] for x in doc: if x in id2word.values(): pruned_data.append(x) pruned_docs.append(pruned_data) new_docs = pruned_docs print('Pruning Done') # Updating Dictionary # id2word.add_documents(new_docs) # id2word.filter_extremes(no_below=5, no_above=0.95, # keep_n=1800) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in new_docs] count += 1 print('MODEL NO:'+str(count)) start = timeit.default_timer() lda.update(corpus) print('MODEL DONE') end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB coherencemodel2 = CoherenceModel( model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') coherence_arr.append(coherencemodel2.get_coherence()) with open('models_online.pkl', 'ab') as f2: pickle.dump( (count, lda, coherence_arr, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
helper.py
import pandas as pd import numpy as np import pickle import re import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel from nltk.corpus import stopwords ''' A collection of helper functions ''' # Initialize spacy 'en' model, keeping only tagger component (for efficiency) nlp = spacy.load('en', disable=['parser', 'ner']) def sent_to_words(sentences): for sentence in sentences: # deacc=True removes punctuations yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) # Define functions for stopwords, bigrams, trigrams and lemmatization def remove_stopwords(texts, stop_words): return [[word for word in gensim.utils.simple_preprocess(str(doc)) if word not in stop_words] for doc in texts] def make_bigrams(texts, bigram_mod): return [bigram_mod[doc] for doc in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append( [token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out def create_dictionary(data_lemmatized): # Create Dictionary id2word = corpora.Dictionary(data_lemmatized) # Filter words id2word.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) return id2word def create_model_corpus(id2word, data_lemmatized): return [id2word.doc2bow(text) for text in data_lemmatized] def build_hdp(corpus, id2word): hdpmodel = HdpModel(corpus=corpus, id2word=id2word, chunksize=2000) hdptopics = hdpmodel.show_topics(formatted=False) hdptopics = [[word for word, prob in topic] for topicid, topic in hdptopics] return hdpmodel, hdptopics
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%202~lda_batch_exp1.py
import pandas as pd import numpy as np import pickle import re import timeit import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel, LdaModel, LdaMulticore from nltk.corpus import stopwords import helper as he # Number of documents to consider as part of existing model in simulation. # Here it is the no. of documents in Mass Media DB INITIAL_DOC_SIZE = 17850 STEP_SIZE = 200 # Load Data with open('corp.pkl', 'rb') as f: data_lemmatized, _, _ = pickle.load(f) # Initialize Parameters count = 0 total_time = 0 coherence_arr = [] time_arr = [] # Set Data State to that of existing model in simulation data = data_lemmatized[:INITIAL_DOC_SIZE] id2word = corpora.HashDictionary(data) # WARNING: Make sure that the dictionary and corpus are always in sync. # If the mappings in the dictionary and the ids in the corpus are different you will get KeyErrors. corpus = [id2word.doc2bow(doc) for doc in data] # Building for the first time - To be considered as the starting/existing model in simulation. start = timeit.default_timer() lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=True) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) with open('models_batch.pkl', 'ab') as f2: pickle.dump( (count, lda, time_arr, data, id2word, len(data)), f2) # The loop simulates arrival of new documents from Google Alerts in batches of STEP_SIZE for i in range(INITIAL_DOC_SIZE, len(data_lemmatized)-STEP_SIZE, STEP_SIZE): # new_docs is the list of STEP_SIZE new documents which have arrived new_docs = data_lemmatized[i:i+STEP_SIZE] # Updating Dictionary id2word.add_documents(new_docs) id2word.filter_extremes(no_below=5, no_above=0.95, keep_n=1800) # Converting Documents to doc2bow format so that they can be fed to models corpus = [id2word.doc2bow(doc) for doc in data_lemmatized[:i+STEP_SIZE]] count += 1 print('MODEL NO:'+str(count)) start = timeit.default_timer() # Since this is batch LDA the entire model has to be retrained from scratch using the new corpus and dictionary lda = LdaMulticore(corpus, num_topics=35, id2word=id2word, workers=3, chunksize=2000, passes=10, batch=True) end = timeit.default_timer() time_taken = end - start total_time += time_taken time_arr.append(time_taken) # Coherence of model updated so far on the entire corpus from start i.e including Mass Media DB # coherencemodel2 = CoherenceModel( # model=lda, texts=data_lemmatized[:i+STEP_SIZE], dictionary=id2word, coherence='c_v') # coherence_arr.append(coherencemodel2.get_coherence()) with open('models_batch.pkl', 'ab') as f2: pickle.dump( (count, lda, time_arr, data_lemmatized[:i+STEP_SIZE], id2word, i), f2) print(total_time)
[]
2024-01-10
adityanathan/Algorithmic-Auditing-of-News-Feeds
Experiment%203~helper.py
import pandas as pd import numpy as np import pickle import re import spacy import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel, HdpModel from nltk.corpus import stopwords # Initialize spacy 'en' model, keeping only tagger component (for efficiency) nlp = spacy.load('en', disable=['parser', 'ner']) def sent_to_words(sentences): for sentence in sentences: # deacc=True removes punctuations yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) def remove_stopwords(texts, stop_words): return [[word for word in gensim.utils.simple_preprocess(str(doc)) if word not in stop_words] for doc in texts] def make_bigrams(texts, bigram_mod): return [bigram_mod[doc] for doc in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append( [token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out def create_dictionary(data_lemmatized): # Create Dictionary id2word = corpora.Dictionary(data_lemmatized) # Filter words id2word.filter_extremes(no_below=5, no_above=0.95, keep_n=1800, keep_tokens=None) return id2word def create_model_corpus(id2word, data_lemmatized): return [id2word.doc2bow(text) for text in data_lemmatized] def build_hdp(corpus, id2word): hdpmodel = HdpModel(corpus=corpus, id2word=id2word, chunksize=2000) hdptopics = hdpmodel.show_topics(formatted=False) hdptopics = [[word for word, prob in topic] for topicid, topic in hdptopics] return hdpmodel, hdptopics def calc_confusion_matrix(model1, model2, corpus1, doc_max=True): lda_corpus_1 = [max(prob, key=lambda y:y[1]) for prob in model1[corpus1]] lda_corpus_2 = [max(prob, key=lambda y:y[1]) for prob in model2[corpus1]] # print(lda_corpus_1 == lda_corpus_2) # print('Corpus 1 Length - ', len(lda_corpus_1)) # print('Corpus 2 Length - ', len(lda_corpus_2)) # max_corpus_size = max(len(lda_corpus_1), len(lda_corpus_2)) # min_corpus_size = min(len(lda_corpus_1), len(lda_corpus_2)) # print(max_corpus_size) positive = 0 negative = 0 # if(doc_max == True): # upper_limit = max_corpus_size # else: # upper_limit = min_corpus_size upper_limit = len(lda_corpus_1) total_permutations = upper_limit * \ (upper_limit-1)/2 # nC2 combinations for i in range(upper_limit): for j in range(i+1, upper_limit): # print(upper_limit) # print(i) # print(j) if(lda_corpus_1[i][0] == lda_corpus_1[j][0] and lda_corpus_2[i][0] == lda_corpus_2[j][0]): positive = positive+1 elif(lda_corpus_1[i][0] != lda_corpus_1[j][0] and lda_corpus_2[i][0] == lda_corpus_2[j][0]): negative = negative+1 elif(lda_corpus_1[i][0] == lda_corpus_1[j][0] and lda_corpus_2[i][0] != lda_corpus_2[j][0]): negative = negative+1 elif(lda_corpus_1[i][0] != lda_corpus_1[j][0] and lda_corpus_2[i][0] != lda_corpus_2[j][0]): positive = positive+1 answers_positive = (round(positive*100/total_permutations, 2)) # answers_negative = (round(negative*100/total_permutations, 2)) return answers_positive
[]
2024-01-10
harrrshall/An-AI-Adventure
speak.py
import os from openai import OpenAI import base64 import json import time import simpleaudio as sa import errno from elevenlabs import generate, play, set_api_key, voices client = OpenAI() set_api_key(os.environ.get("ELEVENLABS_API_KEY")) def encode_image(image_path): while True: try: with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") except IOError as e: if e.errno != errno.EACCES: # Not a "file in use" error, re-raise raise # File is being written to, wait a bit and retry time.sleep(0.1) def play_audio(text): audio = generate(text, voice=os.environ.get("ELEVENLABS_VOICE_ID")) unique_id = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8").rstrip("=") dir_path = os.path.join("narration", unique_id) os.makedirs(dir_path, exist_ok=True) file_path = os.path.join(dir_path, "audio.wav") with open(file_path, "wb") as f: f.write(audio) play(audio) def generate_new_line(base64_image): return [ { "role": "user", "content": [ {"type": "text", "text": "Describe this image"}, { "type": "image_url", "image_url": f"data:image/jpeg;base64,{base64_image}", }, ], }, ] def analyze_image(base64_image, script): response = client.chat.completions.create( model="gpt-4-vision-preview", messages=[ { "role": "system", "content": """ Narrate the picture of the human as if it is a nature documentary. Make it snarky and funny. Don't repeat yourself. Make it short. If I do anything remotely interesting, make a big deal about it! """, }, ] + script + generate_new_line(base64_image), max_tokens=500, ) response_text = response.choices[0].message.content return response_text def main(): script = [] while True: # Path to your image image_path = os.path.join(os.getcwd(), "./captured_frames/frame.jpg") # Getting the base64 encoding base64_image = encode_image(image_path) # Analyze posture print("๐Ÿ‘€ David is watching...") analysis = analyze_image(base64_image, script=script) print("๐ŸŽ™๏ธ David says:") print(analysis) play_audio(analysis) script = script + [{"role": "assistant", "content": analysis}] # Wait for 5 seconds time.sleep(5) if __name__ == "__main__": main()
[ "[{'type': 'text', 'text': 'Describe this image'}, {'type': 'image_url', 'image_url': 'data:image/jpeg;base64,PLACEHOLDER'}]", "\n Narrate the picture of the human as if it is a nature documentary.\n Make it snarky and funny. Don't repeat yourself. Make it short. If I do anything remotely interesting, make a big deal about it!\n " ]
2024-01-10
NotShrirang/README-Generator
readme_generator.py
def ask(query): import openai import json with open("./secretes.json", "r") as f: json_file = json.load(f) openai.organization = "org-56LTi4oJG3ERd1cSlyN6sv2t" openai.api_key = json_file['api_key'] # print(query) response = openai.Completion.create( engine="text-davinci-003", prompt=query, temperature=0.5, max_tokens=1000, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0 ) return response['choices'][0]['text']
[]
2024-01-10
goonbamm/Co_Deep_Learning
code~gpt4~get_response.py
import os import time import json import openai import pandas as pd from tqdm.auto import tqdm # LOAD CONFIG with open('config.json', 'r', encoding='utf-8') as f: CONFIG = json.load(f) openai.api_key = CONFIG['OPENAI_API_KEY'] OPENAI_MODEL_NAME = CONFIG['OPENAI_MODEL_NAME'] def request_chatgpt_api(prompt): response = openai.ChatCompletion.create( model=OPENAI_MODEL_NAME, messages=[ {"role": "user", "content": prompt} ] ) id = response['id'] token_usage = response['usage']['total_tokens'] content = response['choices'][0]['message']['content'] return id, token_usage, content def get_prompt(requirement, model_type): assert MODEL_TYPE in [ 'GPT4, FULL INSTRUCTION, K=2, MANUAL', 'GPT4, FULL INSTRUCTION, K=2, RANDOM', 'GPT4, FULL INSTRUCTION, K=0', 'GPT4, MINIMAL INSTRUCTION, K=0', 'GPT4, NO INSTRUCTION, K=2, MANUAL', 'GPT4, NO INSTRUCTION, K=4, MANUAL', 'GPT4, NO INSTRUCTION, K=6, MANUAL' ] if model_type == 'GPT4, FULL INSTRUCTION, K=2, MANUAL': prompt_list = [ '์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\n', '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, FULL INSTRUCTION, K=0': prompt_list = [ '์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, FULL INSTRUCTION, K=2, RANDOM': prompt_list = [ '์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\n', '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: ๋ฏธ๋ž˜์—์„œ ์˜จ ์Šˆํผ ๊ฐ•์•„์ง€๋ฅผ ๊ทธ๋ ค๋ด. ๊ธˆ์ƒ‰ ํ”ผ๋ถ€์— ๊ทผ์œก์ด ๋งŽ์ด ๋ณด์ด๊ณ , ์ธ๊ฐ„์ฒ˜๋Ÿผ ๊ทผ์œก์ด ํŠผํŠผํ•œ ๋ชธ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด. ๋ฐฐ๊ฒฝ์€ ํฐ์ƒ‰์ด๊ณ  ๋ฏธ๋ž˜์ ์ด๊ณ  ์ดˆํ˜„์‹ค์ ์ธ ๋А๋‚Œ์„ ๋‚ด์ฃผ๋ฉด ์ข‹์•„. ๊ทธ๋ฆผ์€ ์ „์‹ ์„ ๋‹ค ๋ณด์—ฌ์ค˜์•ผ ํ•ด. ๊ทธ๋ฆฌ๊ณ  3D๋กœ ๊ทธ๋ ค์ค˜.', 'model prompt: Naรฏve Art American Bully dog, gold color skin, heavily ripped muscles, with human muscles,hulk type muscled body, white background, futuristic, Ultrarealistic, 8K, Photographic details, full body with legs, Unreal engine, 3D,\n', '์š”๊ตฌ์‚ฌํ•ญ: ์–ด๋‘ก๊ณ  ๋ฌด์„œ์šด ๋ถ„์œ„๊ธฐ์˜ ๋„์‹œ ์•ˆ์— ๋ฒ„๋ ค์ง„ ๋†๊ตฌ์žฅ ์‚ฌ์ง„์„ ์ฐ์–ด๋ด. ๋นจ๊ฐ„ ๋น›์œผ๋กœ ์–ด๋‘์šด ๊ทธ๋ฆผ์ž๊ฐ€ ๋“œ๋ฆฌ์›Œ์ ธ ์žˆ๊ณ , ๋…น์Šจ ๋ฐ”์Šค์ผ“์ด ์“ธ์“ธํ•˜๊ฒŒ ๋งค๋‹ฌ๋ ค์žˆ์–ด. ๊ทธ๋ฆฌ๊ณ  ๋†๊ตฌ์žฅ์—๋Š” ํ•œ๋•Œ ํ™œ๊ธฐ์ฐจ๊ฒŒ ๊ฒฝ๊ธฐ๊ฐ€ ๋ฒŒ์–ด์กŒ๋˜ ํ”์ ์ด ๋ณด์—ฌ์•ผ ํ•ด. ๋†๊ตฌ์žฅ์€ ๋‚ก์•„์„œ ์„ ๊ณผ ๊ทธ๋ž˜ํ”ฝ์ด ํ๋ฆฌ๊ณ  ๋ฒ—๊ฒจ์ ธ ์žˆ์–ด. ์นด๋ฉ”๋ผ๋Š” ์…”ํ„ฐ ์Šคํ”ผ๋“œ 1/200, ์กฐ๋ฆฌ๊ฐœ f/2.8๋กœ ์„ค์ •ํ•˜๊ณ , ISO๋Š” 800์œผ๋กœ ์„ค์ •ํ•ด. ๊ทธ๋ฆผ ์ „์ฒด์ ์œผ๋กœ ์œ ๋ น ๊ฐ™๊ณ  ์˜ํ™” ๊ฐ™์€ ๋А๋‚Œ์„ ์ฃผ๊ณ , ์žŠํ˜€์ง„ ๋„์‹œ์˜ ์žŠํ˜€์ง„ ๊ณต๊ฐ„์˜ ์•„๋ฆ„๋‹ค์›€๊ณผ ์• ์ˆ˜๋ฅผ ๋‹ด์•„๋‚ด์•ผ ํ•ด. ๊ทธ๋ฆผ์˜ ๊ตฌ๋„๋Š” ๋†๊ตฌ์žฅ์— ์ฃผ๋ชฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์‹ ์ค‘ํ•˜๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ์‹œ๊ฐ„์˜ ํ๋ฆ„๊ณผ ์ธ๊ฐ„์˜ ๋…ธ๋ ฅ์˜ ์ผ์‹œ์ ์ธ ์„ฑ๊ฒฉ์— ๋Œ€ํ•œ ์ƒ๊ฐ์„ ์ด๋Œ์–ด๋‚ด์•ผ ํ•ด.', 'model prompt: Date A hauntingly beautiful photograph of an abandoned basketball court in the heart of a run-down, futuristic city, captured with a Sony A7R IV camera and a 35mm f/1.4 lens. The court is the sole focus of the image, with the surrounding cityscape in the background shrouded in deep shadows and ominous red lighting, casting an eerie glow over the scene. The court is worn and weathered, its once-vibrant lines and graphics now faded and peeling. The rusted hoops hang forlornly from the backboards, a testament to a bygone era when the court was a hub of activity and competition. Despite its derelict state, the court still exudes a sense of energy and possibility, a reminder of the dreams and aspirations that were once realized on its surface. The camera is set up with an aperture of f/2.8 to create a shallow depth of field, highlighting the intricate details of the court and blurring the background to create a sense of isolation and abandonment. The ISO is set to 800 to capture the dramatic red lighting that bathes the court, casting deep shadows and emphasizing the texture and detail of the surface. The overall effect is haunting and cinematic, capturing the beauty and melancholy of a forgotten space in a forgotten city. The composition is carefully crafted to draw the viewers eye to the court, emphasizing its significance and inviting reflection on the passage of time and the transience of human endeavor.\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, MINIMAL INSTRUCTION, K=0': prompt_list = [ '์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, NO INSTRUCTION, K=2, MANUAL': prompt_list = [ '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, NO INSTRUCTION, K=4, MANUAL': prompt_list = [ '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\n', '์š”๊ตฌ์‚ฌํ•ญ: ์—ฌ๋ฆ„ ๊ทธ๋ž˜ํ”ฝ, ๋กœ๋“œ ํŠธ๋ฆฝ ์–ด๋“œ๋ฒค์ฒ˜, ๊ณ„ํš์ž ์•ก์„ธ์„œ๋ฆฌ, ๋ ˆํŠธ๋กœ ์ปฌ๋Ÿฌ, ์ˆ˜์ œ ๊ทธ๋ฆผ, ์—ฌํ–‰ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜, ์ˆ˜์ฑ„ํ™” ์š”์†Œ๊ฐ€ ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Topology Summer Graphics, Road Trip Adventure, Planner Accessories, Retro Colors, Hand-Drawn Artwork, Travel Illustrations, Watercolor Elements, single design, 8k, \n', '์š”๊ตฌ์‚ฌํ•ญ: ํด๋กœ๋“œ ๋ชจ๋„ค ๊ฐ™์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์•„๋ฆ„๋‹ค์šด ์ผ๋ชฐ์ด ์žˆ๋Š” ๊ธธ๊ฐ€์— ๊ธด ๋ธŒ๋ ˆ์ด๋“œ๋ฅผ ํ•œ ์ž‘์€ ์†Œ๋…€๊ฐ€ ํฐ ๋ฐ”์œ„ ์œ„์— ์•‰์•„์žˆ์–ด. ๊ทธ๋…€๋Š” ๋ฉ€๋ฆฌ ๋ฐ”๋ผ๋ณด๋ฉฐ ์ƒ๊ฐ์— ์ž ๊ธฐ๊ณ  ์žˆ์–ด.', 'model prompt: Nature Photography A little girl with long braids sits on a large boulder at the end of a linden-lined avenue, with a beautiful sunset in the background. She gazes off into the distance, lost in thought as she takes in the stunning view before her, in the style of Claude Monet, \n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) elif model_type == 'GPT4, NO INSTRUCTION, K=6, MANUAL': prompt_list = [ '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\n', '์š”๊ตฌ์‚ฌํ•ญ: ์—ฌ๋ฆ„ ๊ทธ๋ž˜ํ”ฝ, ๋กœ๋“œ ํŠธ๋ฆฝ ์–ด๋“œ๋ฒค์ฒ˜, ๊ณ„ํš์ž ์•ก์„ธ์„œ๋ฆฌ, ๋ ˆํŠธ๋กœ ์ปฌ๋Ÿฌ, ์ˆ˜์ œ ๊ทธ๋ฆผ, ์—ฌํ–‰ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜, ์ˆ˜์ฑ„ํ™” ์š”์†Œ๊ฐ€ ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Topology Summer Graphics, Road Trip Adventure, Planner Accessories, Retro Colors, Hand-Drawn Artwork, Travel Illustrations, Watercolor Elements, single design, 8k, \n', '์š”๊ตฌ์‚ฌํ•ญ: ํด๋กœ๋“œ ๋ชจ๋„ค ๊ฐ™์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์•„๋ฆ„๋‹ค์šด ์ผ๋ชฐ์ด ์žˆ๋Š” ๊ธธ๊ฐ€์— ๊ธด ๋ธŒ๋ ˆ์ด๋“œ๋ฅผ ํ•œ ์ž‘์€ ์†Œ๋…€๊ฐ€ ํฐ ๋ฐ”์œ„ ์œ„์— ์•‰์•„์žˆ์–ด. ๊ทธ๋…€๋Š” ๋ฉ€๋ฆฌ ๋ฐ”๋ผ๋ณด๋ฉฐ ์ƒ๊ฐ์— ์ž ๊ธฐ๊ณ  ์žˆ์–ด.', 'model prompt: Nature Photography A little girl with long braids sits on a large boulder at the end of a linden-lined avenue, with a beautiful sunset in the background. She gazes off into the distance, lost in thought as she takes in the stunning view before her, in the style of Claude Monet, \n', '์š”๊ตฌ์‚ฌํ•ญ: ์‚ฐ ๊ผญ๋Œ€๊ธฐ ๋™๊ตด์—์„œ ๋ณด๋Š” 4K ์• ๋‹ˆ๋ฉ”์ด์…˜ ์Šคํƒ€์ผ์˜ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ๊ทธ๋ฆฌ๊ณ  ์ŠคํŒŒ์ด๋”๋งจ ์˜†์—๋Š” ๋ฐ”์œ„ ์œ„์— ์•‰์•„ ๋™๊ตด ์ž…๊ตฌ๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ๊ท€์—ฌ์šด ๊ณ ์–‘์ด๊ฐ€ ์žˆ์–ด. ๋„์ฟ„์˜ ๋น„์˜ค๋Š” ๋ฐค ๊ฑฐ๋ฆฌ์™€ ๋ฐ์€ ๋„ค์˜จ ๋ผ์ดํŠธ, ๊ทธ๋ฆฌ๊ณ  ๋น„ํ–‰ํ•˜๋Š” ์ŠคํŒ€ํŽ‘ํฌ ๋ฐฐ๋“ค์ด ๋‚˜์˜ค๋Š” ์žฅ๋ฉด์ด ์žˆ์–ด.', 'model prompt: 4K lofi anime style from the view of a mountain top cave with spiderman next to hir is a cute cat sitting on the rocks looking out of the a circle cave entrance with a night scene of rainy tokyo streets with bright neon lights and flying steampunk ships, \n', '์š”๊ตฌ์‚ฌํ•ญ: ํฐ ๋™๋ฌผ์› ์ž…๊ตฌ๋ฅผ ๋‘๊บผ์šด ํŽ˜์ธํŠธ๋กœ ๊ทธ๋ ค๋ด. ์ž…์ฒด์ ์œผ๋กœ ๊ทธ๋ ค์ค˜.', 'model prompt: Impasto a grand zoo entrance, isometric,\n', f'์š”๊ตฌ์‚ฌํ•ญ: {requirement}', 'model prompt: ' ] prompt = '\n'.join(prompt_list) return prompt def main(model_type): # LOAD DATASET test_dataset = pd.read_csv('../evaluation/final_test_128.csv', index_col=False) # FOR SAVING openai_dict = { 'id': list(), 'token_usage': list(), 'content': list() } # READY FOR TQDM progress_bar = tqdm(total=len(test_dataset), desc=model_type) for i, row in test_dataset.iterrows(): # GET PROMPT prompt = get_prompt(row['requirement'], model_type) # TRY TO REQUEST OPENAI API try: id, token_usage, content = request_chatgpt_api(prompt) openai_dict['id'].append(id) openai_dict['token_usage'].append(token_usage) openai_dict['content'].append(content) except Exception as e: print(f'#{i} DATA ERROR!') print(f'BECAUSE WE HAVE ERROR, WE SLEEP 5 MINUTES FROM NOW.\n') time.sleep(300) progress_bar.update(1) gpt4_requests = pd.DataFrame.from_dict(openai_dict) record = pd.read_csv('../evaluation/record.csv', index_col=False) record[model_type] = gpt4_requests['content'] record.to_csv('../evaluation/record.csv', index=False, encoding='utf-8') if __name__ == '__main__': MODEL_TYPE = 'GPT4, NO INSTRUCTION, K=6, MANUAL' main(model_type=MODEL_TYPE)
[ "['์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "\n", "['์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\\n', '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: ๋ฏธ๋ž˜์—์„œ ์˜จ ์Šˆํผ ๊ฐ•์•„์ง€๋ฅผ ๊ทธ๋ ค๋ด. ๊ธˆ์ƒ‰ ํ”ผ๋ถ€์— ๊ทผ์œก์ด ๋งŽ์ด ๋ณด์ด๊ณ , ์ธ๊ฐ„์ฒ˜๋Ÿผ ๊ทผ์œก์ด ํŠผํŠผํ•œ ๋ชธ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด. ๋ฐฐ๊ฒฝ์€ ํฐ์ƒ‰์ด๊ณ  ๋ฏธ๋ž˜์ ์ด๊ณ  ์ดˆํ˜„์‹ค์ ์ธ ๋А๋‚Œ์„ ๋‚ด์ฃผ๋ฉด ์ข‹์•„. ๊ทธ๋ฆผ์€ ์ „์‹ ์„ ๋‹ค ๋ณด์—ฌ์ค˜์•ผ ํ•ด. ๊ทธ๋ฆฌ๊ณ  3D๋กœ ๊ทธ๋ ค์ค˜.', 'model prompt: Naรฏve Art American Bully dog, gold color skin, heavily ripped muscles, with human muscles,hulk type muscled body, white background, futuristic, Ultrarealistic, 8K, Photographic details, full body with legs, Unreal engine, 3D,\\n', '์š”๊ตฌ์‚ฌํ•ญ: ์–ด๋‘ก๊ณ  ๋ฌด์„œ์šด ๋ถ„์œ„๊ธฐ์˜ ๋„์‹œ ์•ˆ์— ๋ฒ„๋ ค์ง„ ๋†๊ตฌ์žฅ ์‚ฌ์ง„์„ ์ฐ์–ด๋ด. ๋นจ๊ฐ„ ๋น›์œผ๋กœ ์–ด๋‘์šด ๊ทธ๋ฆผ์ž๊ฐ€ ๋“œ๋ฆฌ์›Œ์ ธ ์žˆ๊ณ , ๋…น์Šจ ๋ฐ”์Šค์ผ“์ด ์“ธ์“ธํ•˜๊ฒŒ ๋งค๋‹ฌ๋ ค์žˆ์–ด. ๊ทธ๋ฆฌ๊ณ  ๋†๊ตฌ์žฅ์—๋Š” ํ•œ๋•Œ ํ™œ๊ธฐ์ฐจ๊ฒŒ ๊ฒฝ๊ธฐ๊ฐ€ ๋ฒŒ์–ด์กŒ๋˜ ํ”์ ์ด ๋ณด์—ฌ์•ผ ํ•ด. ๋†๊ตฌ์žฅ์€ ๋‚ก์•„์„œ ์„ ๊ณผ ๊ทธ๋ž˜ํ”ฝ์ด ํ๋ฆฌ๊ณ  ๋ฒ—๊ฒจ์ ธ ์žˆ์–ด. ์นด๋ฉ”๋ผ๋Š” ์…”ํ„ฐ ์Šคํ”ผ๋“œ 1/200, ์กฐ๋ฆฌ๊ฐœ f/2.8๋กœ ์„ค์ •ํ•˜๊ณ , ISO๋Š” 800์œผ๋กœ ์„ค์ •ํ•ด. ๊ทธ๋ฆผ ์ „์ฒด์ ์œผ๋กœ ์œ ๋ น ๊ฐ™๊ณ  ์˜ํ™” ๊ฐ™์€ ๋А๋‚Œ์„ ์ฃผ๊ณ , ์žŠํ˜€์ง„ ๋„์‹œ์˜ ์žŠํ˜€์ง„ ๊ณต๊ฐ„์˜ ์•„๋ฆ„๋‹ค์›€๊ณผ ์• ์ˆ˜๋ฅผ ๋‹ด์•„๋‚ด์•ผ ํ•ด. ๊ทธ๋ฆผ์˜ ๊ตฌ๋„๋Š” ๋†๊ตฌ์žฅ์— ์ฃผ๋ชฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์‹ ์ค‘ํ•˜๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ์‹œ๊ฐ„์˜ ํ๋ฆ„๊ณผ ์ธ๊ฐ„์˜ ๋…ธ๋ ฅ์˜ ์ผ์‹œ์ ์ธ ์„ฑ๊ฒฉ์— ๋Œ€ํ•œ ์ƒ๊ฐ์„ ์ด๋Œ์–ด๋‚ด์•ผ ํ•ด.', 'model prompt: Date A hauntingly beautiful photograph of an abandoned basketball court in the heart of a run-down, futuristic city, captured with a Sony A7R IV camera and a 35mm f/1.4 lens. The court is the sole focus of the image, with the surrounding cityscape in the background shrouded in deep shadows and ominous red lighting, casting an eerie glow over the scene. The court is worn and weathered, its once-vibrant lines and graphics now faded and peeling. The rusted hoops hang forlornly from the backboards, a testament to a bygone era when the court was a hub of activity and competition. Despite its derelict state, the court still exudes a sense of energy and possibility, a reminder of the dreams and aspirations that were once realized on its surface. The camera is set up with an aperture of f/2.8 to create a shallow depth of field, highlighting the intricate details of the court and blurring the background to create a sense of isolation and abandonment. The ISO is set to 800 to capture the dramatic red lighting that bathes the court, casting deep shadows and emphasizing the texture and detail of the surface. The overall effect is haunting and cinematic, capturing the beauty and melancholy of a forgotten space in a forgotten city. The composition is carefully crafted to draw the viewers eye to the court, emphasizing its significance and inviting reflection on the passage of time and the transience of human endeavor.\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "['์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\\n', '์š”๊ตฌ์‚ฌํ•ญ: ์—ฌ๋ฆ„ ๊ทธ๋ž˜ํ”ฝ, ๋กœ๋“œ ํŠธ๋ฆฝ ์–ด๋“œ๋ฒค์ฒ˜, ๊ณ„ํš์ž ์•ก์„ธ์„œ๋ฆฌ, ๋ ˆํŠธ๋กœ ์ปฌ๋Ÿฌ, ์ˆ˜์ œ ๊ทธ๋ฆผ, ์—ฌํ–‰ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜, ์ˆ˜์ฑ„ํ™” ์š”์†Œ๊ฐ€ ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Topology Summer Graphics, Road Trip Adventure, Planner Accessories, Retro Colors, Hand-Drawn Artwork, Travel Illustrations, Watercolor Elements, single design, 8k, \\n', '์š”๊ตฌ์‚ฌํ•ญ: ํด๋กœ๋“œ ๋ชจ๋„ค ๊ฐ™์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์•„๋ฆ„๋‹ค์šด ์ผ๋ชฐ์ด ์žˆ๋Š” ๊ธธ๊ฐ€์— ๊ธด ๋ธŒ๋ ˆ์ด๋“œ๋ฅผ ํ•œ ์ž‘์€ ์†Œ๋…€๊ฐ€ ํฐ ๋ฐ”์œ„ ์œ„์— ์•‰์•„์žˆ์–ด. ๊ทธ๋…€๋Š” ๋ฉ€๋ฆฌ ๋ฐ”๋ผ๋ณด๋ฉฐ ์ƒ๊ฐ์— ์ž ๊ธฐ๊ณ  ์žˆ์–ด.', 'model prompt: Nature Photography A little girl with long braids sits on a large boulder at the end of a linden-lined avenue, with a beautiful sunset in the background. She gazes off into the distance, lost in thought as she takes in the stunning view before her, in the style of Claude Monet, \\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "requirement", "['์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "['์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\\n', '์š”๊ตฌ์‚ฌํ•ญ: ์—ฌ๋ฆ„ ๊ทธ๋ž˜ํ”ฝ, ๋กœ๋“œ ํŠธ๋ฆฝ ์–ด๋“œ๋ฒค์ฒ˜, ๊ณ„ํš์ž ์•ก์„ธ์„œ๋ฆฌ, ๋ ˆํŠธ๋กœ ์ปฌ๋Ÿฌ, ์ˆ˜์ œ ๊ทธ๋ฆผ, ์—ฌํ–‰ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜, ์ˆ˜์ฑ„ํ™” ์š”์†Œ๊ฐ€ ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Topology Summer Graphics, Road Trip Adventure, Planner Accessories, Retro Colors, Hand-Drawn Artwork, Travel Illustrations, Watercolor Elements, single design, 8k, \\n', '์š”๊ตฌ์‚ฌํ•ญ: ํด๋กœ๋“œ ๋ชจ๋„ค ๊ฐ™์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์•„๋ฆ„๋‹ค์šด ์ผ๋ชฐ์ด ์žˆ๋Š” ๊ธธ๊ฐ€์— ๊ธด ๋ธŒ๋ ˆ์ด๋“œ๋ฅผ ํ•œ ์ž‘์€ ์†Œ๋…€๊ฐ€ ํฐ ๋ฐ”์œ„ ์œ„์— ์•‰์•„์žˆ์–ด. ๊ทธ๋…€๋Š” ๋ฉ€๋ฆฌ ๋ฐ”๋ผ๋ณด๋ฉฐ ์ƒ๊ฐ์— ์ž ๊ธฐ๊ณ  ์žˆ์–ด.', 'model prompt: Nature Photography A little girl with long braids sits on a large boulder at the end of a linden-lined avenue, with a beautiful sunset in the background. She gazes off into the distance, lost in thought as she takes in the stunning view before her, in the style of Claude Monet, \\n', '์š”๊ตฌ์‚ฌํ•ญ: ์‚ฐ ๊ผญ๋Œ€๊ธฐ ๋™๊ตด์—์„œ ๋ณด๋Š” 4K ์• ๋‹ˆ๋ฉ”์ด์…˜ ์Šคํƒ€์ผ์˜ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ๊ทธ๋ฆฌ๊ณ  ์ŠคํŒŒ์ด๋”๋งจ ์˜†์—๋Š” ๋ฐ”์œ„ ์œ„์— ์•‰์•„ ๋™๊ตด ์ž…๊ตฌ๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ๊ท€์—ฌ์šด ๊ณ ์–‘์ด๊ฐ€ ์žˆ์–ด. ๋„์ฟ„์˜ ๋น„์˜ค๋Š” ๋ฐค ๊ฑฐ๋ฆฌ์™€ ๋ฐ์€ ๋„ค์˜จ ๋ผ์ดํŠธ, ๊ทธ๋ฆฌ๊ณ  ๋น„ํ–‰ํ•˜๋Š” ์ŠคํŒ€ํŽ‘ํฌ ๋ฐฐ๋“ค์ด ๋‚˜์˜ค๋Š” ์žฅ๋ฉด์ด ์žˆ์–ด.', 'model prompt: 4K lofi anime style from the view of a mountain top cave with spiderman next to hir is a cute cat sitting on the rocks looking out of the a circle cave entrance with a night scene of rainy tokyo streets with bright neon lights and flying steampunk ships, \\n', '์š”๊ตฌ์‚ฌํ•ญ: ํฐ ๋™๋ฌผ์› ์ž…๊ตฌ๋ฅผ ๋‘๊บผ์šด ํŽ˜์ธํŠธ๋กœ ๊ทธ๋ ค๋ด. ์ž…์ฒด์ ์œผ๋กœ ๊ทธ๋ ค์ค˜.', 'model prompt: Impasto a grand zoo entrance, isometric,\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "['์š”๊ตฌ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๊ทธ๋ฆผ ์ƒ์„ฑ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”์ค˜.', '- ๊ทธ๋ฆผ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋ช…์‚ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•ด.\\n', '์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']", "[]", "['์˜ˆ์‹œ)', '์š”๊ตฌ์‚ฌํ•ญ: 1800๋…„๋Œ€ ์Šคํƒ€์ผ๋กœ ์•„ํ‹€๋ผ์Šค๋ฅผ ๊ทธ๋ฆฐ ์Šค์ผ€์น˜๋ฅผ ํ•˜์–€ ๋ฐฐ๊ฒฝ์— ๋ช‡ ๋งˆ๋ฆฌ์˜ ๋…์ˆ˜๋ฆฌ๊ฐ€ ์ฃผ๋ณ€์— ๋‚ ๊ณ  ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด.', 'model prompt: Tethering vintage sketch of Atlas with a few eagles flying around him, on a white backdrop, in the style of 1800s\\n', '์š”๊ตฌ์‚ฌํ•ญ: 1920๋…„๋Œ€ ๋ฉ‹์ง„ ๋ผํ‹ด ๋‚จ์ž๊ฐ€ ๋ฐ˜์ง์ด๋Š” ์€์ƒ‰ ์ˆ˜ํŠธ๋ฅผ ์ž…๊ณ  ์–ด๋‘์šด ์—ฐ๊ธฐ ๊ฐ€๋“ํ•œ ๋ฐ”์—์„œ ์Œ๋ฃŒ๋ฅผ ๋“ค๊ณ  ๊ธฐ๋Œ€์–ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ด. ์ดˆ์ž์—ฐ์ ์ด๊ณ  ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”ฝ ๋…ธ๋ธ” ์ฝ”๋ฏน๋ถ ์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ด์…˜ ๊ฐ™์ด, ์ปฌ๋Ÿฌํ’€ํ•˜๊ฒŒ ๊ทธ๋ ค์ค˜. ๋งˆ์ดํฌ ๋ฏธ๋‡Œ๋ผ, ํ…Œ๋ฆฌ ๋„๋“œ์Šจ, ํ† ๋จธ ํ•˜๋ˆ„์นด๊ฐ€ ๋งŒ๋“  ์บ๋ฆญํ„ฐ ์•„ํŠธ ์Šคํƒ€์ผ๋กœ ํ•ด์ค˜.', 'model prompt: Sand Dune 1920s handsome latino man in shiny silver suit, leaning with a drink in a dark smoky bar, supernatural, highly detailed realistic graphic novel comic book illustration, colorful, by mike mignola, terry dodson and tomer hanuka, character art\\n', '์š”๊ตฌ์‚ฌํ•ญ: PLACEHOLDER', 'model prompt: ']" ]
2024-01-10
SUBITSHA-M/MCA_Generator
mca_questions.py
import os import re import json import openai # To help construct our Chat Messages from langchain.schema import HumanMessage from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate # We will be using ChatGPT model (gpt-3.5-turbo) from langchain.chat_models import ChatOpenAI # To parse outputs and get structured data back from langchain.output_parsers import StructuredOutputParser, ResponseSchema from transformers import BartForConditionalGeneration, BartTokenizer, BartConfig # Enter your API Key os.environ["OPENAI_API_KEY"] = "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" # prompt text = """ The Lancefield-Cobaw Croziers Track planned burn was ignited on Wednesday 30 September 2015 in the Macedon Ranges Shire in spring 2015. The fires breached containment lines on Saturday 3 October and was brought under control overnight with approximately 70 additional hectares burnt. Further breaches of containment lines occurred on Tuesday 6 October and control of the bushfire was transferred from the Midlands District to the Gisborne Incident Control Centre (ICC) that afternoon. The bushfire, when finally contained on Tuesday 13 October, had burnt over 3,000ha and destroyed several dwellings, numerous sheds and many kilometres of fencing. It had also impacted upon lifestyles, livestock and livelihoods and caused considerable economic and social upheaval in the surrounding communities. """ from transformers import pipeline def get_mca_questions(text:str)->list: summarizer = pipeline("summarization") summary = summarizer(text, max_length=150, min_length=30, do_sample=False) print(summary[0]['summary_text']) response_schemas = [ ResponseSchema(name="question", description="A multiple choice question generated from input text snippet."), ResponseSchema(name="options", description="Four choices for the multiple choice question."), ResponseSchema(name="answer", description="Two Correct answers for the question.") ] # The parser that will look for the LLM output in my schema and return it back to me output_parser = StructuredOutputParser.from_response_schemas(response_schemas) # The format instructions that LangChain makes. Let's look at them format_instructions = output_parser.get_format_instructions() print(format_instructions) # create ChatGPT object chat_model = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo') # The prompt template that brings it all together prompt = ChatPromptTemplate( messages=[ HumanMessagePromptTemplate.from_template("""Generate multiple-choice questions (MCQs) with four options for each question. Please ensure that two of the options are correct answers and also each question must have serial number . You should Provide the output in the form of a list. \n{format_instructions}\n{user_prompt}""") ], input_variables=["user_prompt"], partial_variables={"format_instructions": format_instructions} ) user_query = prompt.format_prompt(user_prompt = summary) user_query_output = chat_model(user_query.to_messages()) print(user_query_output) markdown_text = user_query_output.content json_string = re.search(r'{(.*?)}', markdown_text, re.DOTALL).group(1) print(json_string) options_list = json_string.split('\n') question = options_list[0].strip() answer = options_list[-1].strip() options = [opt.strip() for opt in options_list[1:-1] if opt.strip()] question_data = [question] + options + [answer] print(question_data) return question_data get_mca_questions(text)
[ "user_prompt", "format_instructions", "Generate multiple-choice questions (MCQs) with four options for each question. Please ensure that two of the options are correct answers and also each question must have serial number . You should Provide the output in the form of a list. \n \n{format_instructions}\n{user_prompt}" ]