Created
January 21, 2023 17:05
-
-
Save jflam/41528588f8087e4becf713d863ae7c87 to your computer and use it in GitHub Desktop.
Citations Needed with Bing and Azure Open AI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Updated version which uses Bing Search API (you'll need to sign up - free | |
# tier available) It also uses the Azure OpenAI service. | |
import streamlit as st | |
import json, os, requests, openai | |
from langchain.prompts import PromptTemplate | |
# You'll only need this for public OpenAI endpoint | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
# You'll need this for Azure OpenAI service | |
openai.api_base = os.environ["OPENAI_ENDPOINT"] | |
openai.api_type = 'azure' | |
openai.api_version = '2022-12-01' # this may change in the future | |
deployment_id= os.environ["OPENAI_DEPLOYMENT"] | |
# You'll need to create your own Bing API service for this | |
BING_API_KEY = os.environ["BING_API_KEY"] | |
BING_ENDPOINT = os.environ["BING_API_ENDPOINT"] + "/v7.0/search" | |
prompt = PromptTemplate( | |
input_variables=["answer", "number"], | |
template=""" | |
Below is an answer. It needs citations. I want you to find {number} ideas or | |
facts in the answer that could benefit from citations and formulate Google | |
queries to retrieve documents that could be used in citations. | |
Only return your answer in a JSON structure. The format should resemble: | |
{{ | |
"citations": [ | |
{{ | |
"statement": "Statement 1", | |
"query", "Google query 1" | |
}}, | |
{{ | |
"statement": "Statement 2", | |
"query", "Google query 2" | |
}}, | |
... | |
] | |
}} | |
Answer: {answer} | |
""") | |
# Construct a request to be sent to Bing | |
def search_bing(query) -> dict: | |
params = { 'q': query, 'mkt': 'en-US', 'count': 5 } | |
headers = { "Ocp-Apim-Subscription-Key": BING_API_KEY } | |
response = requests.get(BING_ENDPOINT, headers=headers, params=params) | |
response.raise_for_status() | |
return response.json() | |
"## Get citations for an answer from ChatGPT using Bing!" | |
answer = st.text_area("Answer", | |
placeholder="Paste answer from ChatGPT here", | |
height=200) | |
query = prompt.format(answer=answer, number=3) | |
next_queries = openai.Completion.create( | |
engine=deployment_id, | |
prompt=query, | |
max_tokens=1000) | |
# DEBUG only | |
# st.write(next_queries) | |
results = json.loads(next_queries.choices[0].text) | |
for result in results["citations"]: | |
statement = result["statement"] | |
search_results = search_bing(result["query"]) | |
md = f"###### Citations for: '{statement}'\n\n" | |
number = 1 | |
for result in search_results["webPages"]["value"]: | |
md += f"**{number}.** [{result['name']}]({result['url']})\n\n" | |
md += f"{result['snippet']}\n\n" | |
number += 1 | |
st.markdown(md) | |
"### Raw JSON of search results" | |
with st.expander("Show JSON"): | |
st.json(search_results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment