|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +import os |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import cohere |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +from parea import Parea, trace, trace_insert |
| 10 | + |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +p = Parea(api_key=os.getenv("PAREA_API_KEY")) |
| 14 | +co = cohere.Client(api_key=os.getenv("COHERE_API_KEY")) |
| 15 | +p.wrap_cohere_client(co) |
| 16 | + |
| 17 | + |
| 18 | +def call_llm(message: str, chat_history: Optional[List[dict]] = None, system_message: str = "", model: str = "command-r-plus") -> str: |
| 19 | + return co.chat( |
| 20 | + model=model, |
| 21 | + preamble=system_message, |
| 22 | + chat_history=chat_history or [], |
| 23 | + message=message, |
| 24 | + ).text |
| 25 | + |
| 26 | + |
| 27 | +@trace |
| 28 | +def argumentor(query: str, additional_description: str = "") -> str: |
| 29 | + return call_llm( |
| 30 | + system_message=f"""You are a debater making an argument on a topic. {additional_description}. |
| 31 | + The current time is {datetime.now().strftime("%Y-%m-%d")}""", |
| 32 | + message=f"The discussion topic is {query}", |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@trace |
| 37 | +def critic(argument: str) -> str: |
| 38 | + return call_llm( |
| 39 | + system_message="""You are a critic. |
| 40 | + What unresolved questions or criticism do you have after reading the following argument? |
| 41 | + Provide a concise summary of your feedback.""", |
| 42 | + message=argument, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@trace |
| 47 | +def refiner(query: str, additional_description: str, argument: str, criticism: str) -> str: |
| 48 | + return call_llm( |
| 49 | + system_message=f"""You are a debater making an argument on a topic. {additional_description}. |
| 50 | + The current time is {datetime.now().strftime("%Y-%m-%d")}""", |
| 51 | + chat_history=[{"role": "USER", "message": f"""The discussion topic is {query}"""}, {"role": "CHATBOT", "message": argument}, {"role": "USER", "message": criticism}], |
| 52 | + message="Please generate a new argument that incorporates the feedback from the user.", |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +@trace |
| 57 | +def argument_chain(query: str, additional_description: str = "") -> str: |
| 58 | + trace_insert({"session_id": "cus_1234", "end_user_identifier": "user_1234"}) |
| 59 | + argument = argumentor(query, additional_description) |
| 60 | + criticism = critic(argument) |
| 61 | + refined_argument = refiner(query, additional_description, argument, criticism) |
| 62 | + return refined_argument |
| 63 | + |
| 64 | + |
| 65 | +@trace(session_id="cus_1234", end_user_identifier="user_1234") |
| 66 | +def json_call() -> str: |
| 67 | + completion = co.chat( |
| 68 | + model="command-r-plus", |
| 69 | + preamble="You are a helpful assistant talking in JSON.", |
| 70 | + message="What are you?", |
| 71 | + response_format={"type": "json_object"}, |
| 72 | + ) |
| 73 | + return completion.text |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + result = argument_chain( |
| 78 | + "Whether sparkling wine is good for you.", |
| 79 | + additional_description="Provide a concise, few sentence argument on why sparkling wine is good for you.", |
| 80 | + ) |
| 81 | + # print(result) |
| 82 | + # print(json_call()) |
0 commit comments