Spaces:
Runtime error
Runtime error
from langchain.docstore.document import Document | |
from langchain.vectorstores import FAISS | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.memory.simple import SimpleMemory | |
from langchain.chains import ConversationChain, LLMChain, SequentialChain | |
from langchain.memory import ConversationBufferMemory | |
from langchain.prompts import ChatPromptTemplate, PromptTemplate | |
from langchain.document_loaders import UnstructuredFileLoader | |
from langchain.chat_models import ChatOpenAI | |
from langchain.llms import OpenAI | |
from langchain.memory import ConversationSummaryMemory | |
from langchain.callbacks import PromptLayerCallbackHandler | |
from langchain.prompts.chat import ( | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
AIMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
from langchain.schema import AIMessage, HumanMessage, SystemMessage | |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler | |
from langchain.callbacks.base import BaseCallbackHandler | |
import gradio as gr | |
from threading import Thread | |
from queue import Queue, Empty | |
from threading import Thread | |
from collections.abc import Generator | |
from langchain.llms import OpenAI | |
from langchain.callbacks.base import BaseCallbackHandler | |
import itertools | |
import time | |
import os | |
import getpass | |
import json | |
import sys | |
from typing import Any, Dict, List, Union | |
import promptlayer | |
import openai | |
import gradio as gr | |
from pydantic import BaseModel, Field, validator | |
#Load the FAISS Model ( vector ) | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
db = FAISS.load_local("db", OpenAIEmbeddings()) | |
#API Keys | |
promptlayer.api_key = os.environ["PROMPTLAYER"] | |
MODEL = "gpt-3.5-turbo" | |
# MODEL = "gpt-4" | |
from langchain.callbacks import PromptLayerCallbackHandler | |
from langchain.prompts.chat import ( | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
AIMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
from langchain.memory import ConversationSummaryMemory | |
# Defined a QueueCallback, which takes as a Queue object during initialization. Each new token is pushed to the queue. | |
class QueueCallback(BaseCallbackHandler): | |
"""Callback handler for streaming LLM responses to a queue.""" | |
def __init__(self, q): | |
self.q = q | |
def on_llm_new_token(self, token: str, **kwargs: Any) -> None: | |
self.q.put(token) | |
def on_llm_end(self, *args, **kwargs: Any) -> None: | |
return self.q.empty() | |
MODEL = "gpt-3.5-turbo-16k" | |
# Defined a QueueCallback, which takes as a Queue object during initialization. Each new token is pushed to the queue. | |
class QueueCallback(BaseCallbackHandler): | |
"""Callback handler for streaming LLM responses to a queue.""" | |
def __init__(self, q): | |
self.q = q | |
def on_llm_new_token(self, token: str, **kwargs: Any) -> None: | |
self.q.put(token) | |
def on_llm_end(self, *args, **kwargs: Any) -> None: | |
return self.q.empty() | |
class UnitGenerator: | |
def __init__(self, prompt_template='', model_name=MODEL, verbose=False, temp=0.2): | |
self.verbose = verbose | |
self.llm = ChatOpenAI( | |
model_name=MODEL, | |
temperature=temp | |
) | |
#The zero shot prompt provided at creation | |
self.prompt_template = prompt_template | |
def chain(self, prompt: PromptTemplate, llm: ChatOpenAI) -> LLMChain: | |
return LLMChain( | |
llm=llm, | |
prompt=prompt, | |
verbose=self.verbose, | |
) | |
def stream(self, input) -> Generator: | |
# Create a Queue | |
q = Queue() | |
job_done = object() | |
llm = ChatOpenAI( | |
model_name=MODEL, | |
callbacks=[QueueCallback(q), | |
PromptLayerCallbackHandler(pl_tags=["unit-generator"])], | |
streaming=True, | |
) | |
prompt = PromptTemplate( | |
input_variables=['input'], | |
template=self.prompt_template | |
) | |
# Create a funciton to call - this will run in a thread | |
def task(): | |
resp = self.chain(prompt,llm).run( | |
{'input':input}) | |
q.put(job_done) | |
# Create a thread and start the function | |
t = Thread(target=task) | |
t.start() | |
content = "" | |
# Get each new token from the queue and yield for our generator | |
while True: | |
try: | |
next_token = q.get(True, timeout=1) | |
if next_token is job_done: | |
break | |
content += next_token | |
yield next_token, content | |
except Empty: | |
continue | |
agent_prompt = """ | |
Take the following class overview (delimited by three dollar signs) | |
$$$ | |
{input} | |
$$$ | |
The following is a "portrait of a graduate" (delimited by three hashtags). | |
### | |
CHARACTER & INTEGRITY | |
β’ Honest: | |
β Truthful and transparent in all interactions, both in word and action. | |
β’ Responsible: | |
β Accounts for one's actions and decisions and takes responsibility for the | |
consequences that arise from them. | |
β’ Respectful: | |
β Treats others with dignity and compassion while valuing their opinions | |
and perspectives. | |
β’ Fair: | |
β Acts with impartiality and treats all individuals equitably and justly. | |
β Applies the tenets of sportsmanship and fair play to all endeavors | |
β KNOWLEDGE & INQUIRY | |
β’ Curious: | |
β Possesses a natural curiosity and a desire to learn. | |
β Asks questions, seeks answers, and explores new ideas. | |
β’ Broad-minded: | |
β Considers different points of view and revises their own beliefs and | |
opinions as they encounter new information and unexpected challenges. | |
β Understands that meaningful learning includes wellness of mind and body | |
β’ Determined: | |
β Purposeful, persistent, and willing to work through complex and difficult | |
challenges to achieve their goals. | |
β’ Innovative and Creative: | |
β Thinks differently and cultivates innovative perspectives, outside of those | |
traditionally accepted | |
β CITIZENSHIP & CIVILITY | |
β’ Participates: | |
β Actively listens to others, genuinely hearing their perspectives. | |
β Communicates perspectives with tact and respect for others' opinions | |
β’ Ethical: | |
β Considers the interests and the well-being of the community, nation, and | |
the world. | |
β’ Steward: | |
β Committed to social justice, equity, and sustainability. | |
β Takes action to effect constructive, and respectful change. | |
β SKILLS & EXPERTISE | |
β’ Analytical: | |
β Distills information, evaluates arguments, and makes informed decisions. | |
β Identifies assumptions, biases, and fallacies in arguments. | |
β’ Collaborative: | |
β Encourages the sharing of ideas and opinions. | |
β Actively contributes to finding common ground and solutions. | |
β’ Communicative: | |
β Effectively expresses their ideas clearly and succinctly, listens actively to | |
others, and asks clarifying questions. | |
β’ Disciplined: | |
β Exhibits restraint in the face of distraction | |
β Effectively prioritizes needs and responsibilities over desires | |
β Mindful of time and other resource constraints, exhibiting balance in the | |
fulfillment of responsibilities | |
### | |
Do the following: | |
Make a list of 7 big and enduring ideas that students should walk away with. | |
Make a list of 7 essential questions we want students to think about.These questions should be open-ended and provocative. Written in "kid friendly" language. Designed to focus instruction for uncovering the important ideas of the content. | |
Make a list of 7 ideas, concepts, generalizations and principles we want students to know and understand about the unit or topic we are teaching? | |
Make a list of 7 critical skills describing what we want students to be able to do. Each item should begin with "Students will be able to..." | |
Make a list of 7 example assessments we can use to give students opportunities to demonstrate their skills. Explain the assessment idea and which key concepts and skills they map to. | |
Make a list of 7 creative ways that I might adapt the experience for different learning styles. Explain the way I might adapt the experience to the learning style. | |
Make a list of 7 opportunity that this unit can support the learners development towards the "portrait" of a graduate. Each opportunity should identify the trait and how it might be applied. | |
""" | |
unit_generator = UnitGenerator(prompt_template=agent_prompt) | |
def generate_unit(input): | |
for next_token, content in unit_generator.stream(input): | |
yield(content) | |
gr.Interface(generate_unit, | |
[gr.Textbox( | |
label="Enter your unit vision.", | |
info="Provide high level details for the learning experience. Feel free to add any additional integrations, concepts, etc you want incorporated in the unit design." | |
)], | |
[gr.Textbox( | |
label="Unit", | |
)],allow_flagging="never").queue().launch(debug=True) |