Spaces:
Sleeping
Sleeping
File size: 2,884 Bytes
d56ba03 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import os
from functools import lru_cache
import streamlit as st
import anthropic
import openai
# st.set_page_config(layout="wide")
#
os.environ[
"ANTHROPIC_API_KEY"
] = "sk-ant-hoJDA77YMGZ5Rmxt9IYgzkavTj-VdnwXnJINAgUJCgUHtF9uIFLYaLoCFKGbA7Bupd606KVNaDmbYfTANCPygw"
# hackathon-nyc-5
os.environ["OPENAI_API_KEY"] = "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"
class AnthropicSerivce:
def __init__(self):
self.client = self.anthropic_client()
@staticmethod
@lru_cache
def anthropic_client():
return anthropic.Client(os.environ["ANTHROPIC_API_KEY"])
def prompt(self, prompt, max_tokens_to_sample: int = 2000):
response = self.client.completion_stream(
prompt=f"{anthropic.HUMAN_PROMPT} {prompt}{anthropic.AI_PROMPT}",
stop_sequences=[anthropic.HUMAN_PROMPT],
max_tokens_to_sample=max_tokens_to_sample,
model="claude-v1",
stream=True,
)
return response
class OpenAIService:
def __init__(self):
openai.api_key = os.environ["OPENAI_API_KEY"]
def list_models(self):
return openai.Model.list()
def prompt(self, prompt):
# return openai.Completion.create(model="ada", prompt=prompt)
return openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
# model="gpt-4", messages=[{"role": "user", "content": prompt}]
)
def app():
prompt = st.text_area("Prompt", value="How many toes do dogs have?", height=100)
tabs = st.tabs(["Anthropic", "OpenAI"])
if prompt:
# -- Antrhopic
with tabs[0]:
anthropic_service = AnthropicSerivce()
response = anthropic_service.prompt(prompt=prompt)
# for data in response:
# col = st.columns(2)
# with col[0]:
# st.markdown("---")
# st.write(data)
# with col[1]:
# st.markdown("---")
# st.markdown(data["completion"])
# -- pick the last response
col = st.columns(2)
answer = list(response)[-1]
with col[0]:
st.write(answer)
with col[1]:
st.code(answer["completion"])
# -- OpenAI
with tabs[1]:
openai_service = OpenAIService()
response = openai_service.prompt(prompt=prompt)
col = st.columns(2)
# st.write(openai_service.list_models())
with col[0]:
st.write(response)
st.markdown("---")
with col[1]:
answer = response.choices[0]["message"]["content"]
st.code(answer, language="python")
st.markdown("---")
if __name__ == "__main__":
app()
|